From f666f547684472e709e1cc66a7398b00dbb99495 Mon Sep 17 00:00:00 2001 From: Andreas Jonsson Date: Mon, 6 Nov 2023 15:18:42 +0100 Subject: [PATCH] Bug 30627: Verify --days parameter and use find command to select old backups for deletion Test plan * Create some old fake backups: backuproot=/var/spool/koha instance=kohadev backupdir="$backuproot"/"$instance" for i in 1 2 3 4 ; do for j in sql tar xxx ; do file="$backupdir"/"$instance"-$(date -I -d "- $i day").${j}.gz if ! test -e "$file" ; then touch -t "$(date +%Y%m%d%H%M -d "- $i day")" "$file" fi done done * Verify that --days parameter is validated sudo koha-run-backups --days 0 sudo koha-run-backups --days foo * Run backup sudo koha-run-backups --days 3 * Verify that backups from 3 days have been preserved and older backups have been deleted * Verify that filenames that do not match the pattern (the .xxx.gz files) are preserved Signed-off-by: Chris Cormack --- debian/scripts/koha-run-backups | 49 ++++++++++----------------------- 1 file changed, 14 insertions(+), 35 deletions(-) diff --git a/debian/scripts/koha-run-backups b/debian/scripts/koha-run-backups index 1433c06ba1..a9223844fe 100755 --- a/debian/scripts/koha-run-backups +++ b/debian/scripts/koha-run-backups @@ -60,44 +60,23 @@ while true ; do esac done +if ! test $days -gt 0 ; then + echo "Parameter --days must be an integer greater than 0" + exit 1 +fi + for name in $(koha-list --enabled | grep -Fxv demo) do - koha-dump ${exclude_indexes} "$name" > /dev/null - if [ -z "$dirname" ]; then - backupdir="$( xmlstarlet sel -t -v 'yazgfs/config/backupdir' /etc/koha/sites/$name/koha-conf.xml )"; - else - backupdir="$dirname/$name"; - fi + if koha-dump ${exclude_indexes} "$name" > /dev/null; then + # Only delete old backups if dump script return success. - # Remove old dump files. - # FIXME: This could probably be replaced by one line of perl. - ls "$backupdir/" | - sed "s:^$name-\([0-9-]*\)\.\(sql\|tar\)\.gz$:\1:" | - sort -u | - tac | - sed "1,${days}d" | - tac | - while read date - do - tardump="$backupdir/$name-$date.tar.gz" - sqldump="$backupdir/$name-$date.sql.gz" - if [ -e "$tardump" ] && [ -e "$sqldump" ] - then - rm "$tardump" - rm "$sqldump" - elif [ -e "$tardump" ] || [ -e "$sqldump" ] - then - echo "Only one of a pair exists! Not removing it." - for x in "$tardump" "$sqldump" - do - if [ -e "$x" ] - then - echo "Exists : $x" - else - echo "Does not exist: $x" - fi - done + if [ -z "$dirname" ]; then + backupdir="$( xmlstarlet sel -t -v 'yazgfs/config/backupdir' /etc/koha/sites/$name/koha-conf.xml )"; + else + backupdir="$dirname/$name"; fi - done + + find $backupdir -maxdepth 1 \( -mtime +$days -or -mtime $days \) -name $name-'[1-9][0-9][0-9][0-9]-[0-9][0-9]-[0-9][0-9].???'.gz \( -name \*.sql.gz -or -name \*.tar.gz \) -print0 | xargs -0 -r rm + fi done -- 2.30.2