This commit is contained in:
ilguappo 2024-08-14 16:44:00 -07:00
commit 9183173480

39
backup.sh Executable file
View File

@ -0,0 +1,39 @@
# A script to perform incremental backups using rsync
set -o errexit
set -o nounset
set -o pipefail
readonly KEEP="$3"
readonly SOURCE_DIR="$1"
readonly BACKUP_DIR="$2"
readonly DATETIME="$(date '+%Y-%m-%d_%H:%M:%S')"
readonly BACKUP_PATH="${BACKUP_DIR}/${DATETIME}"
readonly LATEST_LINK="$(dirname ${BACKUP_DIR})/latest"
mkdir -p "${BACKUP_DIR}"
rsync -avh --delete \
"${SOURCE_DIR}/" \
--link-dest "${LATEST_LINK}" \
--exclude=".cache" \
"${BACKUP_PATH}"
rm -rf "${LATEST_LINK}"
ln -s "${BACKUP_PATH}" "${LATEST_LINK}"
COUNT=$(ls ${BACKUP_DIR} | wc -l)
if [[ ${KEEP} -lt ${COUNT} ]]; then
TRASH=$(ls ${BACKUP_DIR} | sort -r | tail -n $(( ${COUNT} - ${KEEP} )) )
for t in ${TRASH}; do
rm -rf "${BACKUP_DIR}/${t}"
echo "REMOVED: ${BACKUP_DIR}/${t}"
done
fi