If you have GNU date, it allows to print the representation of an arbitrary date (-d option).
In this case convert the dates to seconds since EPOCH, subtract and divide by 24*3600.
Assume we rsync Oracle DB backups to a tertiary disk manually. Then we want to delete old backups on that disk. So here is a small bash script:
#!/bin/sh
for backup_dir in {'/backup/cmsprd/local/backupset','/backup/cmsprd/local/autobackup','/backup/cfprd/backupset','/backup/cfprd/autobackup'}
do
for f in `find $backup_dir -type d -regex '.*_.*_.*' -printf "%f\n"`
do
f2=`echo $f | sed -e 's/_//g'`
days=$(((`date "+%s"` - `date -d "${f2}" "+%s"`)/86400))
if [ $days -gt 30 ]; then
rm -rf $backup_dir/$f
fi
done
done
Modify the dirs and retention period ("30 days") to suit your needs.
Another Python version:
I'd submit another possible solution in Ruby. Looks like it's the be smallest and cleanest looking one so far:
Even if you don't have GNU date, you'll probably have Perl installed:
This returns
398.041666666667
-- 398 days and one hour due to daylight savings.The question came back up on my feed. Here's a more concise method using a Perl bundled module
Use the shell functions from http://cfajohnson.com/shell/ssr/ssr-scripts.tar.gz; they work in any standard Unix shell.
See the documentation at http://cfajohnson.com/shell/ssr/08-The-Dating-Game.shtml
If you have GNU
date
, it allows to print the representation of an arbitrary date (-d
option). In this case convert the dates to seconds since EPOCH, subtract and divide by 24*3600.Or you need a portable way?
Assume we rsync Oracle DB backups to a tertiary disk manually. Then we want to delete old backups on that disk. So here is a small bash script:
Modify the dirs and retention period ("30 days") to suit your needs.