Skip to content

Commit 58b1704

Browse files
author
Samuel Goebert
committed
added zip support
1 parent 47d9e84 commit 58b1704

File tree

4 files changed

+83
-43
lines changed

4 files changed

+83
-43
lines changed

README

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ BackupFu
33

44
The backup_fu plugin makes it redonkulously easy to:
55

6-
A) dump your database and/or static files to tar/gzipped archives, and
6+
A) dump your database and/or static files to tar/gzipped or zipped archives, and
77
B) upload these archives to a private Amazon S3 bucket for safekeeping
88

99
Installation
@@ -60,7 +60,8 @@ Advanced options include:
6060
* specify static path(s) that should be backed up -- i.e. backup your entire 'public/static' directory
6161
* change default dump path from RAILS_ROOT/tmp/backup to whatever
6262
* specify fully-qualified 'mysqldump' path
63-
* disable tar/gzipping of database dump
63+
* disable compression of database dump
64+
* choose between zip or tar/gzip compression
6465
* enable 'nice' with level specification to prevent backup_fu from bogarding your server
6566

6667
Cronjob Installation

config/backup_fu.yml.advanced_example

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,9 @@ static_paths: "/u/apps/foo/current/public/static"
2626
# The default dump base path is 'tmp/backups'. This can be changed with:
2727
dump_base_path: /tmp
2828

29+
# To use tar-gzipping or zipping for compression. Use the values tar-gzip or zip here. Default is tar-gzip.
30+
compressor: tar-gzip
31+
2932
# To disable compression of the DB backup:
3033
disable_compression: true
3134

lib/backup_fu.rb

Lines changed: 76 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ def dump_static
6161
if !@fu_conf[:static_paths]
6262
raise BackupFuConfigError, 'No static paths are defined in config/backup_fu.yml. See README.'
6363
end
64-
paths = @fu_conf[:static_paths].split(' ')´
64+
paths = @fu_conf[:static_paths].split(' ')
6565
compress_static(paths)
6666
end
6767

@@ -73,24 +73,30 @@ def backup_static
7373
puts "\nBacking up Static files to S3: #{file}\n" if @verbose
7474

7575
AWS::S3::S3Object.store(File.basename(file), open(file), @fu_conf[:s3_bucket], :access => :private)
76-
7776
end
78-
77+
7978
def cleanup
8079
count = @fu_conf[:keep_backups].to_i
8180
backups = Dir.glob("#{dump_base_path}/*.{sql}")
8281
if count >= backups.length
8382
puts "no old backups to cleanup"
8483
else
8584
puts "keeping #{count} of #{backups.length} backups"
86-
85+
8786
files_to_remove = backups - backups.last(count)
88-
files_to_remove = files_to_remove.concat(Dir.glob("#{dump_base_path}/*.{gz}")[0, files_to_remove.length]) unless @fu_conf[:disable_compression]
89-
87+
88+
if(!@fu_conf[:disable_compression])
89+
if(@fu_conf[:compressor] == 'zip')
90+
files_to_remove = files_to_remove.concat(Dir.glob("#{dump_base_path}/*.{zip}")[0, files_to_remove.length])
91+
else
92+
files_to_remove = files_to_remove.concat(Dir.glob("#{dump_base_path}/*.{gz}")[0, files_to_remove.length])
93+
end
94+
end
95+
9096
files_to_remove.each do |f|
9197
File.delete(f)
9298
end
93-
99+
94100
end
95101
end
96102

@@ -135,30 +141,46 @@ def dump_base_path
135141
def db_filename
136142
"#{@fu_conf[:app_name]}_#{ @timestamp }_db.sql"
137143
end
138-
144+
139145
def db_filename_compressed
140-
db_filename.gsub('.sql', '.tar')
146+
if(@fu_conf[:compressor] == 'zip')
147+
db_filename.gsub('.sql', '.zip')
148+
else
149+
db_filename.gsub('.sql', '.tar')
150+
end
141151
end
142-
152+
143153
def final_db_dump_path
144-
if @fu_conf[:disable_compression]
154+
if(@fu_conf[:disable_compression])
145155
filename = db_filename
146156
else
147-
filename = db_filename.gsub('.sql', '.tar.gz')
157+
if(@fu_conf[:compressor] == 'zip')
158+
filename = db_filename.gsub('.sql', '.zip')
159+
else
160+
filename = db_filename.gsub('.sql', '.tar.gz')
161+
end
148162
end
149163
File.join(dump_base_path, filename)
150164
end
151-
165+
152166
def static_compressed_path
153-
f = "#{@fu_conf[:app_name]}_#{ @timestamp }_static.tar"
167+
if(@fu_conf[:compressor] == 'zip')
168+
f = "#{@fu_conf[:app_name]}_#{ @timestamp }_static.zip"
169+
else
170+
f = "#{@fu_conf[:app_name]}_#{ @timestamp }_static.tar"
171+
end
154172
File.join(dump_base_path, f)
155173
end
156-
174+
157175
def final_static_dump_path
158-
f = "#{@fu_conf[:app_name]}_#{ @timestamp }_static.tar.gz"
176+
if(@fu_conf[:compressor] == 'zip')
177+
f = "#{@fu_conf[:app_name]}_#{ @timestamp }_static.zip"
178+
else
179+
f = "#{@fu_conf[:app_name]}_#{ @timestamp }_static.tar.gz"
180+
end
159181
File.join(dump_base_path, f)
160182
end
161-
183+
162184
def create_dirs
163185
ensure_directory_exists(dump_base_path)
164186
end
@@ -180,17 +202,24 @@ def datetime_formatted
180202
end
181203

182204
def compress_db(dump_base_path, db_filename)
183-
tar_path = File.join(dump_base_path, db_filename_compressed)
205+
compressed_path = File.join(dump_base_path, db_filename_compressed)
184206

185-
# TAR it up
186-
cmd = niceify "tar -cf #{tar_path} -C #{dump_base_path} #{db_filename}"
187-
puts "\nTar: #{cmd}\n" if @verbose
188-
`#{cmd}`
207+
if(@fu_conf[:compressor] == 'zip')
208+
cmd = niceify "zip #{compressed_path} #{dump_base_path}/#{db_filename}"
209+
puts "\nZip: #{cmd}\n" if @verbose
210+
`#{cmd}`
211+
else
189212

190-
# GZip it up
191-
cmd = niceify "gzip -f #{tar_path}"
192-
puts "\nGzip: #{cmd}" if @verbose
193-
`#{cmd}`
213+
# TAR it up
214+
cmd = niceify "tar -cf #{compressed_path} -C #{dump_base_path} #{db_filename}"
215+
puts "\nTar: #{cmd}\n" if @verbose
216+
`#{cmd}`
217+
218+
# GZip it up
219+
cmd = niceify "gzip -f #{compressed_path}"
220+
puts "\nGzip: #{cmd}" if @verbose
221+
`#{cmd}`
222+
end
194223
end
195224

196225
def compress_static(paths)
@@ -202,23 +231,30 @@ def compress_static(paths)
202231
end
203232

204233
puts "Static Path: #{p}" if @verbose
205-
if path_num == 0
206-
tar_switch = 'c' # for create
234+
235+
if @fu_conf[:compressor] == 'zip'
236+
cmd = niceify "zip -r #{static_compressed_path} #{p}"
237+
puts "\nZip: #{cmd}\n" if @verbose
238+
`#{cmd}`
207239
else
208-
tar_switch = 'r' # for append
209-
end
240+
if path_num == 0
241+
tar_switch = 'c' # for create
242+
else
243+
tar_switch = 'r' # for append
244+
end
210245

211-
# TAR
212-
cmd = niceify "tar -#{tar_switch}f #{static_compressed_path} #{p}"
213-
puts "\nTar: #{cmd}\n" if @verbose
214-
`#{cmd}`
246+
# TAR
247+
cmd = niceify "tar -#{tar_switch}f #{static_compressed_path} #{p}"
248+
puts "\nTar: #{cmd}\n" if @verbose
249+
`#{cmd}`
215250

216-
path_num += 1
217-
end
251+
path_num += 1
218252

219-
# GZIP
220-
cmd = niceify "gzip -f #{static_compressed_path}"
221-
puts "\nGzip: #{cmd}" if @verbose
222-
`#{cmd}`
253+
# GZIP
254+
cmd = niceify "gzip -f #{static_compressed_path}"
255+
puts "\nGzip: #{cmd}" if @verbose
256+
`#{cmd}`
257+
end
258+
end
223259
end
224260
end

tasks/backup_fu_tasks.rake

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ namespace :backup_fu do
5151

5252
namespace :static do
5353

54-
desc "Tars and gzips static application files locally. Does *not* upload to S3."
54+
desc "Zips or Tars and gzips static application files locally. Does *not* upload to S3."
5555
task :dump do
5656
b = BackupFu.new
5757
b.dump_static

0 commit comments

Comments
 (0)