From f1546487b457e040b0c0d1f708707a08dfcdacd8 Mon Sep 17 00:00:00 2001 From: Andrew Stowell Date: Mon, 27 Apr 2020 16:48:34 +0000 Subject: [PATCH] new prune-old-backups cron script. --- config.example.sh | 6 ++++++ crons/prune-old-backups.sh | 31 +++++++++++++++++++++++++++++++ 2 files changed, 37 insertions(+) create mode 100755 crons/prune-old-backups.sh diff --git a/config.example.sh b/config.example.sh index 8f2bd91..88e013b 100644 --- a/config.example.sh +++ b/config.example.sh @@ -10,6 +10,12 @@ declare -A STORAGE_ALERT # storage in GiB STORAGE_ALERT=(["/"]=5) +# when set to 0, all backups are kept +PRUNE_DOWN_TO=0 + +# the directory with backups to cleanup +PRUNE_DIRECTORY="/home/ubuntu/data" + SERVER_DOMAIN="localhost" DISCORD_SERVER_NAME="" diff --git a/crons/prune-old-backups.sh b/crons/prune-old-backups.sh new file mode 100755 index 0000000..a690fc0 --- /dev/null +++ b/crons/prune-old-backups.sh @@ -0,0 +1,31 @@ +#!/bin/bash + +LOCATION=`dirname "$0"` + +source "${LOCATION}/../config.sh" + +# TODO: The maximum number of backups to keep (when set to 0, all backups are kept) +maxNrOfBackups="$PRUNE_DOWN_TO" + +# TODO: The directory where you store the Nextcloud backups +backupMainDir="$PRUNE_DIRECTORY" + +# +# Delete old backups +# +if [ ${maxNrOfBackups} != 0 ] +then + nrOfBackups=$(ls -l ${backupMainDir} | grep -c ^d) + + if [[ ${nrOfBackups} > ${maxNrOfBackups} ]] + then + echo "Removing old backups..." + ls -t ${backupMainDir} | tail -$(( nrOfBackups - maxNrOfBackups )) | while read -r dirToRemove; do + echo "${dirToRemove}" + rm -r "${backupMainDir}/${dirToRemove:?}" + echo "Done" + echo + done + fi +fi +