Skip to main content

Script to delete old backups files created with Cobian Backup

The following ruby code will delete from a folder old backups files created with Cobian Backup. The deletion is recursive, so all the folders within the folder will be cleaned.

# 
# Deletes old backups made with Cobian Backup. This code is released under the MIT license.
 
NUMBER_OF_FILES_TO_KEEP = 6
def delete_old_backups(path)
 files =  Dir.entries(path)
 files.sort! {|x,y| y <=> x }
 files.delete_if {|x| x == "." or  x == ".." }
 name_of_file = " "
 new_name_of_file = ""
 file_cnt = 0
 files.each do |f|
   puts f
   new_name_of_file = f.split(" ")[0]
   if new_name_of_file != name_of_file
      file_cnt = 0
      name_of_file = new_name_of_file
   end
    if File.directory?(path + '/' + f)
      delete_old_backups(path + '/' + f)
    end
    file_cnt = file_cnt + 1
    if file_cnt > NUMBER_OF_FILES_TO_KEEP
      puts "delete " + path + '/' + f
      File.delete(path + '/' + f)
    end
  end
end

delete_old_backups("./..")

Comments

Mikesco3 said…
I have a set of scripts that also remove old backups, only I wrote them in Batch because ruby is not always available on all of the machines where I'm running Cobian backup.
the script runs in two parts,
the first part collects a list of what is currently there and renames the list of files that was made an x number of backups ago for deletion.
http://docs.google.com/View?id=dc8hktjh_81f5t9wvhr

the second script deletes the contents that are listed in the list that is ready for deletion.
http://docs.google.com/View?id=dc8hktjh_79cgmfd6ff