Class: Dreamback::Backup
- Inherits:
-
Object
- Object
- Dreamback::Backup
- Defined in:
- lib/dreamback/backup.rb
Constant Summary collapse
- BACKUP_FOLDER =
"./dreamback"
Class Method Summary collapse
-
.rotate_backups ⇒ Object
Rotate out the backups that are past our limit.
-
.rotate_daily(backup_folders) ⇒ Array
Rotate folders based on the number of days to keep.
-
.rotate_time_machine(folders, today = nil) ⇒ Hash
Use a time machine-like algorithm to rotate backups.
-
.rsync_backup(link_dir) ⇒ Object
Sync to the backup server using link-dest to save space.
-
.rsync_delete(directories) ⇒ Object
This uses a hack where we sync an empty directory to remove files We do this because sftp has no recursive delete method.
-
.start ⇒ Object
Manage the backup process.
Class Method Details
.rotate_backups ⇒ Object
Rotate out the backups that are past our limit
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 |
# File 'lib/dreamback/backup.rb', line 19 def self.rotate_backups backup_to_link = nil Net::SFTP.start(Dreamback.settings[:backup_server], Dreamback.settings[:backup_server_user]) do |sftp| # Create the backup folder if it doesn't exist backup_folder_exists = false begin sftp.stat!(BACKUP_FOLDER) do |response| backup_folder_exists = response.ok? end rescue Net::SFTP::StatusException => e backup_folder_exists = false if e.code == 2 end # Get a list of all backup directories backup_folders = [] if backup_folder_exists sftp.dir.foreach(BACKUP_FOLDER) do |entry| # Names should be like "dreamback.20120520", so we can sort on the folder name backup_folders << [ entry.name, entry.name.split(".")[1].to_i ] if entry.name.include?("dreamback") backup_folders.sort! {|a,b| b <=> a} end else sftp.mkdir(BACKUP_FOLDER) end # Don't rotate if there aren't any previous folders unless backup_folders.empty? # Get yesterday's folder to link against backup_to_link = backup_folders.first[0] # If this would link us to the same folder, don't do that. Try yesterday's instead. if backup_to_link.eql? "dreamback." + Date.today.strftime("%Y%m%d") backup_to_link = "dreamback." + (Date.today - 1).strftime("%Y%m%d") end # Delete any folders older than our limit if Dreamback.settings[:days_to_keep] folders_to_delete = rotate_daily(backup_folders) elsif Dreamback.settings[:keep_time_machine] folders_to_delete = rotate_time_machine(backup_folders)[:delete] else folders_to_delete = nil end rsync_delete(folders_to_delete) end end backup_to_link end |
.rotate_daily(backup_folders) ⇒ Array
Rotate folders based on the number of days to keep
119 120 121 122 123 124 125 126 127 128 129 |
# File 'lib/dreamback/backup.rb', line 119 def self.rotate_daily(backup_folders) # Subtract one to account for the folder we're about to create # Normally we remove a folder so that our count is one less than the "days to keep" # However, if today's folder already exists then there's no need offset = backup_folders.include?("dreamback.#{Time.now.strftime("%Y%m%d")}") ? 0 : 1 if backup_folders.length >= Dreamback.settings[:days_to_keep] - offset folders_to_delete = backup_folders.slice(Dreamback.settings[:days_to_keep] - offset, backup_folders.length) folders_to_delete.map! {|f| f[0]} end folders_to_delete ||= nil end |
.rotate_time_machine(folders, today = nil) ⇒ Hash
Use a time machine-like algorithm to rotate backups. This will keep daily backups for seven days, weeklies for three months, and monthlies forever
135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 |
# File 'lib/dreamback/backup.rb', line 135 def self.rotate_time_machine(folders, today = nil) today ||= Date.today ymd = "%Y%m%d" folder_data = {} folders.each do |f| folder_data[f] = { :date => Date.strptime(f[1].to_s, ymd) } end keep = [] one_week_ago = today - 7 three_months_ago = today << 3 week_hash = {} month_hash = {} folder_data.each do |k, v| if v[:date] >= one_week_ago keep << k elsif v[:date] >= three_months_ago week_hash[v[:date].cweek] = [] if week_hash[v[:date].cweek].nil? week_hash[v[:date].cweek] << [k, v] else ym = v[:date].strftime("%Y%m").to_i month_hash[ym] = [] if month_hash[ym].nil? month_hash[ym] << [k, v] end end week_hash.each do |week, dates| dates.sort! {|a, b| a[1][:date] <=> b[1][:date]} end week_hash.each do |week, dates| keep << dates.shift[0] unless dates.empty? end month_hash.each do |month, dates| dates.sort! {|a, b| a[1][:date] <=> b[1][:date]} end month_hash.each do |month, dates| keep << dates.shift[0] unless dates.empty? end keep.sort!.compact! # Convert elements from ["dreamback.20120701", "20120701"] to "dreamback.20120701" keep_clean = keep.map {|folder| folder.shift} delete = (folders - keep).map {|folder| folder.shift} { :keep => keep_clean, :delete => delete} end |
.rsync_backup(link_dir) ⇒ Object
Sync to the backup server using link-dest to save space
89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 |
# File 'lib/dreamback/backup.rb', line 89 def self.rsync_backup(link_dir) tmp_dir_path = "~/.dreamback_tmp" tmp_dir = File.(tmp_dir_path) Dir.mkdir(tmp_dir) unless File.exists?(tmp_dir) user_exclusions_path = File.("~/.dreamback_exclusions") default_exclusions_path = File.("../exclusions.txt", __FILE__) exclusions_path = File.exists?(user_exclusions_path) ? user_exclusions_path : default_exclusions_path begin backup_server_user = Dreamback.settings[:backup_server_user] backup_server = Dreamback.settings[:backup_server] today = Time.now.strftime("%Y%m%d") Dreamback.settings[:dreamhost_users].each do |dreamhost| # User that we're going to back up user = dreamhost[:user] server = dreamhost[:server] # rsync won't do remote<->remote syncing, so we stage everything here first `rsync -e ssh -av --keep-dirlinks --exclude-from #{exclusions_path} --copy-links #{user}@#{server}:~/ #{tmp_dir}/#{user}@#{server}` end # Now we can sync local to remote. Only use link-dest if a previous folder to link to exists. link_dest = link_dir.nil? ? "" : "--link-dest=~#{BACKUP_FOLDER.gsub(".", "")}/#{link_dir}" `rsync -e ssh -av --delete --copy-links --keep-dirlinks #{link_dest} #{tmp_dir}/ #{backup_server_user}@#{backup_server}:#{BACKUP_FOLDER}/dreamback.#{today}` ensure # Remove the staging directory `rm -rf #{File.(tmp_dir_path)}` end end |
.rsync_delete(directories) ⇒ Object
This uses a hack where we sync an empty directory to remove files We do this because sftp has no recursive delete method
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 |
# File 'lib/dreamback/backup.rb', line 71 def self.rsync_delete(directories) return if directories.nil? || directories.empty? empty_dir_path = File.("../.dreamback_empty_dir", __FILE__) empty_dir = Dir.mkdir(empty_dir_path) unless File.exists?(empty_dir_path) begin directories.each do |dir| `rsync --delete -a #{empty_dir_path}/ #{Dreamback.settings[:backup_server_user]}@#{Dreamback.settings[:backup_server]}:#{BACKUP_FOLDER}/#{dir}` Net::SFTP.start(Dreamback.settings[:backup_server], Dreamback.settings[:backup_server_user]) do |sftp| sftp.rmdir!("#{BACKUP_FOLDER}/#{dir}") end end ensure Dir.delete(empty_dir_path) end end |
.start ⇒ Object
Manage the backup process
8 9 10 11 12 13 14 15 |
# File 'lib/dreamback/backup.rb', line 8 def self.start # rotate the backup folders backup_to_link = rotate_backups # rsync backup of dreamhost user accounts rsync_backup(backup_to_link) # mysql backup # TODO: Add mysql backups end |