I'm pretty new to Keyboard Maestro, and very much still finding my way- I'm sure there's probably a simple solution to this, but I've been unable to find the answer so far.
I am trying to build a macro that will make a daily copy of a folder to a new location, add a dated suffix to the name of that copy, and then when I have built up 10 copies trash the oldest copy, meaning that from the point that the macro has run 10 times I will always have the last 10 copies.
I can't work out how to tell my macro to not start deleting old copies until there are 10 versions in the location I'm copying to. I have been using the attached macro, and just disabling the last action until I have my 10 copies, but I can't help but feel there must be a much more elegant solution out there!
I found this macro on the forum that would count the number of items in a folder, but I can't work out how I would use this data to not trigger the 'Trash File' action until I had reached my count of 10:
Thank you so much for reading, and for any advice you can offer, its much appreciated.
I'm not sure why you need a date-suffix added to the file, since all files will always "have a creation date" that you can access. It will be much easier to use shell commands to delete files older than ten days when using that date, rather than using a date you encode into the file name.
I'm not sure why you want to make a copy of a directory instead of just zipping the directory and then moving the zip file into your folder. This will be much faster to run, and it will reduce your data storage perhaps by a lot.
I'm not sure how you plan to ensure that no files will be opened when you try to run this macro. Open files could mess up your backup.
I won't object to using KM to implement this, but I don't think you need KM at all. You just need to run the backup as a shell command that's scheduled to run overnight, and you can also run the cleanup command the same way. Both of these commands are likely writeable as a single shell command each.
When I had to do this, I used Terminal and rsync, which lets you create a Time Machine-like backup where older backups are deleted as required. It is Unix, so no pretty GUI, but it works very well.
Start by using a 4-digit year in your renaming -- you just might be using your macro in 76 years' time and it would be a shame if it failed
You've already coped -- very nicely -- with deleting the "least recent copy". All you need to do is wait until there's 10. The brute force way of doing that would be to reverse your file listing so that newest files are first, then count the loops and delete if the counter is greater than 10:
Perhaps better to use you original deletion method, but wrap it in a "While" action that finds the number of directories in the "Archive" folder:
That isn't "correct" though -- the shell counts apps, RTFDs, and so on as directories. You'll be OK for this particular use, but perhaps better to use AppleScript so it's more generally useful:
Lots of options, and probably more being posted even now, so pick what works best for you. But test thoroughly!
There's nothing wrong with his solution, but I would definitely just schedule my backup command to run nightly that looked like this:
gzip -r ~/data/* > ~/backups/gzip_$RANDOM$RANDOM
Then I would also run a command nightly that would delete any file in the backups folder that was over 10 days old.
In case you are wondering, the double RANDOM above just makes each filename unique. The filenames need to be different and unique, but don't need to contain the date.
Whereas I just let Time Machine take the strain and never even think about it -- except, of course, for monthly restore attempts to make sure things are still working.
I know that lots of people have had trouble with TM. I'm afraid that, for me, it's always worked as advertised.
I use both. Time Machine runs on its schedule, and then my ultra-critical backups run more frequently than that via the rsync time machine, and copy things to two separate drives (and one cloud for truly critical stuff).
So many options, thank you all so much! As I say, I'm pretty new to this, hence my very rookie lack of knowledge! But thank you all for so many options to test!