Last question...

In order to keep only a certain number of backups, what would be the easiest and SAFEST way to delete the older ones?

For example, I want to keep 17 hourlies, and 30 nightlies, so I have two cron jobs set up, the hourly, and the nightly. Each backs up to a separate dir.

I'm thinking the easiest way would be to find and delete the oldest file in the backup target directory before executing the backup command.

For the hourlies dir, I'd just find the files that are older than one day - so maybe:

find $BACKUP_DIR/hourly* -type f -mtime +1 -delete

Would that do it?

So, in my script, I could add this in like:

#!/bin/bash
BACKUP_DIR="/home/user/mypg_backups/hourly"
PGUSER="superuser"
PGyy=`date '+%Y'`
PGmm=`date '+%m'`
PGdd=`date '+%d_'`
PGtt=`date '+%H:%M'`
find $BACKUP_DIR* -type f -mtime +1 -delete
/usr/bin/pg_dumpall -U $PGUSER -o -f
$BACKUP_DIR/mypg-$PGyy-$PGmm-$PGdd$PGtt.gz

and for the nightly backup script, since I want to keep 30 days worth:

#!/bin/bash
BACKUP_DIR="/home/user/mypg_backups/nightly"
PGUSER="superuser"
PGyy=`date '+%Y'`
PGmm=`date '+%m'`
PGdd=`date '+%d_'`
PGtt=`date '+%H:%M'`
find $BACKUP_DIR* -type f -mtime +30 -delete
/usr/bin/pg_dumpall -U $PGUSER -o -f
$BACKUP_DIR/mypg-$PGyy-$PGmm-$PGdd$PGtt.gz

Am I asking for trouble doing it this way?

On 2013-05-05 5:56 PM, Tanstaafl <tansta...@libertytrek.org> wrote:
So, my final script looks like this:

#!/bin/bash
BACKUP_DIR="/home/user/mypg_backups"
PGUSER="superuser"
PGyy=`date '+%Y'`
PGmm=`date '+%m'`
PGdd=`date '+%d_'`
PGtt=`date '+%H:%M'`
/usr/bin/pg_dumpall -U $PGUSER -o -f
$BACKUP_DIR/mypg-$PGyy-$PGmm-$PGdd$PGtt.gz

For some reason, if I put the underscore in the filename itself, ie:

$BACKUP_DIR/mypg-$PGyy-$PGmm-$PGdd_$PGtt.gz

It omitted the $PGdd variable entirely.

I had to add the underscore into the variable to get the output like I
wanted. Weird...

Anyway, this is working perfectly, thanks guys.



Reply via email to