Last active
December 20, 2015 17:29
-
-
Save paulmooring/6169463 to your computer and use it in GitHub Desktop.
This file contains hidden or 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 'date' | |
require 'syslog' | |
require 'fileutils' | |
bkup_dir = ARGV.first | |
log = Syslog.open($0, Syslog::LOG_PID | Syslog::LOG_CONS) | |
log.info "Starting backup rotation for #{bkup_dir}" | |
def get_days(day = Date.today, days = 7) | |
if day.class != Array | |
get_days(Array(day)) | |
else | |
if day.length == days | |
day | |
else | |
get_days day.push(day.last.prev_day) | |
end | |
end | |
end | |
def get_weeks(day = Date.today, weeks = 6) | |
until day.sunday? | |
day = day.prev_day | |
end | |
sundays = Array(day) | |
(weeks - 1).times do | |
sundays.push sundays.last.prev_day(7) | |
end | |
sundays | |
end | |
def get_first_sunday(day = Date.today, months_ago = 0) | |
mons_ago = day.prev_month(months_ago) | |
start_of_month = mons_ago.prev_day(mons_ago.mday - 1) | |
until start_of_month.sunday? | |
start_of_month = start_of_month.next_day | |
end | |
start_of_month | |
end | |
def get_months(day = Date.today, months = 6) | |
if day < get_first_sunday(day) | |
first_ofs = Array(get_first_sunday(day, 1)) | |
else | |
first_ofs = Array(get_first_sunday(day)) | |
months -= 1 | |
end | |
months.times do | |
first_ofs.push get_first_sunday(first_ofs.last, 1) | |
end | |
first_ofs | |
end | |
backups = { | |
:daily => get_days, | |
:weekly => get_weeks, | |
:monthly => get_months | |
} | |
def before_cutover?(date) | |
date < Date.parse("20130807") | |
end | |
Dir.glob("#{bkup_dir}/*").each do |backup| | |
file_name = File.basename(backup) | |
bkup_date = Date.parse(file_name.gsub!(/(weekly|monthly|daily)-/, "")) | |
if backups[:monthly].include? bkup_date | |
if file_name =~ /monthly/ | |
log.info "found monthly backup: #{file_name}" | |
elsif before_cutover? bkup_date | |
if bkup_date < Date.today.prev_month(6) | |
log.info "Deleting file: #{backup}" | |
FileUtils.rm_rf(backup) | |
end | |
else | |
log.info "Renaming file: #{file_name}" | |
File.rename(backup, backup.gsub(/(weekly|daily)/, "monthly")) | |
end | |
elsif backups[:weekly].include? bkup_date | |
if file_name =~ /weekly/ | |
log.info "found weekly backup: #{file_name}" | |
elsif before_cutover? bkup_date | |
if bkup_date < Date.today.prev_day(7 * 6) | |
log.info "Deleting file: #{backup}" | |
FileUtils.rm_rf(backup) | |
end | |
else | |
log.info "Renaming file: #{file_name}" | |
File.rename(backup, backup.gsub(/(monthly|daily)/, "monthly")) | |
end | |
elsif backups[:daily].include? bkup_date | |
log.info "Found daily backup: #{backup}" | |
elsif before_cutover? bkup_date | |
if bkup_date < Date.today.prev_day(7) | |
log.info "Deleting file: #{backup}" | |
FileUtils.rm_rf(backup) | |
end | |
else | |
log.info "Deleting file: #{backup}" | |
FileUtils.rm_rf(backup) | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment