prune-old-backups rewritten to work with optional command line args, and to work with files or folders.
This commit is contained in:
+6
-3
@@ -15,12 +15,15 @@ STORAGE_ALERT=(["/"]=5)
|
||||
|
||||
BACKUP_DIRECTORY="/home/ubuntu/data"
|
||||
|
||||
# when set to 0, all backups are kept
|
||||
PRUNE_DOWN_TO=0
|
||||
|
||||
# the directory with backups to cleanup
|
||||
PRUNE_DIRECTORY="/home/ubuntu/data"
|
||||
|
||||
# what type of backups to cleanup (what to find) "*.zip" etc, or "<DIR>"
|
||||
PRUNE_TYPE="<DIR>"
|
||||
|
||||
# when set to 0, all backups are kept
|
||||
PRUNE_DOWN_TO=0
|
||||
|
||||
SERVER_DOMAIN="localhost"
|
||||
|
||||
DISCORD_SERVER_NAME="<YOUR SERVER>"
|
||||
|
||||
+50
-18
@@ -1,31 +1,63 @@
|
||||
#!/bin/bash
|
||||
|
||||
# prune the directory of matching files or subfolders,
|
||||
# until we only have the desired number of backups remaining.
|
||||
# assumes backups are named with something like datestamp,
|
||||
# as we want to keep the bottom of the file list wheb sorted.
|
||||
|
||||
# pulls from config, but can take overrides:
|
||||
# prune-old-backups.sh "where-to-look" "what-to-find" "how-many-to-keep"
|
||||
|
||||
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"
|
||||
# directory to prune
|
||||
pruneDir="$PRUNE_DIRECTORY"
|
||||
|
||||
# TODO: The directory where you store the Nextcloud backups
|
||||
backupMainDir="$PRUNE_DIRECTORY"
|
||||
# what to prune from that directory
|
||||
pruneType="$PRUNE_TYPE"
|
||||
|
||||
#
|
||||
# Delete old backups
|
||||
#
|
||||
if [ ${maxNrOfBackups} != 0 ]
|
||||
then
|
||||
nrOfBackups=$(ls -l ${backupMainDir} | grep -c ^d)
|
||||
# how many to keep
|
||||
keepCount="$PRUNE_DOWN_TO"
|
||||
|
||||
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
|
||||
if [ -n "$1" ]; then
|
||||
pruneDir="$1"
|
||||
fi
|
||||
|
||||
if [ -n "$2" ]; then
|
||||
pruneType=""$2
|
||||
fi
|
||||
|
||||
if [ -n "$3" ]; then
|
||||
keepCount="$3"
|
||||
fi
|
||||
|
||||
if (( "${keepCount}" > "0" )); then
|
||||
removedSome=0
|
||||
|
||||
# find direct subfolders to remove
|
||||
if [ "${pruneType}" = "<DIR>" ]; then
|
||||
# notice the grep to remove the pruneDir from the results
|
||||
# head given a negative number will stop that much short from the bottom, so we keep those matches
|
||||
find "${pruneDir}" -maxdepth 1 -type d | grep -v "^${pruneDir}$" | sort | head -n "-${keepCount}" | while read -r dirToRemove; do
|
||||
removedSome=1
|
||||
echo "Removing: ${dirToRemove}"
|
||||
rm -r "${dirToRemove}"
|
||||
done
|
||||
else
|
||||
# head given a negative number will stop that much short from the bottom, so we keep those matches
|
||||
find "${pruneDir}" -type f -name "${pruneType}" | sort | head -n "-${keepCount}" | while read -r fileToRemove; do
|
||||
removedSome=1
|
||||
echo "Removing: ${fileToRemove}"
|
||||
rm "${fileToRemove}"
|
||||
done
|
||||
fi
|
||||
|
||||
if [ "${removedSome}" = "0" ]; then
|
||||
echo 'Nothing to do, we do not have too many backups.'
|
||||
fi
|
||||
else
|
||||
echo 'Nothing to do, you said keep everything.'
|
||||
fi
|
||||
|
||||
|
||||
Reference in New Issue
Block a user