Commit 4dae41d5dc90473e3c5c08064c25433fecf5abd3
1 parent
12b34c81
Exists in
master
and in
4 other branches
Changes done as per feedback
Showing
3 changed files
with
26 additions
and
23 deletions
Show diff stats
config/gitlab.yml.example
@@ -22,7 +22,7 @@ email: | @@ -22,7 +22,7 @@ email: | ||
22 | app: | 22 | app: |
23 | default_projects_limit: 10 | 23 | default_projects_limit: 10 |
24 | # backup_path: "/vol/backups" # default: Rails.root + backups/ | 24 | # backup_path: "/vol/backups" # default: Rails.root + backups/ |
25 | - # backup_keep_time: 7.days # default: 0 (forever) | 25 | + # backup_keep_time: 604800 # default: 0 (forever) (in seconds) |
26 | 26 | ||
27 | 27 | ||
28 | # | 28 | # |
config/initializers/1_settings.rb
@@ -101,5 +101,15 @@ class Settings < Settingslogic | @@ -101,5 +101,15 @@ class Settings < Settingslogic | ||
101 | def default_projects_limit | 101 | def default_projects_limit |
102 | app['default_projects_limit'] || 10 | 102 | app['default_projects_limit'] || 10 |
103 | end | 103 | end |
104 | + | ||
105 | + def backup_path | ||
106 | + t = app['backup_path'] || "backups/" | ||
107 | + t = /^\//.match(t) ? t : File.join(Rails.root + t) | ||
108 | + t | ||
109 | + end | ||
110 | + | ||
111 | + def backup_keep_time | ||
112 | + app['backup_keep_time'] || 0 | ||
113 | + end | ||
104 | end | 114 | end |
105 | end | 115 | end |
lib/tasks/gitlab/backup.rake
1 | require 'active_record/fixtures' | 1 | require 'active_record/fixtures' |
2 | 2 | ||
3 | -# load config | ||
4 | -GITLAB_OPTS = YAML.load_file("#{Rails.root}/config/gitlab.yml")['app'] | ||
5 | - | ||
6 | -# backup path options | ||
7 | -backup_path = GITLAB_OPTS['backup_path'].nil? ? "backups/" : GITLAB_OPTS['backup_path'] # set default path if not specified in settings. | ||
8 | -backup_path = /^\//.match(backup_path) ? backup_path : File.join(Rails.root + backup_path) # check for absolut or relative path. | ||
9 | -FileUtils.mkdir_p(backup_path) until Dir.exists?(backup_path) | ||
10 | - | ||
11 | -# keep last n backups | ||
12 | -backup_keep_time = GITLAB_OPTS['backup_keep_time'].nil? ? 0 : eval(GITLAB_OPTS['backup_keep_time']) | ||
13 | - | ||
14 | -backup_path_repo = File.join(backup_path, "repositories") | ||
15 | -backup_path_db = File.join(backup_path, "db") | ||
16 | - | ||
17 | namespace :gitlab do | 3 | namespace :gitlab do |
18 | namespace :app do | 4 | namespace :app do |
19 | 5 | ||
@@ -21,11 +7,11 @@ namespace :gitlab do | @@ -21,11 +7,11 @@ namespace :gitlab do | ||
21 | desc "GITLAB | Create a backup of the gitlab system" | 7 | desc "GITLAB | Create a backup of the gitlab system" |
22 | task :backup_create => :environment do | 8 | task :backup_create => :environment do |
23 | 9 | ||
24 | - Dir.chdir(backup_path) | ||
25 | - | ||
26 | Rake::Task["gitlab:app:db_dump"].invoke | 10 | Rake::Task["gitlab:app:db_dump"].invoke |
27 | Rake::Task["gitlab:app:repo_dump"].invoke | 11 | Rake::Task["gitlab:app:repo_dump"].invoke |
28 | 12 | ||
13 | + Dir.chdir(Gitlab.config.backup_path) | ||
14 | + | ||
29 | # saving additional informations | 15 | # saving additional informations |
30 | s = Hash.new | 16 | s = Hash.new |
31 | s["db_version"] = "#{ActiveRecord::Migrator.current_version}" | 17 | s["db_version"] = "#{ActiveRecord::Migrator.current_version}" |
@@ -33,7 +19,7 @@ namespace :gitlab do | @@ -33,7 +19,7 @@ namespace :gitlab do | ||
33 | s["gitlab_version"] = %x{git rev-parse HEAD}.gsub(/\n/,"") | 19 | s["gitlab_version"] = %x{git rev-parse HEAD}.gsub(/\n/,"") |
34 | s["tar_version"] = %x{tar --version | head -1}.gsub(/\n/,"") | 20 | s["tar_version"] = %x{tar --version | head -1}.gsub(/\n/,"") |
35 | 21 | ||
36 | - File.open("#{backup_path}/backup_information.yml", "w+") do |file| | 22 | + File.open("#{Gitlab.config.backup_path}/backup_information.yml", "w+") do |file| |
37 | file << s.to_yaml.gsub(/^---\n/,'') | 23 | file << s.to_yaml.gsub(/^---\n/,'') |
38 | end | 24 | end |
39 | 25 | ||
@@ -55,10 +41,10 @@ namespace :gitlab do | @@ -55,10 +41,10 @@ namespace :gitlab do | ||
55 | 41 | ||
56 | # delete backups | 42 | # delete backups |
57 | print "Deleting old backups... " | 43 | print "Deleting old backups... " |
58 | - if backup_keep_time > 0 | 44 | + if Gitlab.config.backup_keep_time > 0 |
59 | 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 } |
60 | file_list.sort.each do |timestamp| | 46 | file_list.sort.each do |timestamp| |
61 | - if Time.at(timestamp) < (Time.now - backup_keep_time) | 47 | + if Time.at(timestamp) < (Time.now - Gitlab.config.backup_keep_time) |
62 | %x{rm #{timestamp}_gitlab_backup.tar} | 48 | %x{rm #{timestamp}_gitlab_backup.tar} |
63 | end | 49 | end |
64 | end | 50 | end |
@@ -70,11 +56,13 @@ namespace :gitlab do | @@ -70,11 +56,13 @@ namespace :gitlab do | ||
70 | end | 56 | end |
71 | 57 | ||
72 | 58 | ||
59 | + # Restore backup of gitlab system | ||
73 | desc "GITLAB | Restore a previously created backup" | 60 | desc "GITLAB | Restore a previously created backup" |
74 | task :backup_restore => :environment do | 61 | task :backup_restore => :environment do |
75 | 62 | ||
76 | - Dir.chdir(backup_path) | 63 | + Dir.chdir(Gitlab.config.backup_path) |
77 | 64 | ||
65 | + # check for existing backups in the backup dir | ||
78 | file_list = Dir.glob("*_gitlab_backup.tar").each.map { |f| f.split(/_/).first.to_i } | 66 | file_list = Dir.glob("*_gitlab_backup.tar").each.map { |f| f.split(/_/).first.to_i } |
79 | puts "no backup found" if file_list.count == 0 | 67 | puts "no backup found" if file_list.count == 0 |
80 | if file_list.count > 1 && ENV["BACKUP"].nil? | 68 | if file_list.count > 1 && ENV["BACKUP"].nil? |
@@ -85,12 +73,13 @@ namespace :gitlab do | @@ -85,12 +73,13 @@ namespace :gitlab do | ||
85 | 73 | ||
86 | tar_file = ENV["BACKUP"].nil? ? File.join(file_list.first.to_s + "_gitlab_backup.tar") : File.join(ENV["BACKUP"] + "_gitlab_backup.tar") | 74 | tar_file = ENV["BACKUP"].nil? ? File.join(file_list.first.to_s + "_gitlab_backup.tar") : File.join(ENV["BACKUP"] + "_gitlab_backup.tar") |
87 | 75 | ||
88 | - if ! File.exists?(tar_file) | 76 | + unless File.exists?(tar_file) |
89 | puts "The specified backup doesn't exist!" | 77 | puts "The specified backup doesn't exist!" |
78 | + exit 1; | ||
90 | end | 79 | end |
91 | 80 | ||
92 | print "Unpacking backup... " | 81 | print "Unpacking backup... " |
93 | - if ! Kernel.system("tar -xf #{tar_file}") | 82 | + unless Kernel.system("tar -xf #{tar_file}") |
94 | puts "[FAILED]".red | 83 | puts "[FAILED]".red |
95 | exit 1 | 84 | exit 1 |
96 | else | 85 | else |
@@ -129,6 +118,7 @@ namespace :gitlab do | @@ -129,6 +118,7 @@ namespace :gitlab do | ||
129 | ################################# REPOSITORIES ################################# | 118 | ################################# REPOSITORIES ################################# |
130 | 119 | ||
131 | task :repo_dump => :environment do | 120 | task :repo_dump => :environment do |
121 | + backup_path_repo = File.join(Gitlab.config.backup_path, "repositories") | ||
132 | FileUtils.mkdir_p(backup_path_repo) until Dir.exists?(backup_path_repo) | 122 | FileUtils.mkdir_p(backup_path_repo) until Dir.exists?(backup_path_repo) |
133 | puts "Dumping repositories:" | 123 | puts "Dumping repositories:" |
134 | project = Project.all.map { |n| [n.name,n.path_to_repo] } | 124 | project = Project.all.map { |n| [n.name,n.path_to_repo] } |
@@ -144,6 +134,7 @@ namespace :gitlab do | @@ -144,6 +134,7 @@ namespace :gitlab do | ||
144 | end | 134 | end |
145 | 135 | ||
146 | task :repo_restore => :environment do | 136 | task :repo_restore => :environment do |
137 | + backup_path_repo = File.join(Gitlab.config.backup_path, "repositories") | ||
147 | puts "Restoring repositories:" | 138 | puts "Restoring repositories:" |
148 | project = Project.all.map { |n| [n.name,n.path_to_repo] } | 139 | project = Project.all.map { |n| [n.name,n.path_to_repo] } |
149 | project << ["gitolite-admin.git", File.join(File.dirname(project.first.second), "gitolite-admin.git")] | 140 | project << ["gitolite-admin.git", File.join(File.dirname(project.first.second), "gitolite-admin.git")] |
@@ -161,6 +152,7 @@ namespace :gitlab do | @@ -161,6 +152,7 @@ namespace :gitlab do | ||
161 | ###################################### DB ###################################### | 152 | ###################################### DB ###################################### |
162 | 153 | ||
163 | task :db_dump => :environment do | 154 | task :db_dump => :environment do |
155 | + backup_path_db = File.join(Gitlab.config.backup_path, "db") | ||
164 | FileUtils.mkdir_p(backup_path_db) until Dir.exists?(backup_path_db) | 156 | FileUtils.mkdir_p(backup_path_db) until Dir.exists?(backup_path_db) |
165 | puts "Dumping database tables:" | 157 | puts "Dumping database tables:" |
166 | ActiveRecord::Base.connection.tables.each do |tbl| | 158 | ActiveRecord::Base.connection.tables.each do |tbl| |
@@ -179,6 +171,7 @@ namespace :gitlab do | @@ -179,6 +171,7 @@ namespace :gitlab do | ||
179 | end | 171 | end |
180 | 172 | ||
181 | task :db_restore=> :environment do | 173 | task :db_restore=> :environment do |
174 | + backup_path_db = File.join(Gitlab.config.backup_path, "db") | ||
182 | puts "Restoring database tables:" | 175 | puts "Restoring database tables:" |
183 | Rake::Task["db:reset"].invoke | 176 | Rake::Task["db:reset"].invoke |
184 | Dir.glob(File.join(backup_path_db, "*.yml") ).each do |dir| | 177 | Dir.glob(File.join(backup_path_db, "*.yml") ).each do |dir| |