Skip to content

Instantly share code, notes, and snippets.

@aldavigdis
Last active August 29, 2015 14:23
Show Gist options
  • Save aldavigdis/50b4bb4487a09d7b685f to your computer and use it in GitHub Desktop.
Save aldavigdis/50b4bb4487a09d7b685f to your computer and use it in GitHub Desktop.
Daily backup from a Rails application
#! /usr/bin/env ruby -w
# Include the YAML library
require 'yaml'
# Get the current Rails environment. Falls back on 'production'
backup_env = (ENV['RAILS_ENV'] || 'production')
# Set the backup location
backup_location = ARGV[0] || '.'
# Set the config file location
config_location = ARGV[1] || '/var/unmmdb/config/database.yml'
# Read the config file and assign the relevant values as variables
config = YAML.load_file(config_location)
database = config[backup_env]['database']
username = config[backup_env]['username']
password = config[backup_env]['password']
# Run mysqldump and write the bzipped output to the location
backup = system "mysqldump --add-drop-table --user=#{username} "\
"--password=#{password} #{database} | bzip2 > "\
"#{backup_location}/weekly.$(date -u \"+%G-%V\").sql.bz2"
# Output a message to stderr on error.
# This is the message cron delivers to root
if backup == false
STDERR.puts 'Weekly Automatic Backup failed'
else
puts 'Done.'
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment