Commit 1b6c28b9766aff2075bcd6e8c394ac4b9ed66f96
1 parent
430d3ad4
Exists in
master
and in
4 other branches
Update output of gitlab:backup:resore
Showing
1 changed file
with
39 additions
and
35 deletions
Show diff stats
lib/tasks/gitlab/backup.rake
@@ -5,6 +5,8 @@ namespace :gitlab do | @@ -5,6 +5,8 @@ namespace :gitlab do | ||
5 | # Create backup of GitLab system | 5 | # Create backup of GitLab system |
6 | desc "GITLAB | Create a backup of the GitLab system" | 6 | desc "GITLAB | Create a backup of the GitLab system" |
7 | task :create => :environment do | 7 | task :create => :environment do |
8 | + warn_user_is_not_gitlab | ||
9 | + | ||
8 | Rake::Task["gitlab:backup:db:create"].invoke | 10 | Rake::Task["gitlab:backup:db:create"].invoke |
9 | Rake::Task["gitlab:backup:repo:create"].invoke | 11 | Rake::Task["gitlab:backup:repo:create"].invoke |
10 | 12 | ||
@@ -22,23 +24,23 @@ namespace :gitlab do | @@ -22,23 +24,23 @@ namespace :gitlab do | ||
22 | end | 24 | end |
23 | 25 | ||
24 | # create archive | 26 | # create archive |
25 | - print "Creating backup archive: #{Time.now.to_i}_gitlab_backup.tar " | 27 | + print "Creating backup archive: #{Time.now.to_i}_gitlab_backup.tar ... " |
26 | if Kernel.system("tar -cf #{Time.now.to_i}_gitlab_backup.tar repositories/ db/ backup_information.yml") | 28 | if Kernel.system("tar -cf #{Time.now.to_i}_gitlab_backup.tar repositories/ db/ backup_information.yml") |
27 | - puts "[DONE]".green | 29 | + puts "done".green |
28 | else | 30 | else |
29 | - puts "[FAILED]".red | 31 | + puts "failed".red |
30 | end | 32 | end |
31 | 33 | ||
32 | # cleanup: remove tmp files | 34 | # cleanup: remove tmp files |
33 | - print "Deleting tmp directories..." | 35 | + print "Deleting tmp directories ... " |
34 | if Kernel.system("rm -rf repositories/ db/ backup_information.yml") | 36 | if Kernel.system("rm -rf repositories/ db/ backup_information.yml") |
35 | - puts "[DONE]".green | 37 | + puts "done".green |
36 | else | 38 | else |
37 | - puts "[FAILED]".red | 39 | + puts "failed".red |
38 | end | 40 | end |
39 | 41 | ||
40 | # delete backups | 42 | # delete backups |
41 | - print "Deleting old backups... " | 43 | + print "Deleting old backups ... " |
42 | if Gitlab.config.backup.keep_time > 0 | 44 | if Gitlab.config.backup.keep_time > 0 |
43 | file_list = Dir.glob("*_gitlab_backup.tar").map { |f| f.split(/_/).first.to_i } | 45 | file_list = Dir.glob("*_gitlab_backup.tar").map { |f| f.split(/_/).first.to_i } |
44 | file_list.sort.each do |timestamp| | 46 | file_list.sort.each do |timestamp| |
@@ -46,15 +48,17 @@ namespace :gitlab do | @@ -46,15 +48,17 @@ namespace :gitlab do | ||
46 | %x{rm #{timestamp}_gitlab_backup.tar} | 48 | %x{rm #{timestamp}_gitlab_backup.tar} |
47 | end | 49 | end |
48 | end | 50 | end |
49 | - puts "[DONE]".green | 51 | + puts "done".green |
50 | else | 52 | else |
51 | - puts "[SKIPPING]".yellow | 53 | + puts "skipping".yellow |
52 | end | 54 | end |
53 | end | 55 | end |
54 | 56 | ||
55 | # Restore backup of GitLab system | 57 | # Restore backup of GitLab system |
56 | desc "GITLAB | Restore a previously created backup" | 58 | desc "GITLAB | Restore a previously created backup" |
57 | task :restore => :environment do | 59 | task :restore => :environment do |
60 | + warn_user_is_not_gitlab | ||
61 | + | ||
58 | Dir.chdir(Gitlab.config.backup.path) | 62 | Dir.chdir(Gitlab.config.backup.path) |
59 | 63 | ||
60 | # check for existing backups in the backup dir | 64 | # check for existing backups in the backup dir |
@@ -63,22 +67,22 @@ namespace :gitlab do | @@ -63,22 +67,22 @@ namespace :gitlab do | ||
63 | if file_list.count > 1 && ENV["BACKUP"].nil? | 67 | if file_list.count > 1 && ENV["BACKUP"].nil? |
64 | puts "Found more than one backup, please specify which one you want to restore:" | 68 | puts "Found more than one backup, please specify which one you want to restore:" |
65 | puts "rake gitlab:backup:restore BACKUP=timestamp_of_backup" | 69 | puts "rake gitlab:backup:restore BACKUP=timestamp_of_backup" |
66 | - exit 1; | 70 | + exit 1 |
67 | end | 71 | end |
68 | 72 | ||
69 | tar_file = ENV["BACKUP"].nil? ? File.join("#{file_list.first}_gitlab_backup.tar") : File.join(ENV["BACKUP"] + "_gitlab_backup.tar") | 73 | tar_file = ENV["BACKUP"].nil? ? File.join("#{file_list.first}_gitlab_backup.tar") : File.join(ENV["BACKUP"] + "_gitlab_backup.tar") |
70 | 74 | ||
71 | unless File.exists?(tar_file) | 75 | unless File.exists?(tar_file) |
72 | puts "The specified backup doesn't exist!" | 76 | puts "The specified backup doesn't exist!" |
73 | - exit 1; | 77 | + exit 1 |
74 | end | 78 | end |
75 | 79 | ||
76 | - print "Unpacking backup... " | 80 | + print "Unpacking backup ... " |
77 | unless Kernel.system("tar -xf #{tar_file}") | 81 | unless Kernel.system("tar -xf #{tar_file}") |
78 | - puts "[FAILED]".red | 82 | + puts "failed".red |
79 | exit 1 | 83 | exit 1 |
80 | else | 84 | else |
81 | - puts "[DONE]".green | 85 | + puts "done".green |
82 | end | 86 | end |
83 | 87 | ||
84 | settings = YAML.load_file("backup_information.yml") | 88 | settings = YAML.load_file("backup_information.yml") |
@@ -86,7 +90,7 @@ namespace :gitlab do | @@ -86,7 +90,7 @@ namespace :gitlab do | ||
86 | 90 | ||
87 | # restoring mismatching backups can lead to unexpected problems | 91 | # restoring mismatching backups can lead to unexpected problems |
88 | if settings[:gitlab_version] != %x{git rev-parse HEAD}.gsub(/\n/,"") | 92 | if settings[:gitlab_version] != %x{git rev-parse HEAD}.gsub(/\n/,"") |
89 | - puts "gitlab_version mismatch:".red | 93 | + puts "GitLab version mismatch:".red |
90 | puts " Your current HEAD differs from the HEAD in the backup!".red | 94 | puts " Your current HEAD differs from the HEAD in the backup!".red |
91 | puts " Please switch to the following revision and try again:".red | 95 | puts " Please switch to the following revision and try again:".red |
92 | puts " revision: #{settings[:gitlab_version]}".red | 96 | puts " revision: #{settings[:gitlab_version]}".red |
@@ -97,11 +101,11 @@ namespace :gitlab do | @@ -97,11 +101,11 @@ namespace :gitlab do | ||
97 | Rake::Task["gitlab:backup:repo:restore"].invoke | 101 | Rake::Task["gitlab:backup:repo:restore"].invoke |
98 | 102 | ||
99 | # cleanup: remove tmp files | 103 | # cleanup: remove tmp files |
100 | - print "Deleting tmp directories..." | 104 | + print "Deleting tmp directories ... " |
101 | if Kernel.system("rm -rf repositories/ db/ backup_information.yml") | 105 | if Kernel.system("rm -rf repositories/ db/ backup_information.yml") |
102 | - puts "[DONE]".green | 106 | + puts "done".green |
103 | else | 107 | else |
104 | - puts "[FAILED]".red | 108 | + puts "failed".red |
105 | end | 109 | end |
106 | end | 110 | end |
107 | 111 | ||
@@ -114,26 +118,26 @@ namespace :gitlab do | @@ -114,26 +118,26 @@ namespace :gitlab do | ||
114 | task :create => :environment do | 118 | task :create => :environment do |
115 | backup_path_repo = File.join(Gitlab.config.backup.path, "repositories") | 119 | backup_path_repo = File.join(Gitlab.config.backup.path, "repositories") |
116 | FileUtils.mkdir_p(backup_path_repo) until Dir.exists?(backup_path_repo) | 120 | FileUtils.mkdir_p(backup_path_repo) until Dir.exists?(backup_path_repo) |
117 | - puts "Dumping repositories:" | 121 | + puts "Dumping repositories ..." |
118 | project = Project.all.map { |n| [n.path, n.path_to_repo] } | 122 | project = Project.all.map { |n| [n.path, n.path_to_repo] } |
119 | project << ["gitolite-admin.git", File.join(Gitlab.config.git_base_path, "gitolite-admin.git")] | 123 | project << ["gitolite-admin.git", File.join(Gitlab.config.git_base_path, "gitolite-admin.git")] |
120 | project.each do |project| | 124 | project.each do |project| |
121 | - print "- Dumping repository #{project.first}... " | 125 | + print "#{project.first.yellow} ... " |
122 | if Kernel.system("cd #{project.second} > /dev/null 2>&1 && git bundle create #{backup_path_repo}/#{project.first}.bundle --all > /dev/null 2>&1") | 126 | if Kernel.system("cd #{project.second} > /dev/null 2>&1 && git bundle create #{backup_path_repo}/#{project.first}.bundle --all > /dev/null 2>&1") |
123 | - puts "[DONE]".green | 127 | + puts "done".green |
124 | else | 128 | else |
125 | - puts "[FAILED]".red | 129 | + puts "failed".red |
126 | end | 130 | end |
127 | end | 131 | end |
128 | end | 132 | end |
129 | 133 | ||
130 | task :restore => :environment do | 134 | task :restore => :environment do |
131 | backup_path_repo = File.join(Gitlab.config.backup.path, "repositories") | 135 | backup_path_repo = File.join(Gitlab.config.backup.path, "repositories") |
132 | - puts "Restoring repositories:" | 136 | + puts "Restoring repositories ... " |
133 | project = Project.all.map { |n| [n.path, n.path_to_repo] } | 137 | project = Project.all.map { |n| [n.path, n.path_to_repo] } |
134 | - project << ["gitolite-admin.git", File.join(File.dirname(project.first.second), "gitolite-admin.git")] | 138 | + project << ["gitolite-admin.git", File.join(Gitlab.config.git_base_path, "gitolite-admin.git")] |
135 | project.each do |project| | 139 | project.each do |project| |
136 | - print "- Restoring repository #{project.first}... " | 140 | + print "#{project.first.yellow} ... " |
137 | FileUtils.rm_rf(project.second) if File.dirname(project.second) # delete old stuff | 141 | FileUtils.rm_rf(project.second) if File.dirname(project.second) # delete old stuff |
138 | 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") | 142 | 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") |
139 | permission_commands = [ | 143 | permission_commands = [ |
@@ -141,9 +145,9 @@ namespace :gitlab do | @@ -141,9 +145,9 @@ namespace :gitlab do | ||
141 | "sudo chown -R #{Gitlab.config.ssh_user}:#{Gitlab.config.ssh_user} #{Gitlab.config.git_base_path}" | 145 | "sudo chown -R #{Gitlab.config.ssh_user}:#{Gitlab.config.ssh_user} #{Gitlab.config.git_base_path}" |
142 | ] | 146 | ] |
143 | permission_commands.each { |command| Kernel.system(command) } | 147 | permission_commands.each { |command| Kernel.system(command) } |
144 | - puts "[DONE]".green | 148 | + puts "done".green |
145 | else | 149 | else |
146 | - puts "[FAILED]".red | 150 | + puts "failed".red |
147 | end | 151 | end |
148 | end | 152 | end |
149 | end | 153 | end |
@@ -156,9 +160,9 @@ namespace :gitlab do | @@ -156,9 +160,9 @@ namespace :gitlab do | ||
156 | backup_path_db = File.join(Gitlab.config.backup.path, "db") | 160 | backup_path_db = File.join(Gitlab.config.backup.path, "db") |
157 | FileUtils.mkdir_p(backup_path_db) unless Dir.exists?(backup_path_db) | 161 | FileUtils.mkdir_p(backup_path_db) unless Dir.exists?(backup_path_db) |
158 | 162 | ||
159 | - puts "Dumping database tables:" | 163 | + puts "Dumping database tables ... " |
160 | ActiveRecord::Base.connection.tables.each do |tbl| | 164 | ActiveRecord::Base.connection.tables.each do |tbl| |
161 | - print "- Dumping table #{tbl}... " | 165 | + print "#{tbl.yellow} ... " |
162 | count = 1 | 166 | count = 1 |
163 | File.open(File.join(backup_path_db, tbl + ".yml"), "w+") do |file| | 167 | File.open(File.join(backup_path_db, tbl + ".yml"), "w+") do |file| |
164 | ActiveRecord::Base.connection.select_all("SELECT * FROM `#{tbl}`").each do |line| | 168 | ActiveRecord::Base.connection.select_all("SELECT * FROM `#{tbl}`").each do |line| |
@@ -167,25 +171,25 @@ namespace :gitlab do | @@ -167,25 +171,25 @@ namespace :gitlab do | ||
167 | file << output.to_yaml.gsub(/^---\n/,'') + "\n" | 171 | file << output.to_yaml.gsub(/^---\n/,'') + "\n" |
168 | count += 1 | 172 | count += 1 |
169 | end | 173 | end |
170 | - puts "[DONE]".green | 174 | + puts "done".green |
171 | end | 175 | end |
172 | end | 176 | end |
173 | end | 177 | end |
174 | 178 | ||
175 | - task :restore=> :environment do | 179 | + task :restore => :environment do |
176 | backup_path_db = File.join(Gitlab.config.backup.path, "db") | 180 | backup_path_db = File.join(Gitlab.config.backup.path, "db") |
177 | 181 | ||
178 | - puts "Restoring database tables:" | 182 | + puts "Restoring database tables (loading fixtures) ... " |
179 | Rake::Task["db:reset"].invoke | 183 | Rake::Task["db:reset"].invoke |
180 | 184 | ||
181 | Dir.glob(File.join(backup_path_db, "*.yml") ).each do |dir| | 185 | Dir.glob(File.join(backup_path_db, "*.yml") ).each do |dir| |
182 | fixture_file = File.basename(dir, ".*" ) | 186 | fixture_file = File.basename(dir, ".*" ) |
183 | - print "- Loading fixture #{fixture_file}..." | 187 | + print "#{fixture_file.yellow} ... " |
184 | if File.size(dir) > 0 | 188 | if File.size(dir) > 0 |
185 | ActiveRecord::Fixtures.create_fixtures(backup_path_db, fixture_file) | 189 | ActiveRecord::Fixtures.create_fixtures(backup_path_db, fixture_file) |
186 | - puts "[DONE]".green | 190 | + puts "done".green |
187 | else | 191 | else |
188 | - puts "[SKIPPING]".yellow | 192 | + puts "skipping".yellow |
189 | end | 193 | end |
190 | end | 194 | end |
191 | end | 195 | end |