Skip to content

Instantly share code, notes, and snippets.

@mmrwoods
Created May 20, 2011 16:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mmrwoods/983229 to your computer and use it in GitHub Desktop.
Save mmrwoods/983229 to your computer and use it in GitHub Desktop.
Dumb-ass Daily Backup - Better than no backup!
#!/usr/bin/env ruby
# Dumb-ass Daily Backup - Better than no backup, and so simple it can't fail!
#
# A backup directory will be created within the destination directory for each key of the sources hash.
# Values can be files, directories or glob patterns.
# Directories will be backed up as gzipped tar archives, text files are gzipped, binary files are copied.
# Create a /etc/dumb_ass_daily_backup.yml config file to override the default sources and destination.
# Nothing fancy, no incremental backups, no syncing to remote servers, just a dumb backup script.
require 'yaml'
require 'fileutils'
config = File.exist?("/etc/dumb_ass_daily_backup.yml") ? YAML::load_file("/etc/dumb_ass_daily_backup.yml") : {}
sources = config['sources'] || {
"etc" => "/etc/"
}
destination = config['destination'] || "/var/local/backup/daily/"
today = Time.now.strftime('%A').downcase
destination_with_day = File.join(destination, today)
puts "Deleting #{destination_with_day} if exists..."
system("/bin/rm -rf #{destination_with_day}")
sources.each do |name, glob_pattern|
backup_path = File.join(destination, today, name)
FileUtils.mkdir_p(backup_path)
puts "\n#{name}: backing up #{glob_pattern} to #{backup_path}"
Dir.glob(glob_pattern).each do |path|
print "* #{File.basename(path)} -> "
if File.directory?(path)
# directory, tar and gzip it
backup_file_path = File.join(backup_path, "#{File.basename(path)}.tar.gz")
system("tar -zpc \"#{path}\" > \"#{backup_file_path}\" 2> /dev/null")
elsif `file -b "#{path}"`.include?('text')
# looks like a text file, gzip it
backup_file_path = File.join(backup_path, "#{File.basename(path)}.gz")
system("gzip -c \"#{path}\" > \"#{backup_file_path}\"")
else
# seems to be a binary, just copy it
backup_file_path = File.join(backup_path, File.basename(path))
system("/bin/cp \"#{path}\" \"#{backup_file_path}\"")
end
print "#{backup_file_path}\n"
end
end
puts "\nCleaning up permissions in destination directory..."
system("find #{destination_with_day} -type d -exec chmod 700 \\{} \\;")
system("find #{destination_with_day} -type f -exec chmod 600 \\{} \\;")
sources:
etc: /etc/
mail: "/var/spool/virtual/*"
destination: /var/local/backup/daily/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment