I'm trying to get crontab to automatically remove backup folders in a directory apart from the most recent 6 folders as a backup is created in the directory every day.
All my backup folders are just datestamps e.g: 2017_12_29.
When I run rm -rf $(ls -1 /BACKUPDIR/ | head -n -6)
from a terminal it deletes all folders except the most recent 6 which is what I expect it to do, but it doesn't seem to do anything when crontab runs it and nothing is deleted from the directory.
I'm not sure where I'm going wrong here.
EDIT: Currently I'm just using rsync to clone the / directory excluding a few directories.
This is what it looks like in root's crontab:
00 22 * * * /usr/bin/rsync -aqvHAXS --exclude /dev/ --exclude /proc/ --exclude /sys/ --exclude /tmp/ --exclude /run/ --exclude /mnt/ --exclude /media/ --exclude /lost+found/ --delete / /BACKUPDIR/$(date +\%Y_\%m_\%d)
/BACKUPDIR/
? As a simple debug, put under cron something likels -1 /BACKUPDIR/ | head -n -6 > /tmp/debug 2>&1
and see what appears in/tmp/debug
. Also, if not already, have a look atrsnapshot
tool it handles all things for you automatically. – Patrick Mevzek Dec 29 '17 at 22:59ls
– dessert Dec 30 '17 at 8:24