bgbackup icon indicating copy to clipboard operation
bgbackup copied to clipboard

Limit the number of differential backups

Open ethaniel opened this issue 7 years ago • 2 comments

Hello!

I do 1 full backup every week. But I would also like to do differential backups every hour.

I would like to keep last 48 differential backups. But as far as I see from config, it's not possible yet.

I think this would be a good addition to the functionality of the script.

ethaniel avatar Apr 15 '17 18:04 ethaniel

Great idea. I'll work on adding this. Thanks.

bstillman avatar Apr 20 '17 01:04 bstillman

I assume it's easy as adding this to bgbackup.cnf:

# Number of Differential backups to keep
keepdiffnum=10

And this to backup_cleanup function in bgbackup.sh (I basically replaced the word "Full" to "Differential" and just copied the original cleanup code:

        limitoffset=$((keepdiffnum-1))
        delcountcmd=$mysqlcommand" \"SELECT COUNT(*) FROM $backuphistschema.backup_history WHERE end_time < (SELECT end_time FROM $backuphistschema.backup_history WHERE butype = 'Differential' ORDER BY end_time DESC LIMIT $limitoffset,1) AND butype = 'Differential' AND hostname = '$mhost' AND status = 'SUCCEEDED' AND deleted_at = 0\" "
        delcount=$(eval "$delcountcmd")
        if [ "$delcount" -gt 0 ]; then
            deletecmd=$mysqlcommand" \"SELECT bulocation FROM $backuphistschema.backup_history WHERE end_time < (SELECT end_time FROM $backuphistschema.backup_history WHERE butype = 'Differential' ORDER BY end_time DESC LIMIT $limitoffset,1) AND butype = 'Differential' AND hostname = '$mhost' AND status = 'SUCCEEDED' AND deleted_at = 0\" "
            eval "$deletecmd" | while read -r todelete; do
                log_info "Deleted backup $todelete"
                markdeletedcmd=$mysqlcommand" \"UPDATE $backuphistschema.backup_history SET deleted_at = NOW() WHERE bulocation = '$todelete' AND hostname = '$mhost' AND status = 'SUCCEEDED' \" "
                rm -Rf "$todelete"
                eval "$markdeletedcmd"
            done
        else
            log_info "No Differential backups to delete at this time."
        fi

ethaniel avatar May 16 '17 17:05 ethaniel