Skip to content

Instantly share code, notes, and snippets.

@justqyx
Created December 24, 2014 02:53
Show Gist options
  • Save justqyx/8192869069d61403bcd2 to your computer and use it in GitHub Desktop.
Save justqyx/8192869069d61403bcd2 to your computer and use it in GitHub Desktop.
清理 PostgreSQL 备份文件
require 'date'
file_paths = Dir.glob("/BACKUP_DIR/*.dump")
file_paths.map! do |file_path|
basename = File.basename(file_path, '.dump')
key = Date.parse(basename)
[key, file_path]
end.sort! do |a, b|
a[0] <=> b[0]
end
# 保留最近 5 份
file_paths.pop 5
# 保留 01 和 15 号的备份文件
file_paths.each do |f_obj|
d = f_obj[0].day
File.delete(f_obj[1]) unless(d == 1 || d == 15)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment