Commit 4dae41d5dc90473e3c5c08064c25433fecf5abd3

Authored by Stefan Morgenthaler
1 parent 12b34c81

Changes done as per feedback

config/gitlab.yml.example
... ... @@ -22,7 +22,7 @@ email:
22 22 app:
23 23 default_projects_limit: 10
24 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 101 def default_projects_limit
102 102 app['default_projects_limit'] || 10
103 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 114 end
105 115 end
... ...
lib/tasks/gitlab/backup.rake
1 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 3 namespace :gitlab do
18 4 namespace :app do
19 5  
... ... @@ -21,11 +7,11 @@ namespace :gitlab do
21 7 desc "GITLAB | Create a backup of the gitlab system"
22 8 task :backup_create => :environment do
23 9  
24   - Dir.chdir(backup_path)
25   -
26 10 Rake::Task["gitlab:app:db_dump"].invoke
27 11 Rake::Task["gitlab:app:repo_dump"].invoke
28 12  
  13 + Dir.chdir(Gitlab.config.backup_path)
  14 +
29 15 # saving additional informations
30 16 s = Hash.new
31 17 s["db_version"] = "#{ActiveRecord::Migrator.current_version}"
... ... @@ -33,7 +19,7 @@ namespace :gitlab do
33 19 s["gitlab_version"] = %x{git rev-parse HEAD}.gsub(/\n/,"")
34 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 23 file << s.to_yaml.gsub(/^---\n/,'')
38 24 end
39 25  
... ... @@ -55,10 +41,10 @@ namespace :gitlab do
55 41  
56 42 # delete backups
57 43 print "Deleting old backups... "
58   - if backup_keep_time > 0
  44 + if Gitlab.config.backup_keep_time > 0
59 45 file_list = Dir.glob("*_gitlab_backup.tar").map { |f| f.split(/_/).first.to_i }
60 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 48 %x{rm #{timestamp}_gitlab_backup.tar}
63 49 end
64 50 end
... ... @@ -70,11 +56,13 @@ namespace :gitlab do
70 56 end
71 57  
72 58  
  59 + # Restore backup of gitlab system
73 60 desc "GITLAB | Restore a previously created backup"
74 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 66 file_list = Dir.glob("*_gitlab_backup.tar").each.map { |f| f.split(/_/).first.to_i }
79 67 puts "no backup found" if file_list.count == 0
80 68 if file_list.count > 1 && ENV["BACKUP"].nil?
... ... @@ -85,12 +73,13 @@ namespace :gitlab do
85 73  
86 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 77 puts "The specified backup doesn't exist!"
  78 + exit 1;
90 79 end
91 80  
92 81 print "Unpacking backup... "
93   - if ! Kernel.system("tar -xf #{tar_file}")
  82 + unless Kernel.system("tar -xf #{tar_file}")
94 83 puts "[FAILED]".red
95 84 exit 1
96 85 else
... ... @@ -129,6 +118,7 @@ namespace :gitlab do
129 118 ################################# REPOSITORIES #################################
130 119  
131 120 task :repo_dump => :environment do
  121 + backup_path_repo = File.join(Gitlab.config.backup_path, "repositories")
132 122 FileUtils.mkdir_p(backup_path_repo) until Dir.exists?(backup_path_repo)
133 123 puts "Dumping repositories:"
134 124 project = Project.all.map { |n| [n.name,n.path_to_repo] }
... ... @@ -144,6 +134,7 @@ namespace :gitlab do
144 134 end
145 135  
146 136 task :repo_restore => :environment do
  137 + backup_path_repo = File.join(Gitlab.config.backup_path, "repositories")
147 138 puts "Restoring repositories:"
148 139 project = Project.all.map { |n| [n.name,n.path_to_repo] }
149 140 project << ["gitolite-admin.git", File.join(File.dirname(project.first.second), "gitolite-admin.git")]
... ... @@ -161,6 +152,7 @@ namespace :gitlab do
161 152 ###################################### DB ######################################
162 153  
163 154 task :db_dump => :environment do
  155 + backup_path_db = File.join(Gitlab.config.backup_path, "db")
164 156 FileUtils.mkdir_p(backup_path_db) until Dir.exists?(backup_path_db)
165 157 puts "Dumping database tables:"
166 158 ActiveRecord::Base.connection.tables.each do |tbl|
... ... @@ -179,6 +171,7 @@ namespace :gitlab do
179 171 end
180 172  
181 173 task :db_restore=> :environment do
  174 + backup_path_db = File.join(Gitlab.config.backup_path, "db")
182 175 puts "Restoring database tables:"
183 176 Rake::Task["db:reset"].invoke
184 177 Dir.glob(File.join(backup_path_db, "*.yml") ).each do |dir|
... ...