Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save LuckOfWise/4759142 to your computer and use it in GitHub Desktop.
Save LuckOfWise/4759142 to your computer and use it in GitHub Desktop.
# -*- coding: utf-8 -*-
task :backup do
FILES = 3
path = "config/database.yml"
info = YAML.load_file(path)["production"]
backup_dir = "/backup"
unless File.exists?(backup_dir)
Dir::mkdir(backup_dir)
end
backup_path = "#{backup_dir}/#{Time.now.strftime("%Y%m%d%H%M%S")}.sql"
command = "pg_dump #{info["database"]} | gzip > #{backup_path}.gz"
system(command)
while(Dir::glob(File.join(backup_dir,"*.gz")).size > FILES) do
del_file = Dir::glob(File.join(backup_dir,"*.gz")).sort.first
File.delete(del_file)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment