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 <chris@bigballofwax.co.nz>

Signed-off-by: Marcel de Rooy <m.de.rooy@rijksmuseum.nl>
Signed-off-by: Katrin Fischer <katrin.fischer@bsz-bw.de>
This commit is contained in:
Andreas Jonsson 2023-11-06 15:18:42 +01:00 committed by Katrin Fischer
parent 6341d520ac
commit b2e5efbdae
No known key found for this signature in database
GPG Key ID: 0EF6E2C03357A834
1 changed files with 14 additions and 35 deletions

View File

@ -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