diff options
Diffstat (limited to 'lib/backup')
-rw-r--r-- | lib/backup/artifacts.rb | 4 | ||||
-rw-r--r-- | lib/backup/builds.rb | 4 | ||||
-rw-r--r-- | lib/backup/database.rb | 86 | ||||
-rw-r--r-- | lib/backup/files.rb | 32 | ||||
-rw-r--r-- | lib/backup/helper.rb | 25 | ||||
-rw-r--r-- | lib/backup/lfs.rb | 4 | ||||
-rw-r--r-- | lib/backup/manager.rb | 60 | ||||
-rw-r--r-- | lib/backup/pages.rb | 4 | ||||
-rw-r--r-- | lib/backup/registry.rb | 4 | ||||
-rw-r--r-- | lib/backup/repository.rb | 12 | ||||
-rw-r--r-- | lib/backup/uploads.rb | 4 |
11 files changed, 118 insertions, 121 deletions
diff --git a/lib/backup/artifacts.rb b/lib/backup/artifacts.rb index 33658ae225f..fbc058b54bc 100644 --- a/lib/backup/artifacts.rb +++ b/lib/backup/artifacts.rb @@ -1,6 +1,6 @@ # frozen_string_literal: true -require 'backup/files' +require "backup/files" module Backup class Artifacts < Files @@ -9,7 +9,7 @@ module Backup def initialize(progress) @progress = progress - super('artifacts', JobArtifactUploader.root) + super("artifacts", JobArtifactUploader.root) end end end diff --git a/lib/backup/builds.rb b/lib/backup/builds.rb index 5e795a449de..766a35972f5 100644 --- a/lib/backup/builds.rb +++ b/lib/backup/builds.rb @@ -1,6 +1,6 @@ # frozen_string_literal: true -require 'backup/files' +require "backup/files" module Backup class Builds < Files @@ -9,7 +9,7 @@ module Backup def initialize(progress) @progress = progress - super('builds', Settings.gitlab_ci.builds_path) + super("builds", Settings.gitlab_ci.builds_path) end end end diff --git a/lib/backup/database.rb b/lib/backup/database.rb index e6bf3d1856f..b35fe4142eb 100644 --- a/lib/backup/database.rb +++ b/lib/backup/database.rb @@ -1,6 +1,6 @@ # frozen_string_literal: true -require 'yaml' +require "yaml" module Backup class Database @@ -9,26 +9,26 @@ module Backup def initialize(progress) @progress = progress - @config = YAML.load_file(File.join(Rails.root, 'config', 'database.yml'))[Rails.env] - @db_file_name = File.join(Gitlab.config.backup.path, 'db', 'database.sql.gz') + @config = YAML.load_file(File.join(Rails.root, "config", "database.yml"))[Rails.env] + @db_file_name = File.join(Gitlab.config.backup.path, "db", "database.sql.gz") end def dump FileUtils.mkdir_p(File.dirname(db_file_name)) FileUtils.rm_f(db_file_name) compress_rd, compress_wr = IO.pipe - compress_pid = spawn(*%w(gzip -1 -c), in: compress_rd, out: [db_file_name, 'w', 0600]) + compress_pid = spawn("gzip", "-1", "-c", in: compress_rd, out: [db_file_name, "w", 0o600]) compress_rd.close dump_pid = case config["adapter"] when /^mysql/ then - progress.print "Dumping MySQL database #{config['database']} ... " + progress.print "Dumping MySQL database #{config["database"]} ... " # Workaround warnings from MySQL 5.6 about passwords on cmd line - ENV['MYSQL_PWD'] = config["password"].to_s if config["password"] - spawn('mysqldump', *mysql_args, config['database'], out: compress_wr) + ENV["MYSQL_PWD"] = config["password"].to_s if config["password"] + spawn("mysqldump", *mysql_args, config["database"], out: compress_wr) when "postgresql" then - progress.print "Dumping PostgreSQL database #{config['database']} ... " + progress.print "Dumping PostgreSQL database #{config["database"]} ... " pg_env pgsql_args = ["--clean"] # Pass '--clean' to include 'DROP TABLE' statements in the DB dump. if Gitlab.config.backup.pg_schema @@ -36,88 +36,88 @@ module Backup pgsql_args << Gitlab.config.backup.pg_schema end - spawn('pg_dump', *pgsql_args, config['database'], out: compress_wr) + spawn("pg_dump", *pgsql_args, config["database"], out: compress_wr) end compress_wr.close - success = [compress_pid, dump_pid].all? do |pid| + success = [compress_pid, dump_pid].all? { |pid| Process.waitpid(pid) $?.success? - end + } report_success(success) - raise Backup::Error, 'Backup failed' unless success + raise Backup::Error, "Backup failed" unless success end def restore decompress_rd, decompress_wr = IO.pipe - decompress_pid = spawn(*%w(gzip -cd), out: decompress_wr, in: db_file_name) + decompress_pid = spawn("gzip", "-cd", out: decompress_wr, in: db_file_name) decompress_wr.close restore_pid = case config["adapter"] when /^mysql/ then - progress.print "Restoring MySQL database #{config['database']} ... " + progress.print "Restoring MySQL database #{config["database"]} ... " # Workaround warnings from MySQL 5.6 about passwords on cmd line - ENV['MYSQL_PWD'] = config["password"].to_s if config["password"] - spawn('mysql', *mysql_args, config['database'], in: decompress_rd) + ENV["MYSQL_PWD"] = config["password"].to_s if config["password"] + spawn("mysql", *mysql_args, config["database"], in: decompress_rd) when "postgresql" then - progress.print "Restoring PostgreSQL database #{config['database']} ... " + progress.print "Restoring PostgreSQL database #{config["database"]} ... " pg_env - spawn('psql', config['database'], in: decompress_rd) + spawn("psql", config["database"], in: decompress_rd) end decompress_rd.close - success = [decompress_pid, restore_pid].all? do |pid| + success = [decompress_pid, restore_pid].all? { |pid| Process.waitpid(pid) $?.success? - end + } report_success(success) - abort Backup::Error, 'Restore failed' unless success + abort Backup::Error, "Restore failed" unless success end protected def mysql_args args = { - 'host' => '--host', - 'port' => '--port', - 'socket' => '--socket', - 'username' => '--user', - 'encoding' => '--default-character-set', + "host" => "--host", + "port" => "--port", + "socket" => "--socket", + "username" => "--user", + "encoding" => "--default-character-set", # SSL - 'sslkey' => '--ssl-key', - 'sslcert' => '--ssl-cert', - 'sslca' => '--ssl-ca', - 'sslcapath' => '--ssl-capath', - 'sslcipher' => '--ssl-cipher' + "sslkey" => "--ssl-key", + "sslcert" => "--ssl-cert", + "sslca" => "--ssl-ca", + "sslcapath" => "--ssl-capath", + "sslcipher" => "--ssl-cipher", } args.map { |opt, arg| "#{arg}=#{config[opt]}" if config[opt] }.compact end def pg_env args = { - 'username' => 'PGUSER', - 'host' => 'PGHOST', - 'port' => 'PGPORT', - 'password' => 'PGPASSWORD', + "username" => "PGUSER", + "host" => "PGHOST", + "port" => "PGPORT", + "password" => "PGPASSWORD", # SSL - 'sslmode' => 'PGSSLMODE', - 'sslkey' => 'PGSSLKEY', - 'sslcert' => 'PGSSLCERT', - 'sslrootcert' => 'PGSSLROOTCERT', - 'sslcrl' => 'PGSSLCRL', - 'sslcompression' => 'PGSSLCOMPRESSION' + "sslmode" => "PGSSLMODE", + "sslkey" => "PGSSLKEY", + "sslcert" => "PGSSLCERT", + "sslrootcert" => "PGSSLROOTCERT", + "sslcrl" => "PGSSLCRL", + "sslcompression" => "PGSSLCOMPRESSION", } args.each { |opt, arg| ENV[arg] = config[opt].to_s if config[opt] } end def report_success(success) if success - progress.puts '[DONE]'.color(:green) + progress.puts "[DONE]".color(:green) else - progress.puts '[FAILED]'.color(:red) + progress.puts "[FAILED]".color(:red) end end end diff --git a/lib/backup/files.rb b/lib/backup/files.rb index 2bac84846c5..03b41c17d78 100644 --- a/lib/backup/files.rb +++ b/lib/backup/files.rb @@ -1,7 +1,7 @@ # frozen_string_literal: true -require 'open3' -require_relative 'helper' +require "open3" +require_relative "helper" module Backup class Files @@ -12,9 +12,9 @@ module Backup def initialize(name, app_files_dir) @name = name @app_files_dir = File.realpath(app_files_dir) - @files_parent_dir = File.realpath(File.join(@app_files_dir, '..')) - @backup_files_dir = File.join(Gitlab.config.backup.path, File.basename(@app_files_dir) ) - @backup_tarball = File.join(Gitlab.config.backup.path, name + '.tar.gz') + @files_parent_dir = File.realpath(File.join(@app_files_dir, "..")) + @backup_files_dir = File.join(Gitlab.config.backup.path, File.basename(@app_files_dir)) + @backup_tarball = File.join(Gitlab.config.backup.path, name + ".tar.gz") end # Copy files from public/files to backup/files @@ -22,34 +22,34 @@ module Backup FileUtils.mkdir_p(Gitlab.config.backup.path) FileUtils.rm_f(backup_tarball) - if ENV['STRATEGY'] == 'copy' - cmd = %W(rsync -a --exclude=lost+found #{app_files_dir} #{Gitlab.config.backup.path}) + if ENV["STRATEGY"] == "copy" + cmd = %W[rsync -a --exclude=lost+found #{app_files_dir} #{Gitlab.config.backup.path}] output, status = Gitlab::Popen.popen(cmd) unless status.zero? puts output - raise Backup::Error, 'Backup failed' + raise Backup::Error, "Backup failed" end - run_pipeline!([%W(#{tar} --exclude=lost+found -C #{@backup_files_dir} -cf - .), %w(gzip -c -1)], out: [backup_tarball, 'w', 0600]) + run_pipeline!([%W[#{tar} --exclude=lost+found -C #{@backup_files_dir} -cf - .], %w[gzip -c -1]], out: [backup_tarball, "w", 0o600]) FileUtils.rm_rf(@backup_files_dir) else - run_pipeline!([%W(#{tar} --exclude=lost+found -C #{app_files_dir} -cf - .), %w(gzip -c -1)], out: [backup_tarball, 'w', 0600]) + run_pipeline!([%W[#{tar} --exclude=lost+found -C #{app_files_dir} -cf - .], %w[gzip -c -1]], out: [backup_tarball, "w", 0o600]) end end def restore backup_existing_files_dir - run_pipeline!([%w(gzip -cd), %W(#{tar} --unlink-first --recursive-unlink -C #{app_files_dir} -xf -)], in: backup_tarball) + run_pipeline!([%w[gzip -cd], %W[#{tar} --unlink-first --recursive-unlink -C #{app_files_dir} -xf -]], in: backup_tarball) end def tar - if system(*%w[gtar --version], out: '/dev/null') + if system("gtar", "--version", out: "/dev/null") # It looks like we can get GNU tar by running 'gtar' - 'gtar' + "gtar" else - 'tar' + "tar" end end @@ -58,7 +58,7 @@ module Backup if File.exist?(app_files_dir) # Move all files in the existing repos directory except . and .. to # repositories.old.<timestamp> directory - FileUtils.mkdir_p(timestamped_files_path, mode: 0700) + FileUtils.mkdir_p(timestamped_files_path, mode: 0o700) files = Dir.glob(File.join(app_files_dir, "*"), File::FNM_DOTMATCH) - [File.join(app_files_dir, "."), File.join(app_files_dir, "..")] begin FileUtils.mv(files, timestamped_files_path) @@ -78,7 +78,7 @@ module Backup return if status.compact.all?(&:success?) regex = /^g?tar: \.: Cannot mkdir: No such file or directory$/ - raise Backup::Error, 'Backup failed' unless err_r.read =~ regex + raise Backup::Error, "Backup failed" unless err_r.read =~ regex end end end diff --git a/lib/backup/helper.rb b/lib/backup/helper.rb index 22f00aef569..fd3fe1b47f9 100644 --- a/lib/backup/helper.rb +++ b/lib/backup/helper.rb @@ -4,13 +4,12 @@ module Backup module Helper def access_denied_error(path) message = <<~EOS - - ### NOTICE ### - As part of restore, the task tried to move existing content from #{path}. - However, it seems that directory contains files/folders that are not owned - by the user #{Gitlab.config.gitlab.user}. To proceed, please move the files - or folders inside #{path} to a secure location so that #{path} is empty and - run restore task again. + ### NOTICE ### + As part of restore, the task tried to move existing content from #{path}. + However, it seems that directory contains files/folders that are not owned + by the user #{Gitlab.config.gitlab.user}. To proceed, please move the files + or folders inside #{path} to a secure location so that #{path} is empty and + run restore task again. EOS raise message @@ -18,13 +17,11 @@ module Backup def resource_busy_error(path) message = <<~EOS - - ### NOTICE ### - As part of restore, the task tried to rename `#{path}` before restoring. - This could not be completed, perhaps `#{path}` is a mountpoint? - - To complete the restore, please move the contents of `#{path}` to a - different location and run the restore task again. + ### NOTICE ### + As part of restore, the task tried to rename `#{path}` before restoring. + This could not be completed, perhaps `#{path}` is a mountpoint? + To complete the restore, please move the contents of `#{path}` to a + different location and run the restore task again. EOS raise message diff --git a/lib/backup/lfs.rb b/lib/backup/lfs.rb index 0dfe56e214f..689e8093b58 100644 --- a/lib/backup/lfs.rb +++ b/lib/backup/lfs.rb @@ -1,6 +1,6 @@ # frozen_string_literal: true -require 'backup/files' +require "backup/files" module Backup class Lfs < Files @@ -9,7 +9,7 @@ module Backup def initialize(progress) @progress = progress - super('lfs', Settings.lfs.storage_path) + super("lfs", Settings.lfs.storage_path) end end end diff --git a/lib/backup/manager.rb b/lib/backup/manager.rb index 06b0338b1ed..52fbbbda382 100644 --- a/lib/backup/manager.rb +++ b/lib/backup/manager.rb @@ -4,7 +4,7 @@ module Backup class Manager ARCHIVES_TO_BACKUP = %w[uploads builds artifacts pages lfs registry].freeze FOLDERS_TO_BACKUP = %w[repositories db].freeze - FILE_NAME_SUFFIX = '_gitlab_backup.tar'.freeze + FILE_NAME_SUFFIX = "_gitlab_backup.tar" attr_reader :progress @@ -18,18 +18,18 @@ module Backup Dir.chdir(backup_path) do File.open("#{backup_path}/backup_information.yml", "w+") do |file| - file << backup_information.to_yaml.gsub(/^---\n/, '') + file << backup_information.to_yaml.gsub(/^---\n/, "") end # create archive progress.print "Creating backup archive: #{tar_file} ... " # Set file permissions on open to prevent chmod races. - tar_system_options = { out: [tar_file, 'w', Gitlab.config.backup.archive_permissions] } - if Kernel.system('tar', '-cf', '-', *backup_contents, tar_system_options) + tar_system_options = {out: [tar_file, "w", Gitlab.config.backup.archive_permissions]} + if Kernel.system("tar", "-cf", "-", *backup_contents, tar_system_options) progress.puts "done".color(:green) else puts "creating archive #{tar_file} failed".color(:red) - raise Backup::Error, 'Backup failed' + raise Backup::Error, "Backup failed" end upload @@ -55,7 +55,7 @@ module Backup progress.puts "done".color(:green) else puts "uploading backup to #{remote_directory} failed".color(:red) - raise Backup::Error, 'Backup failed' + raise Backup::Error, "Backup failed" end end @@ -69,7 +69,7 @@ module Backup progress.puts "done".color(:green) else puts "deleting tmp directory '#{dir}' failed".color(:red) - raise Backup::Error, 'Backup failed' + raise Backup::Error, "Backup failed" end end end @@ -118,35 +118,35 @@ module Backup progress.puts "Please make sure that file name ends with #{FILE_NAME_SUFFIX}" exit 1 elsif backup_file_list.many? && ENV["BACKUP"].nil? - progress.puts 'Found more than one backup:' + progress.puts "Found more than one backup:" # print list of available backups progress.puts " " + available_timestamps.join("\n ") - progress.puts 'Please specify which one you want to restore:' - progress.puts 'rake gitlab:backup:restore BACKUP=timestamp_of_backup' + progress.puts "Please specify which one you want to restore:" + progress.puts "rake gitlab:backup:restore BACKUP=timestamp_of_backup" exit 1 end - tar_file = if ENV['BACKUP'].present? - "#{ENV['BACKUP']}#{FILE_NAME_SUFFIX}" - else - backup_file_list.first - end + tar_file = if ENV["BACKUP"].present? + "#{ENV["BACKUP"]}#{FILE_NAME_SUFFIX}" + else + backup_file_list.first + end unless File.exist?(tar_file) progress.puts "The backup file #{tar_file} does not exist!" exit 1 end - progress.print 'Unpacking backup ... ' + progress.print "Unpacking backup ... " - unless Kernel.system(*%W(tar -xf #{tar_file})) - progress.puts 'unpacking backup failed'.color(:red) - exit 1 + if Kernel.system("tar", "-xf", tar_file.to_s) + progress.puts "done".color(:green) else - progress.puts 'done'.color(:green) + progress.puts "unpacking backup failed".color(:red) + exit 1 end - ENV["VERSION"] = "#{settings[:db_version]}" if settings[:db_version].to_i > 0 + ENV["VERSION"] = settings[:db_version].to_s if settings[:db_version].to_i > 0 # restoring mismatching backups can lead to unexpected problems if settings[:gitlab_version] != Gitlab::VERSION @@ -164,12 +164,12 @@ module Backup end def tar_version - tar_version, _ = Gitlab::Popen.popen(%w(tar --version)) - tar_version.dup.force_encoding('locale').split("\n").first + tar_version, _ = Gitlab::Popen.popen(%w[tar --version]) + tar_version.dup.force_encoding("locale").split("\n").first end def skipped?(item) - settings[:skipped] && settings[:skipped].include?(item) || disabled_features.include?(item) + settings[:skipped]&.include?(item) || disabled_features.include?(item) end private @@ -183,7 +183,7 @@ module Backup end def available_timestamps - @backup_file_list.map {|item| item.gsub("#{FILE_NAME_SUFFIX}", "")} + @backup_file_list.map {|item| item.gsub(FILE_NAME_SUFFIX.to_s, "")} end def connect_to_remote_directory(connection_settings) @@ -205,8 +205,8 @@ module Backup end def remote_target - if ENV['DIRECTORY'] - File.join(ENV['DIRECTORY'], tar_file) + if ENV["DIRECTORY"] + File.join(ENV["DIRECTORY"], tar_file) else tar_file end @@ -226,7 +226,7 @@ module Backup def disabled_features features = [] - features << 'registry' unless Gitlab.config.registry.enabled + features << "registry" unless Gitlab.config.registry.enabled features end @@ -235,7 +235,7 @@ module Backup end def tar_file - @tar_file ||= "#{backup_information[:backup_created_at].strftime('%s_%Y_%m_%d_')}#{backup_information[:gitlab_version]}#{FILE_NAME_SUFFIX}" + @tar_file ||= "#{backup_information[:backup_created_at].strftime("%s_%Y_%m_%d_")}#{backup_information[:gitlab_version]}#{FILE_NAME_SUFFIX}" end def backup_information @@ -245,7 +245,7 @@ module Backup gitlab_version: Gitlab::VERSION, tar_version: tar_version, installation_type: Gitlab::INSTALLATION_TYPE, - skipped: ENV["SKIP"] + skipped: ENV["SKIP"], } end end diff --git a/lib/backup/pages.rb b/lib/backup/pages.rb index a4be728df08..10a915cfd41 100644 --- a/lib/backup/pages.rb +++ b/lib/backup/pages.rb @@ -1,6 +1,6 @@ # frozen_string_literal: true -require 'backup/files' +require "backup/files" module Backup class Pages < Files @@ -9,7 +9,7 @@ module Backup def initialize(progress) @progress = progress - super('pages', Gitlab.config.pages.path) + super("pages", Gitlab.config.pages.path) end end end diff --git a/lib/backup/registry.rb b/lib/backup/registry.rb index d16ed2facf1..1fdacccea26 100644 --- a/lib/backup/registry.rb +++ b/lib/backup/registry.rb @@ -1,6 +1,6 @@ # frozen_string_literal: true -require 'backup/files' +require "backup/files" module Backup class Registry < Files @@ -9,7 +9,7 @@ module Backup def initialize(progress) @progress = progress - super('registry', Settings.registry.path) + super("registry", Settings.registry.path) end end end diff --git a/lib/backup/repository.rb b/lib/backup/repository.rb index 22ed1d8e7b4..76763b4a0f3 100644 --- a/lib/backup/repository.rb +++ b/lib/backup/repository.rb @@ -1,6 +1,6 @@ # frozen_string_literal: true -require 'yaml' +require "yaml" module Backup class Repository @@ -54,7 +54,7 @@ module Backup backup_custom_hooks(project) rescue => e - progress_warn(project, e, 'Failed to backup repo') + progress_warn(project, e, "Failed to backup repo") end def backup_custom_hooks(project) @@ -125,7 +125,7 @@ module Backup protected def path_to_bundle(project) - File.join(backup_repos_path, project.disk_path + '.bundle') + File.join(backup_repos_path, project.disk_path + ".bundle") end def project_backup_path(project) @@ -137,13 +137,13 @@ module Backup end def backup_repos_path - File.join(Gitlab.config.backup.path, 'repositories') + File.join(Gitlab.config.backup.path, "repositories") end def prepare FileUtils.rm_rf(backup_repos_path) FileUtils.mkdir_p(Gitlab.config.backup.path) - FileUtils.mkdir(backup_repos_path, mode: 0700) + FileUtils.mkdir(backup_repos_path, mode: 0o700) end private @@ -167,7 +167,7 @@ module Backup progress.puts " - Object pool #{pool.disk_path}..." pool.source_project ||= pool.member_projects.first.root_of_fork_network - pool.state = 'none' + pool.state = "none" pool.save pool.schedule diff --git a/lib/backup/uploads.rb b/lib/backup/uploads.rb index 9577df2634a..72c6e823026 100644 --- a/lib/backup/uploads.rb +++ b/lib/backup/uploads.rb @@ -1,6 +1,6 @@ # frozen_string_literal: true -require 'backup/files' +require "backup/files" module Backup class Uploads < Files @@ -9,7 +9,7 @@ module Backup def initialize(progress) @progress = progress - super('uploads', Rails.root.join('public/uploads')) + super("uploads", Rails.root.join("public/uploads")) end end end |