Welcome to mirror list, hosted at ThFree Co, Russian Federation.

gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
Diffstat (limited to 'lib/backup')
-rw-r--r--lib/backup/database.rb78
-rw-r--r--lib/backup/manager.rb170
-rw-r--r--lib/backup/repository.rb140
-rw-r--r--lib/backup/uploads.rb30
4 files changed, 0 insertions, 418 deletions
diff --git a/lib/backup/database.rb b/lib/backup/database.rb
deleted file mode 100644
index 9ab6aca276d..00000000000
--- a/lib/backup/database.rb
+++ /dev/null
@@ -1,78 +0,0 @@
-require 'yaml'
-
-module Backup
- class Database
- attr_reader :config, :db_dir
-
- def initialize
- @config = YAML.load_file(File.join(Rails.root,'config','database.yml'))[Rails.env]
- @db_dir = File.join(Gitlab.config.backup.path, 'db')
- FileUtils.mkdir_p(@db_dir) unless Dir.exists?(@db_dir)
- end
-
- def dump
- success = case config["adapter"]
- when /^mysql/ then
- $progress.print "Dumping MySQL database #{config['database']} ... "
- system('mysqldump', *mysql_args, config['database'], out: db_file_name)
- when "postgresql" then
- $progress.print "Dumping PostgreSQL database #{config['database']} ... "
- pg_env
- system('pg_dump', config['database'], out: db_file_name)
- end
- report_success(success)
- abort 'Backup failed' unless success
- end
-
- def restore
- success = case config["adapter"]
- when /^mysql/ then
- $progress.print "Restoring MySQL database #{config['database']} ... "
- 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
- report_success(success)
- abort 'Restore failed' unless success
- end
-
- protected
-
- def db_file_name
- File.join(db_dir, 'database.sql')
- end
-
- def mysql_args
- args = {
- 'host' => '--host',
- 'port' => '--port',
- 'socket' => '--socket',
- 'username' => '--user',
- 'encoding' => '--default-character-set',
- 'password' => '--password'
- }
- args.map { |opt, arg| "#{arg}=#{config[opt]}" if config[opt] }.compact
- end
-
- def pg_env
- ENV['PGUSER'] = config["username"] if config["username"]
- ENV['PGHOST'] = config["host"] if config["host"]
- ENV['PGPORT'] = config["port"].to_s if config["port"]
- ENV['PGPASSWORD'] = config["password"].to_s if config["password"]
- end
-
- def report_success(success)
- if success
- $progress.puts '[DONE]'.green
- else
- $progress.puts '[FAILED]'.red
- end
- end
- end
-end
diff --git a/lib/backup/manager.rb b/lib/backup/manager.rb
deleted file mode 100644
index b69aebf9fe1..00000000000
--- a/lib/backup/manager.rb
+++ /dev/null
@@ -1,170 +0,0 @@
-module Backup
- class Manager
- def pack
- # saving additional informations
- s = {}
- s[:db_version] = "#{ActiveRecord::Migrator.current_version}"
- s[:backup_created_at] = Time.now
- s[:gitlab_version] = Gitlab::VERSION
- s[:tar_version] = tar_version
- s[:skipped] = ENV["SKIP"]
- tar_file = "#{s[:backup_created_at].to_i}_gitlab_backup.tar"
-
- Dir.chdir(Gitlab.config.backup.path) do
- File.open("#{Gitlab.config.backup.path}/backup_information.yml",
- "w+") do |file|
- file << s.to_yaml.gsub(/^---\n/,'')
- end
-
- FileUtils.chmod(0700, folders_to_backup)
-
- # create archive
- $progress.print "Creating backup archive: #{tar_file} ... "
- orig_umask = File.umask(0077)
- if Kernel.system('tar', '-cf', tar_file, *backup_contents)
- $progress.puts "done".green
- else
- puts "creating archive #{tar_file} failed".red
- abort 'Backup failed'
- end
- File.umask(orig_umask)
-
- upload(tar_file)
- end
- end
-
- def upload(tar_file)
- remote_directory = Gitlab.config.backup.upload.remote_directory
- $progress.print "Uploading backup archive to remote storage #{remote_directory} ... "
-
- connection_settings = Gitlab.config.backup.upload.connection
- if connection_settings.blank?
- $progress.puts "skipped".yellow
- return
- end
-
- connection = ::Fog::Storage.new(connection_settings)
- directory = connection.directories.get(remote_directory)
-
- if directory.files.create(key: tar_file, body: File.open(tar_file), public: false)
- $progress.puts "done".green
- else
- puts "uploading backup to #{remote_directory} failed".red
- abort 'Backup failed'
- end
- end
-
- def cleanup
- $progress.print "Deleting tmp directories ... "
-
- backup_contents.each do |dir|
- next unless File.exist?(File.join(Gitlab.config.backup.path, dir))
-
- if FileUtils.rm_rf(File.join(Gitlab.config.backup.path, dir))
- $progress.puts "done".green
- else
- puts "deleting tmp directory '#{dir}' failed".red
- abort 'Backup failed'
- end
- end
- end
-
- def remove_old
- # delete backups
- $progress.print "Deleting old backups ... "
- keep_time = Gitlab.config.backup.keep_time.to_i
-
- if keep_time > 0
- removed = 0
-
- Dir.chdir(Gitlab.config.backup.path) do
- file_list = Dir.glob('*_gitlab_backup.tar')
- file_list.map! { |f| $1.to_i if f =~ /(\d+)_gitlab_backup.tar/ }
- file_list.sort.each do |timestamp|
- if Time.at(timestamp) < (Time.now - keep_time)
- if Kernel.system(*%W(rm #{timestamp}_gitlab_backup.tar))
- removed += 1
- end
- end
- end
- end
-
- $progress.puts "done. (#{removed} removed)".green
- else
- $progress.puts "skipping".yellow
- end
- end
-
- def unpack
- Dir.chdir(Gitlab.config.backup.path)
-
- # check for existing backups in the backup dir
- file_list = Dir.glob("*_gitlab_backup.tar").each.map { |f| f.split(/_/).first.to_i }
- puts "no backups found" if file_list.count == 0
-
- if file_list.count > 1 && ENV["BACKUP"].nil?
- puts "Found more than one backup, please specify which one you want to restore:"
- puts "rake gitlab:backup:restore BACKUP=timestamp_of_backup"
- exit 1
- end
-
- tar_file = ENV["BACKUP"].nil? ? File.join("#{file_list.first}_gitlab_backup.tar") : File.join(ENV["BACKUP"] + "_gitlab_backup.tar")
-
- unless File.exists?(tar_file)
- puts "The specified backup doesn't exist!"
- exit 1
- end
-
- $progress.print "Unpacking backup ... "
-
- unless Kernel.system(*%W(tar -xf #{tar_file}))
- puts "unpacking backup failed".red
- exit 1
- else
- $progress.puts "done".green
- end
-
- ENV["VERSION"] = "#{settings[:db_version]}" if settings[:db_version].to_i > 0
-
- # restoring mismatching backups can lead to unexpected problems
- if settings[:gitlab_version] != Gitlab::VERSION
- puts "GitLab version mismatch:".red
- puts " Your current GitLab version (#{Gitlab::VERSION}) differs from the GitLab version in the backup!".red
- puts " Please switch to the following version and try again:".red
- puts " version: #{settings[:gitlab_version]}".red
- puts
- puts "Hint: git checkout v#{settings[:gitlab_version]}"
- exit 1
- end
- end
-
- def tar_version
- tar_version, _ = Gitlab::Popen.popen(%W(tar --version))
- tar_version.force_encoding('locale').split("\n").first
- end
-
- def skipped?(item)
- settings[:skipped] && settings[:skipped].include?(item)
- end
-
- private
-
- def backup_contents
- folders_to_backup + ["backup_information.yml"]
- end
-
- def folders_to_backup
- folders = %w{repositories db uploads}
-
- if ENV["SKIP"]
- return folders.reject{ |folder| ENV["SKIP"].include?(folder) }
- end
-
- folders
- end
-
- def settings
- @settings ||= YAML.load_file("backup_information.yml")
- end
- end
-end
diff --git a/lib/backup/repository.rb b/lib/backup/repository.rb
deleted file mode 100644
index dfb2da9f84e..00000000000
--- a/lib/backup/repository.rb
+++ /dev/null
@@ -1,140 +0,0 @@
-require 'yaml'
-
-module Backup
- class Repository
- attr_reader :repos_path
-
- def dump
- prepare
-
- Project.find_each(batch_size: 1000) do |project|
- $progress.print " * #{project.path_with_namespace} ... "
-
- # Create namespace dir if missing
- FileUtils.mkdir_p(File.join(backup_repos_path, project.namespace.path)) if project.namespace
-
- if project.empty_repo?
- $progress.puts "[SKIPPED]".cyan
- else
- cmd = %W(tar -cf #{path_to_bundle(project)} -C #{path_to_repo(project)} .)
- output, status = Gitlab::Popen.popen(cmd)
- if status.zero?
- $progress.puts "[DONE]".green
- else
- puts "[FAILED]".red
- puts "failed: #{cmd.join(' ')}"
- puts output
- abort 'Backup failed'
- end
- end
-
- wiki = ProjectWiki.new(project)
-
- if File.exists?(path_to_repo(wiki))
- $progress.print " * #{wiki.path_with_namespace} ... "
- if wiki.repository.empty?
- $progress.puts " [SKIPPED]".cyan
- else
- cmd = %W(git --git-dir=#{path_to_repo(wiki)} bundle create #{path_to_bundle(wiki)} --all)
- output, status = Gitlab::Popen.popen(cmd)
- if status.zero?
- $progress.puts " [DONE]".green
- else
- puts " [FAILED]".red
- puts "failed: #{cmd.join(' ')}"
- abort 'Backup failed'
- end
- end
- end
- end
- end
-
- def restore
- if File.exists?(repos_path)
- # Move repos dir to 'repositories.old' dir
- bk_repos_path = File.join(repos_path, '..', 'repositories.old.' + Time.now.to_i.to_s)
- FileUtils.mv(repos_path, bk_repos_path)
- end
-
- FileUtils.mkdir_p(repos_path)
-
- Project.find_each(batch_size: 1000) do |project|
- $progress.print " * #{project.path_with_namespace} ... "
-
- project.namespace.ensure_dir_exist if project.namespace
-
- if File.exists?(path_to_bundle(project))
- FileUtils.mkdir_p(path_to_repo(project))
- cmd = %W(tar -xf #{path_to_bundle(project)} -C #{path_to_repo(project)})
- else
- cmd = %W(git init --bare #{path_to_repo(project)})
- end
-
- if system(*cmd, silent)
- $progress.puts "[DONE]".green
- else
- puts "[FAILED]".red
- puts "failed: #{cmd.join(' ')}"
- abort 'Restore failed'
- end
-
- wiki = ProjectWiki.new(project)
-
- if File.exists?(path_to_bundle(wiki))
- $progress.print " * #{wiki.path_with_namespace} ... "
-
- # If a wiki bundle exists, first remove the empty repo
- # that was initialized with ProjectWiki.new() and then
- # try to restore with 'git clone --bare'.
- FileUtils.rm_rf(path_to_repo(wiki))
- cmd = %W(git clone --bare #{path_to_bundle(wiki)} #{path_to_repo(wiki)})
-
- if system(*cmd, silent)
- $progress.puts " [DONE]".green
- else
- puts " [FAILED]".red
- puts "failed: #{cmd.join(' ')}"
- abort 'Restore failed'
- end
- end
- end
-
- $progress.print 'Put GitLab hooks in repositories dirs'.yellow
- cmd = "#{Gitlab.config.gitlab_shell.path}/bin/create-hooks"
- if system(cmd)
- $progress.puts " [DONE]".green
- else
- puts " [FAILED]".red
- puts "failed: #{cmd}"
- end
-
- end
-
- protected
-
- def path_to_repo(project)
- project.repository.path_to_repo
- end
-
- def path_to_bundle(project)
- File.join(backup_repos_path, project.path_with_namespace + ".bundle")
- end
-
- def repos_path
- Gitlab.config.gitlab_shell.repos_path
- end
-
- def backup_repos_path
- File.join(Gitlab.config.backup.path, "repositories")
- end
-
- def prepare
- FileUtils.rm_rf(backup_repos_path)
- FileUtils.mkdir_p(backup_repos_path)
- end
-
- def silent
- {err: '/dev/null', out: '/dev/null'}
- end
- end
-end
diff --git a/lib/backup/uploads.rb b/lib/backup/uploads.rb
deleted file mode 100644
index e50e1ff4f13..00000000000
--- a/lib/backup/uploads.rb
+++ /dev/null
@@ -1,30 +0,0 @@
-module Backup
- class Uploads
- attr_reader :app_uploads_dir, :backup_uploads_dir, :backup_dir
-
- def initialize
- @app_uploads_dir = File.realpath(Rails.root.join('public', 'uploads'))
- @backup_dir = Gitlab.config.backup.path
- @backup_uploads_dir = File.join(Gitlab.config.backup.path, 'uploads')
- end
-
- # Copy uploads from public/uploads to backup/uploads
- def dump
- FileUtils.mkdir_p(backup_uploads_dir)
- FileUtils.cp_r(app_uploads_dir, backup_dir)
- end
-
- def restore
- backup_existing_uploads_dir
-
- FileUtils.cp_r(backup_uploads_dir, app_uploads_dir)
- end
-
- def backup_existing_uploads_dir
- timestamped_uploads_path = File.join(app_uploads_dir, '..', "uploads.#{Time.now.to_i}")
- if File.exists?(app_uploads_dir)
- FileUtils.mv(app_uploads_dir, timestamped_uploads_path)
- end
- end
- end
-end