1

I have a mount where I store my db backups, and to avoid it growing beyond control, I only intend on keeping 30 days worth of backups, but also backups created every 15 days. So as 30 days expires, if the folder was created on the 1st or 15th, I would like it to stay as well.

I'm currently using this to only keep the backups for 30 days, how can I omit folders created on the 1st and 15th?

find /mnt/clusterfs01-data01/db01/* -type d -ctime +30 -exec rm -rf {} \;

Sample directory listing for folder names.

0 drwx------ 7 root root 324 Nov 11 01:35 2016-11-11_01-31-41
0 drwx------ 7 root root 250 Nov 11 06:25 2016-11-11_06-25-03

I don't mind a solution other than find.

2
  • 1
    Do your filenames contain the date? Or are you relying on the date stats of the file? Nov 11, 2016 at 14:03
  • I was relying on the date but the folder could be acceptable. I updated the question to include the folder names. @glennjackman
    – user508889
    Nov 11, 2016 at 14:08

2 Answers 2

2

To exclude matches based on the directory name's datetime string, you could add

! \( -name '????-15-??*' -o -name '????-30-??*' \)

or

-not \( -name '????-15-??*' -o -name '????-30-??*' \)

to the find command.

If you really want to base it on the timestamp, then the only way I can think to do that is something like (note I'm using mtime here since your touch commands don't seem to modify the ctime on my system: change %Td to %Cd for ctime):

find dir -type d -mtime +30 -printf '%Td\t%p\0' | 
    while read -rd '' day dir; do 
      case "$day" in 
        15|30) ;; 
        *) echo rm -rf -- "$dir";; 
      esac
    done
6
  • Steel, +1, but touch commands don't seem to modify the ctime on my system is weird cause touch makes the inode to change, which should modify the ctime. Which FS are you on?
    – heemayl
    Nov 11, 2016 at 17:16
  • @heemayl maybe I just copy-pasted it wrong - the dir is ext4 on an SSD that is mounted with noatime, if that makes a difference? Otherwise, it's vanilla Ubuntu 12.04 (well, mythbuntu actually) Nov 11, 2016 at 17:29
  • I experienced the same thing with the touch commands I had posted. It seems to leave the Change time but adjusts the access/modify times. I had to do my testing with mtime assuming the real deal would use ctime. @heemayl
    – user508889
    Nov 11, 2016 at 17:31
  • The more I thought about it, using the folder name is actually better. At least on ext4, the Birth time is not stored. The folder name has the timestamp in it, so for instance, 1:31:41 in my first example. The timstamp on from stat on the folder is 1:35:57. If the backup ever spanned over the day, that would cause a problem and then be on the 16th instead of the 15th perhaps. @heemayl
    – user508889
    Nov 11, 2016 at 17:41
  • 1
    @bc2946088 you may need to add -depth to the find command so that it traverses the tree in strict order Nov 11, 2016 at 18:30
1

Try this one:

rm -rf $(find /mnt/clusterfs01-data01/db01/* -maxdepth 0 -type d -ctime +30 | grep -v "[0-9]\{4\}-01\|15-[0-9]\{2\}")
1
  • Actually, the grep was just misplaced the date.
    – user508889
    Nov 11, 2016 at 16:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy