From 69c659ebd3387ec331a30d6bf53d989803f7276f Mon Sep 17 00:00:00 2001 From: Kamil Trzcinski Date: Tue, 23 Jun 2015 15:45:24 +0200 Subject: Compress database backup --- lib/backup/database.rb | 14 ++++++++++++++ 1 file changed, 14 insertions(+) (limited to 'lib/backup') diff --git a/lib/backup/database.rb b/lib/backup/database.rb index 9ab6aca276d..f677f8def2b 100644 --- a/lib/backup/database.rb +++ b/lib/backup/database.rb @@ -22,9 +22,19 @@ module Backup end report_success(success) abort 'Backup failed' unless success + + $progress.print 'Compressing database ... ' + success = system('gzip', db_file_name) + report_success(success) + abort 'Backup failed: compress error' unless success end def restore + $progress.print 'Decompressing database ... ' + success = system('gzip', '-d', db_file_name_gz) + report_success(success) + abort 'Restore failed: decompress error' unless success + success = case config["adapter"] when /^mysql/ then $progress.print "Restoring MySQL database #{config['database']} ... " @@ -48,6 +58,10 @@ module Backup File.join(db_dir, 'database.sql') end + def db_file_name_gz + File.join(db_dir, 'database.sql.gz') + end + def mysql_args args = { 'host' => '--host', -- cgit v1.2.1 From 90ab5a59bb28053c9da768679b8036caa7885e95 Mon Sep 17 00:00:00 2001 From: Jacob Vosmaer Date: Tue, 7 Jul 2015 15:34:06 +0200 Subject: Use native Postgres database cleaning during backup restore We were using hacks to drop tables etc during a Postgres backup restore. With this change, we let pg_dump insert the DROP TABLE statements it needs at the start of the SQL dump. --- lib/backup/database.rb | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) (limited to 'lib/backup') diff --git a/lib/backup/database.rb b/lib/backup/database.rb index 9ab6aca276d..7aa54dd55ae 100644 --- a/lib/backup/database.rb +++ b/lib/backup/database.rb @@ -18,7 +18,8 @@ module Backup when "postgresql" then $progress.print "Dumping PostgreSQL database #{config['database']} ... " pg_env - system('pg_dump', config['database'], out: db_file_name) + # Pass '--clean' to include 'DROP TABLE' statements in the DB dump. + system('pg_dump', '--clean', config['database'], out: db_file_name) end report_success(success) abort 'Backup failed' unless success @@ -31,10 +32,6 @@ module Backup system('mysql', *mysql_args, config['database'], in: db_file_name) when "postgresql" then $progress.print "Restoring PostgreSQL database #{config['database']} ... " - # Drop all tables because PostgreSQL DB dumps do not contain DROP TABLE - # statements like MySQL. - Rake::Task["gitlab:db:drop_all_tables"].invoke - Rake::Task["gitlab:db:drop_all_postgres_sequences"].invoke pg_env system('psql', config['database'], '-f', db_file_name) end -- cgit v1.2.1 From 346b07497989c824b201e501dfa24b8af630da8a Mon Sep 17 00:00:00 2001 From: Jacob Vosmaer Date: Tue, 21 Jul 2015 10:37:27 +0200 Subject: Don't stop if database.sql.gz already exists The existing behavior of the backups is to overwrite whatever data was still there in the scratch directories. This broke when we added a 'gzip' step because 'gzip database.sql' will fail if 'database.sql.gz' already exists. Doing 'rm -f database.sql.gz' before the 'gzip' avoids this failure. --- lib/backup/database.rb | 1 + 1 file changed, 1 insertion(+) (limited to 'lib/backup') diff --git a/lib/backup/database.rb b/lib/backup/database.rb index b8aa6b9ff2f..c5a5396cbbf 100644 --- a/lib/backup/database.rb +++ b/lib/backup/database.rb @@ -25,6 +25,7 @@ module Backup abort 'Backup failed' unless success $progress.print 'Compressing database ... ' + FileUtils.rm_f db_file_name_gz success = system('gzip', db_file_name) report_success(success) abort 'Backup failed: compress error' unless success -- cgit v1.2.1