Commit c9bf2bb2 authored by Riyad Preukschas's avatar Riyad Preukschas

Rename gitlab:app:backup_* to gitlab:backup:*

Rename gitlab:app:db_* to gitlab:backup:db:*
Rename gitlab:app:repo_* to gitlab:backup:repo:*
Rename *_dump to *_create
parent 56f9a674
...@@ -4,7 +4,7 @@ Creates a backup archive of the database and all repositories. This archive will ...@@ -4,7 +4,7 @@ Creates a backup archive of the database and all repositories. This archive will
The filename will be `[TIMESTAMP]_gitlab_backup.tar`. This timestamp can be used to restore an specific backup. The filename will be `[TIMESTAMP]_gitlab_backup.tar`. This timestamp can be used to restore an specific backup.
``` ```
bundle exec rake gitlab:app:backup_create bundle exec rake gitlab:backup:create
``` ```
Example output: Example output:
...@@ -40,7 +40,7 @@ Deleting old backups... [SKIPPING] ...@@ -40,7 +40,7 @@ Deleting old backups... [SKIPPING]
### Restore a previously created backup ### Restore a previously created backup
``` ```
bundle exec rake gitlab:app:backup_restore bundle exec rake gitlab:backup:restore
``` ```
Options: Options:
......
require 'active_record/fixtures' require 'active_record/fixtures'
namespace :gitlab do namespace :gitlab do
namespace :app do namespace :backup do
# Create backup of GitLab system # Create backup of GitLab system
desc "GITLAB | Create a backup of the GitLab system" desc "GITLAB | Create a backup of the GitLab system"
task :backup_create => :environment do task :create => :environment do
Rake::Task["gitlab:app:db_dump"].invoke Rake::Task["gitlab:backup:db:create"].invoke
Rake::Task["gitlab:app:repo_dump"].invoke Rake::Task["gitlab:backup:repo:create"].invoke
Dir.chdir(Gitlab.config.backup.path) Dir.chdir(Gitlab.config.backup.path)
...@@ -54,7 +54,7 @@ namespace :gitlab do ...@@ -54,7 +54,7 @@ namespace :gitlab do
# Restore backup of GitLab system # Restore backup of GitLab system
desc "GITLAB | Restore a previously created backup" desc "GITLAB | Restore a previously created backup"
task :backup_restore => :environment do task :restore => :environment do
Dir.chdir(Gitlab.config.backup.path) Dir.chdir(Gitlab.config.backup.path)
# check for existing backups in the backup dir # check for existing backups in the backup dir
...@@ -62,7 +62,7 @@ namespace :gitlab do ...@@ -62,7 +62,7 @@ namespace :gitlab do
puts "no backups found" if file_list.count == 0 puts "no backups found" if file_list.count == 0
if file_list.count > 1 && ENV["BACKUP"].nil? if file_list.count > 1 && ENV["BACKUP"].nil?
puts "Found more than one backup, please specify which one you want to restore:" puts "Found more than one backup, please specify which one you want to restore:"
puts "rake gitlab:app:backup_restore BACKUP=timestamp_of_backup" puts "rake gitlab:backup:restore BACKUP=timestamp_of_backup"
exit 1; exit 1;
end end
...@@ -93,8 +93,8 @@ namespace :gitlab do ...@@ -93,8 +93,8 @@ namespace :gitlab do
exit 1 exit 1
end end
Rake::Task["gitlab:app:db_restore"].invoke Rake::Task["gitlab:backup:db:restore"].invoke
Rake::Task["gitlab:app:repo_restore"].invoke Rake::Task["gitlab:backup:repo:restore"].invoke
# cleanup: remove tmp files # cleanup: remove tmp files
print "Deleting tmp directories..." print "Deleting tmp directories..."
...@@ -110,82 +110,86 @@ namespace :gitlab do ...@@ -110,82 +110,86 @@ namespace :gitlab do
################################# REPOSITORIES ################################# ################################# REPOSITORIES #################################
task :repo_dump => :environment do namespace :repo do
backup_path_repo = File.join(Gitlab.config.backup.path, "repositories") task :create => :environment do
FileUtils.mkdir_p(backup_path_repo) until Dir.exists?(backup_path_repo) backup_path_repo = File.join(Gitlab.config.backup.path, "repositories")
puts "Dumping repositories:" FileUtils.mkdir_p(backup_path_repo) until Dir.exists?(backup_path_repo)
project = Project.all.map { |n| [n.path, n.path_to_repo] } puts "Dumping repositories:"
project << ["gitolite-admin.git", File.join(File.dirname(project.first.second), "gitolite-admin.git")] project = Project.all.map { |n| [n.path, n.path_to_repo] }
project.each do |project| project << ["gitolite-admin.git", File.join(File.dirname(project.first.second), "gitolite-admin.git")]
print "- Dumping repository #{project.first}... " project.each do |project|
if Kernel.system("cd #{project.second} > /dev/null 2>&1 && git bundle create #{backup_path_repo}/#{project.first}.bundle --all > /dev/null 2>&1") print "- Dumping repository #{project.first}... "
puts "[DONE]".green if Kernel.system("cd #{project.second} > /dev/null 2>&1 && git bundle create #{backup_path_repo}/#{project.first}.bundle --all > /dev/null 2>&1")
else puts "[DONE]".green
puts "[FAILED]".red else
puts "[FAILED]".red
end
end end
end end
end
task :repo_restore => :environment do task :restore => :environment do
backup_path_repo = File.join(Gitlab.config.backup.path, "repositories") backup_path_repo = File.join(Gitlab.config.backup.path, "repositories")
puts "Restoring repositories:" puts "Restoring repositories:"
project = Project.all.map { |n| [n.path, n.path_to_repo] } project = Project.all.map { |n| [n.path, n.path_to_repo] }
project << ["gitolite-admin.git", File.join(File.dirname(project.first.second), "gitolite-admin.git")] project << ["gitolite-admin.git", File.join(File.dirname(project.first.second), "gitolite-admin.git")]
project.each do |project| project.each do |project|
print "- Restoring repository #{project.first}... " print "- Restoring repository #{project.first}... "
FileUtils.rm_rf(project.second) if File.dirname(project.second) # delete old stuff FileUtils.rm_rf(project.second) if File.dirname(project.second) # delete old stuff
if Kernel.system("cd #{File.dirname(project.second)} > /dev/null 2>&1 && git clone --bare #{backup_path_repo}/#{project.first}.bundle #{project.first}.git > /dev/null 2>&1") if Kernel.system("cd #{File.dirname(project.second)} > /dev/null 2>&1 && git clone --bare #{backup_path_repo}/#{project.first}.bundle #{project.first}.git > /dev/null 2>&1")
permission_commands = [ permission_commands = [
"sudo chmod -R g+rwX #{Gitlab.config.gitolite.repos_path}", "sudo chmod -R g+rwX #{Gitlab.config.git_base_path}",
"sudo chown -R #{Gitlab.config.gitolite.ssh_user}:#{Gitlab.config.gitolite.ssh_user} #{Gitlab.config.gitolite.repos_path}" "sudo chown -R #{Gitlab.config.ssh_user}:#{Gitlab.config.ssh_user} #{Gitlab.config.git_base_path}"
] ]
permission_commands.each { |command| Kernel.system(command) } permission_commands.each { |command| Kernel.system(command) }
puts "[DONE]".green puts "[DONE]".green
else else
puts "[FAILED]".red puts "[FAILED]".red
end
end end
end end
end end
###################################### DB ###################################### ###################################### DB ######################################
task :db_dump => :environment do namespace :db do
backup_path_db = File.join(Gitlab.config.backup.path, "db") task :create => :environment do
FileUtils.mkdir_p(backup_path_db) unless Dir.exists?(backup_path_db) backup_path_db = File.join(Gitlab.config.backup.path, "db")
FileUtils.mkdir_p(backup_path_db) unless Dir.exists?(backup_path_db)
puts "Dumping database tables:"
ActiveRecord::Base.connection.tables.each do |tbl| puts "Dumping database tables:"
print "- Dumping table #{tbl}... " ActiveRecord::Base.connection.tables.each do |tbl|
count = 1 print "- Dumping table #{tbl}... "
File.open(File.join(backup_path_db, tbl + ".yml"), "w+") do |file| count = 1
ActiveRecord::Base.connection.select_all("SELECT * FROM `#{tbl}`").each do |line| File.open(File.join(backup_path_db, tbl + ".yml"), "w+") do |file|
line.delete_if{|k,v| v.blank?} ActiveRecord::Base.connection.select_all("SELECT * FROM `#{tbl}`").each do |line|
output = {tbl + '_' + count.to_s => line} line.delete_if{|k,v| v.blank?}
file << output.to_yaml.gsub(/^---\n/,'') + "\n" output = {tbl + '_' + count.to_s => line}
count += 1 file << output.to_yaml.gsub(/^---\n/,'') + "\n"
count += 1
end
puts "[DONE]".green
end end
puts "[DONE]".green
end end
end end
end
task :db_restore=> :environment do task :restore=> :environment do
backup_path_db = File.join(Gitlab.config.backup.path, "db") backup_path_db = File.join(Gitlab.config.backup.path, "db")
puts "Restoring database tables:" puts "Restoring database tables:"
Rake::Task["db:reset"].invoke Rake::Task["db:reset"].invoke
Dir.glob(File.join(backup_path_db, "*.yml") ).each do |dir| Dir.glob(File.join(backup_path_db, "*.yml") ).each do |dir|
fixture_file = File.basename(dir, ".*" ) fixture_file = File.basename(dir, ".*" )
print "- Loading fixture #{fixture_file}..." print "- Loading fixture #{fixture_file}..."
if File.size(dir) > 0 if File.size(dir) > 0
ActiveRecord::Fixtures.create_fixtures(backup_path_db, fixture_file) ActiveRecord::Fixtures.create_fixtures(backup_path_db, fixture_file)
puts "[DONE]".green puts "[DONE]".green
else else
puts "[SKIPPING]".yellow puts "[SKIPPING]".yellow
end
end end
end end
end end
end # namespace end: app end # namespace end: backup
end # namespace end: gitlab end # namespace end: gitlab
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment