Monday, 12 August 2013

how to get basename in -exec of find?

how to get basename in -exec of find?

I cannot get the following piece of script (which is part of a larger
backup script) to work correctly:
BACKUPDIR=/BACKUP/db01/physical/incremental # Backups base directory
FULLBACKUPDIR=$BACKUPDIR/full # Full backups directory
INCRBACKUPDIR=$BACKUPDIR/incr # Incremental backups directory
KEEP=5 # Number of full backups (and its incrementals) to keep
...
FIRST_DELETE=`expr $KEEP + 1` # add one to the number of backups to keep,
this will be the first deleted
FILE0=`ls -ltr $FULLBACKUPDIR | awk '{print $9}' | tail -$FIRST_DELETE |
head -1` # search for the first backup to be deleted
...
find $FULLBACKUPDIR -maxdepth 1 -type d ! -newer $FULLBACKUPDIR/$FILE0
-execdir echo "removing: "$FULLBACKUPDIR/$(basename {}) \; -execdir bash
-c 'rm -rf $FULLBACKUPDIR/$(basename {})' \; -execdir echo "removing:
"$INCRBACKUPDIR/$(basename {}) \; -execdir bash -c 'rm -rf
$INCRBACKUPDIR/$(basename {})' \;
So the find works correctly which on its own will output something like this:
/BACKUPS/db01/physical/incremental/full/2013-08-12_17-51-28
/BACKUPS/db01/physical/incremental/full/2013-08-12_17-51-28
/BACKUPS/db01/physical/incremental/full/2013-08-12_17-25-07
What I want is the -exec to echo a line showing what is being removed and
then remove the folder from both directories.
I've tried various ways to get just the basename but nothing seems to be
working. I get this:
removing:
/BACKUPS/mysql/physical/incremental/full/"/BACKUPS/mysql/physical/incremental/full/2013-08-12_17-51-28"
removing:
/BACKUPS/mysql/physical/incremental/incr/"/BACKUPS/mysql/physical/incremental/full/2013-08-12_17-51-28"
removing:
/BACKUPS/mysql/physical/incremental/full/"/BACKUPS/mysql/physical/incremental/full/2013-08-12_17-25-07"
And of course the folders arn't deleted because they don't exist, just
fail silently because of the -f option. If I remove the -f I get the
'cannot be found' error on each rm.
How do I accomplish this? Because backups and parts of backups may be
stored across different storage systems I really need the ability to just
get the folder name for use in any known path.

No comments:

Post a Comment