Skip to content

Commit f78f11d

Browse files
author
shantibraford
committed
Postgres dump support; MySQL dump options (ability to specify); ability to specify AWS keys via ENV vars
1 parent e1b841c commit f78f11d

File tree

3 files changed

+26
-10
lines changed

3 files changed

+26
-10
lines changed

config/backup_fu.yml.advanced_example

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,10 @@ aws_secret_access_key: --replace me with your AWS secret access key--
1111
verbose: true
1212

1313
# If the user running the script cannot find 'mysqldump' in its path, specify the full path with:
14-
mysqldump_path: /usr/local/mysql/bin/mysqldump
14+
dump_path: /usr/local/mysql/bin/mysqldump
15+
16+
# To specify your own options for mysqldump. Default is: "--complete-insert --skip-extended-insert"
17+
mysqldump_options: --single-transaction --flush-logs --add-drop-table --add-locks --create-options --disable-keys --extended-insert --quick
1518

1619
# Enable the ability to backup your application's static files with the 'static_paths' config key.
1720
# Specify either relative (to RAILS_ROOT) paths (space-delimited)
@@ -28,4 +31,4 @@ disable_tar_gzip: true
2831

2932
# To turn on using 'nice' to keep backup CPU processing to a minimum:
3033
enable_nice: true
31-
nice_level: 15
34+
nice_level: 15

lib/backup_fu.rb

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,11 @@ class S3ConnectError < StandardError; end
77

88
class BackupFu
99

10-
def initialize(period = 'daily')
11-
@period = period
10+
def initialize
1211
db_conf = YAML.load_file(File.join(RAILS_ROOT, 'config', 'database.yml'))
1312
@db_conf = db_conf[RAILS_ENV].symbolize_keys
1413
@fu_conf = YAML.load_file(File.join(RAILS_ROOT, 'config', 'backup_fu.yml')).symbolize_keys
14+
@fu_conf[:mysqldump_options] ||= '--complete-insert --skip-extended-insert'
1515
@verbose = !@fu_conf[:verbose].nil?
1616
check_conf
1717
create_dirs
@@ -29,7 +29,12 @@ def dump
2929
password = "--password=#{@db_conf[:password]}"
3030
end
3131
full_dump_path = File.join(dump_base_path, db_filename)
32-
cmd = niceify "#{mysqldump_path} --complete-insert --skip-extended-insert #{host} #{port} --user=#{@db_conf[:username]} #{password} #{@db_conf[:database]} > #{full_dump_path}"
32+
case @db_conf[:adapter]
33+
when 'postgresql'
34+
cmd = niceify "PGPASSWORD=#{password} #{dump_path} --user=#{@db_conf[:username]} --host=#{host} --port=#{port} #{@db_conf[:database]} > #{full_dump_path}"
35+
when 'mysql'
36+
cmd = niceify "#{dump_path} #{@fu_conf[:mysqldump_options]} #{host} #{port} --user=#{@db_conf[:username]} #{password} #{@db_conf[:database]} > #{full_dump_path}"
37+
end
3338
puts cmd if @verbose
3439
`#{cmd}`
3540

@@ -125,13 +130,21 @@ def check_conf
125130
raise BackupFuConfigError, 'Application name (app_name) key not set in config/backup_fu.yml.'
126131
elsif @fu_conf[:s3_bucket] == 'some-s3-bucket'
127132
raise BackupFuConfigError, 'S3 bucket (s3_bucket) not set in config/backup_fu.yml. This bucket must be created using an external S3 tool like S3 Browser for OS X, or JetS3t (Java-based, cross-platform).'
128-
elsif @fu_conf[:aws_access_key_id].include?('--replace me') || @fu_conf[:aws_secret_access_key].include?('--replace me')
129-
raise BackupFuConfigError, 'AWS Access Key Id or AWS Secret Key not set in config/backup_fu.yml.'
133+
else
134+
# Check for access keys set as environment variables:
135+
if ENV.keys.include?('AMAZON_ACCESS_KEY_ID') && ENV.keys.include?('AMAZON_SECRET_ACCESS_KEY')
136+
@fu_conf[:aws_access_key_id] = ENV['AMAZON_ACCESS_KEY_ID']
137+
@fu_conf[:aws_secret_access_key] = ENV['AMAZON_SECRET_ACCESS_KEY']
138+
elsif @fu_conf[:aws_access_key_id].include?('--replace me') || @fu_conf[:aws_secret_access_key].include?('--replace me')
139+
raise BackupFuConfigError, 'AWS Access Key Id or AWS Secret Key not set in config/backup_fu.yml.'
140+
end
130141
end
131142
end
132143

133-
def mysqldump_path
134-
@fu_conf[:mysqldump_path] || 'mysqldump'
144+
def dump_path
145+
dump = {:postgresql => 'pg_dump',:mysql => 'mysqldump'}
146+
# Note: the 'mysqldump_path' config option is DEPRECATED but keeping this in for legacy config file support
147+
@fu_conf[:mysqldump_path] || @fu_conf[:dump_path] || dump[@db_conf[:adapter].intern]
135148
end
136149

137150
def dump_base_path

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
b.dump_static
5252
end
5353

54-
desc "Backups up static files to Amazon S3. For configuration see the backup_fo README."
54+
desc "Backups up static files to Amazon S3. For configuration see the backup_fu README."
5555
task :backup do
5656
b = BackupFu.new
5757
b.backup_static

0 commit comments

Comments
 (0)