Applescript to purge backup copies

Discussion in 'Mac Programming' started by macstatic, May 7, 2015.

  1. macstatic macrumors 65816


    Oct 21, 2005
    I need a script which will purge (remove) old backups from two internal drives. Since I already have an automated backup setup (using Chronosync) I'm thinking I can just have it run this script whenever it does its backup.

    The first drive contains Adobe Lightroom catalogs. There are two backup folders on this drive:

    /Volumes/Cache HD/Family/Backups/
    /Volumes/Cache HD/Projects/Backups/

    Within these folders you'll find an endless number of backup folders named with the date, such as:

    2015-05-02 2214
    2015-05-03 2329
    2015-05-04 0049
    2015-05-04 0645
    2015-05-05 1441
    2015-05-05 2119
    2015-05-07 1756

    At any time I'd like to keep no more than say the 4 most recent backups.

    The second drive (incidently the Time Machine drive, using its root for this) consists of a folder with numerous media files (images, videos, audio files etc.) which are copied to here whenever I import anything new to Adobe Lightroom or via Adobe Photo-downloader, in addition to its main drive. This is just to ensure I have a backup from my memory cards in case the main drive goes down in the process etc.
    The imported files are either placed directly in the backup folder (/Volumes/Time Machine/Media backups/) or in a sub-folder of it named something like "Imported on 2 May 2015". The folder naming depends on how I configure it in Lightroom so this might change.

    I figure it might be best to purge according to the file's date, regardless of being in a sub-folder or not, so let's say I want to purge any file which is older than 3 weeks old, and if a sub-folder is purged so that it's empty that folder should also be deleted.

    Are there any ready made scripts around which would be adapted for this use?
  2. JustMartin macrumors 6502a

    Feb 28, 2012
    You could probably write a shell script to do this quite easily. The 'find' command will find files older than a certain number of days and these can be deleted. I have not tested this, but it should give you a clue

    find "/Volumes/Cache HD" -mtime +2 -exec rm {} \;
    That will find all files that were modified more than 2 days ago and execute the rm command on each one.

    You could put that into a shell script and either call it direct from terminal or an Applescript

Share This Page