Last active
August 29, 2015 14:05
-
-
Save 5t111111/73f870f77111445171ec to your computer and use it in GitHub Desktop.
/etc/cron.daily/backup-wordpress.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env ruby | |
require 'pathname' | |
require 'fileutils' | |
def backup(src, dst) | |
src_path = File.expand_path(src) | |
dst_path = File.expand_path(dst) | |
name = File.basename(src) | |
name = File.join(dst_path, name) # full path | |
p name | |
return false unless Pathname.new(name).absolute? | |
p name | |
backup_filename = name + '.tar.gz.' + Time.new.strftime('%Y%m%d%H%M%S') | |
p backup_filename | |
cmd = "tar cvfzp #{backup_filename} #{src_path}" | |
`#{cmd}` | |
purge_old_backups(name, 3) | |
end | |
def purge_old_backups(backup_name, number) | |
files = [] | |
Dir::glob("#{backup_name}.tar.gz.*").each do |f| | |
files << f | |
end | |
files.sort! | |
(files.size - number).times.each do | |
puts "Removing #{files.first} ..." | |
FileUtils.rm(files.shift) | |
end | |
end | |
backup('/mnt/nfs/wordpress', '/mnt/nfs/backup/wordpress') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment