I have a pair of bash scripts, 1 that dumps mysql dbs, and the second to purge old backups.
I have always relied on date, so any files older than 7 days are purged, and new backups created daily. The result was a set of backups 7 days back.
Well now that I have 20+ dbs I have separated that daily job into a daily, and weekly job. If I continue to use the find and date method I would lose the past week.
QUESTION
How can I sort the output of a find by date, and then purge all but the most recent 7. (so even if the most recent is 2 months old, it remains)
This is as far as I got, but it is not as expected. Perhaps using ls might be the better choice 9find is a remnant from using date as the sole criteria for purging)
OLDFILES=`find -regex .*sql.*`
set -- $OLDFILES
if [ -z $1 ]
then
printf "\tNo files matching purge criteria\n" | tee -a $LOGFILE
else
printf "\tSQL Files being Delete from $HERE\n" | tee -a $LOGFILE
printf "\t\t%s\n" $OLDFILES | tee -a $LOGFILE
fi
# $RETAIN is the # of recent to keep
# so I want to grab a subset from 0 to (total - retain), and purge them
TOBURN=$(( $# - $RETAIN ))
printf " grab first %s files. %s - %s" $TOBURN $# $RETAIN
if [ $TOBURN -gt 0 ]
then
TOPURGE=("$@:0:$TOBURN")
printf "\n\nREMOVING..\n\t\t%s\n" $TOPURGE
fi
Existing files
./webbmaster_bellarose_joomla_01-09-2010.sql.gz
./webbmaster_bellarose_joomla_12-25-2009.sql.gz
./webbmaster_bellarose_joomla_01-10-2010.sql.gz
./webbmaster_bellarose_joomla_01-04-2010.sql.gz
./webbmaster_bellarose_joomla_01-25-2010.sql.gz
./webbmaster_bellarose_joomla_01-29-2010.sql.gz
./webbmaster_bellarose_joomla_12-30-2009.sql.gz
./webbmaster_bellarose_joomla_12-28-2009.sql.gz
./webbmaster_bellarose_joomla_01-06-2010.sql.gz
./webbmaster_bellarose_joomla_01-13-2010.sql.gz
./webbmaster_bellarose_joomla_01-24-2010.sql.gz
./webbmaster_bellarose_joomla_12-21-2009.sql.gz
./webbmaster_bellarose_joomla_12-24-2009.sql.gz
./webbmaster_bellarose_joomla_12-27-2009.sql.gz
./webbmaster_bellarose_joomla_01-26-2010.sql.gz
./webbmaster_bellarose_joomla_12-17-2009.sql.gz
./webbmaster_bellarose_joomla_01-03-2010.sql.gz
./webbmaster_bellarose_joomla_01-21-2010.sql.gz
./webbmaster_bellarose_joomla_01-20-2010.sql.gz
./webbmaster_bellarose_joomla_01-16-2010.sql.gz
./webbmaster_bellarose_joomla_12-31-2009.sql.gz
./webbmaster_bellarose_joomla_01-11-2010.sql.gz
./webbmaster_bellarose_joomla_01-05-2010.sql.gz
./webbmaster_bellarose_joomla_01-14-2010.sql.gz
./webbmaster_bellarose_joomla_01-22-2010.sql.gz
./webbmaster_bellarose_joomla_01-12-2010.sql.gz
./webbmaster_bellarose_joomla_01-17-2010.sql.gz
./webbmaster_bellarose_joomla_12-18-2009.sql.gz
./webbmaster_bellarose_joomla_12-20-2009.sql.gz
./webbmaster_bellarose_joomla_01-08-2010.sql.gz
./webbmaster_bellarose_joomla_12-22-2009.sql.gz
./webbmaster_bellarose_joomla_01-27-2010.sql.gz
./webbmaster_bellarose_joomla_12-23-2009.sql.gz
./webbmaster_bellarose_joomla_01-07-2010.sql.gz
./webbmaster_bellarose_joomla_12-26-2009.sql.gz
./webbmaster_bellarose_joomla_01-19-2010.sql.gz
./webbmaster_bellarose_joomla_12-29-2009.sql.gz
./webbmaster_bellarose_joomla_01-15-2010.sql.gz
./webbmaster_bellarose_joomla_01-28-2010.sql.gz
./webbmaster_bellarose_joomla_01-01-2010.sql.gz
./webbmaster_bellarose_joomla_01-18-2010.sql.gz
./webbmaster_bellarose_joomla_01-02-2010.sql.gz
./webbmaster_bellarose_joomla_01-23-2010.sql.gz
./webbmaster_bellarose_joomla_12-19-2009.sql.gz
Targets for purging (which I want to set in variable for simple rm -f $var)
./webbmaster_bellarose_joomla_01-09-2010.sql.gz
./webbmaster_bellarose_joomla_12-25-2009.sql.gz
./webbmaster_bellarose_joomla_01-10-2010.sql.gz
./webbmaster_bellarose_joomla_01-04-2010.sql.gz
./webbmaster_bellarose_joomla_12-30-2009.sql.gz
./webbmaster_bellarose_joomla_12-28-2009.sql.gz
./webbmaster_bellarose_joomla_01-06-2010.sql.gz
./webbmaster_bellarose_joomla_01-13-2010.sql.gz
./webbmaster_bellarose_joomla_12-21-2009.sql.gz
./webbmaster_bellarose_joomla_12-24-2009.sql.gz
./webbmaster_bellarose_joomla_12-27-2009.sql.gz
./webbmaster_bellarose_joomla_12-17-2009.sql.gz
./webbmaster_bellarose_joomla_01-03-2010.sql.gz
./webbmaster_bellarose_joomla_01-21-2010.sql.gz
./webbmaster_bellarose_joomla_01-20-2010.sql.gz
./webbmaster_bellarose_joomla_01-16-2010.sql.gz
./webbmaster_bellarose_joomla_12-31-2009.sql.gz
./webbmaster_bellarose_joomla_01-11-2010.sql.gz
./webbmaster_bellarose_joomla_01-05-2010.sql.gz
./webbmaster_bellarose_joomla_01-14-2010.sql.gz
./webbmaster_bellarose_joomla_01-22-2010.sql.gz
./webbmaster_bellarose_joomla_01-12-2010.sql.gz
./webbmaster_bellarose_joomla_01-17-2010.sql.gz
./webbmaster_bellarose_joomla_12-18-2009.sql.gz
./webbmaster_bellarose_joomla_12-20-2009.sql.gz
./webbmaster_bellarose_joomla_01-08-2010.sql.gz
./webbmaster_bellarose_joomla_12-22-2009.sql.gz
./webbmaster_bellarose_joomla_12-23-2009.sql.gz
./webbmaster_bellarose_joomla_01-07-2010.sql.gz
./webbmaster_bellarose_joomla_12-26-2009.sql.gz
./webbmaster_bellarose_joomla_01-19-2010.sql.gz
./webbmaster_bellarose_joomla_12-29-2009.sql.gz
./webbmaster_bellarose_joomla_01-15-2010.sql.gz
./webbmaster_bellarose_joomla_01-01-2010.sql.gz
./webbmaster_bellarose_joomla_01-18-2010.sql.gz
./webbmaster_bellarose_joomla_01-02-2010.sql.gz
./webbmaster_bellarose_joomla_12-19-2009.sql.gz
(dates 1/23 - 1/29 are left intact)