Question

I use a 12+31 backup system -- that is, I keep one file from each month for the past year, plus one file for each day of the current month. I'm having a hard time figuring out how to build a bash script that will do the following cleanup:

  • search a directory and its subdirectories on a remote SFTP server
  • in each directory, look for files that are: NOT from this month, AND not from the FIRST of the previous month
  • with the selected files, delete them off the remote SFTP server

For instance, if it is now November, this script would:

  • search all the directories below a specified target, on my remote SFTP server
  • ignore all the files that were created in November
  • remove all the files from October EXCEPT for the files from October 1st
  • remove all the files from September EXCEPT for the files from September 1st
  • etc...

This will be running on a shared hosting server (Dreamhost), so I won't have the ability to install any custom CLI tools.

All of my backup files include the date in the filename, but I'd prefer to check against the file creation date if possible.

I've seen a lot of stuff on deleting files older than [x], but I'm not at all clear how to skip over files that are from the first of the month.

Was it helpful?

Solution

Here's the best I can think of for the moment:

find . -type f \         # files only
    -mtime +31 \         # exclude files less than 31 days old
    -printf '%Td %p\n' | # prepend filename with day of the month of last modification
  grep -v '^01' |
  sed 's/^[0-9][0-9] //'

Some caveats:

  • depends on GNU find
  • excludes files within the last 31 days, rather than strictly excluding the current month
  • looks at last modification time, rather than creation time. Most filesystems don't track creation time; if yours does, you'll need some tool other than GNU find to access it
  • you can't have newlines in your filenames
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top