#1 Updated vaultwarden script to keep only 30 backups

This commit is contained in:
Tibo De Peuter 2023-06-15 13:08:05 +02:00
parent c07961a4c8
commit 5e9c23ff05

View file

@ -2,7 +2,9 @@
# Backup script for Vaultwarden in a kubernetes cluster
BACKUP_DEST="/mnt/PRIVATE_DOCS/BACKUPS/vaultwarden"
PASSFILE=./vaultwarden_pass.txt
PASSFILE="./vaultwarden_pass.txt"
BACKUP_FILENAME=vaultwarden-sqlbkp_`date +"%Y%m%d"`.bak
# Retrieve container names
base_container=$( docker ps --format "{{.Names}}" | grep vaultwarden_vaultwarden )
@ -13,8 +15,9 @@ set -e
# Database backup
>&2 echo "Backing up database"
docker exec --env-file "${PASSFILE}" "${database_container}" pg_dump "vaultwarden" -cwv -h "localhost" -U "vaultwarden" -f /tmp/vaultwarden-sqlbkp_`date +"%Y%m%d"`.bak
docker cp "${database_container}":/tmp/vaultwarden-sqlbkp_$(date +"%Y%m%d").bak "${BACKUP_DEST}"
internal_backup="/tmp/${BACKUP_FILENAME}"
docker exec --env-file "${PASSFILE}" "${database_container}" pg_dump "vaultwarden" -cwv -h "localhost" -U "vaultwarden" -f "${internal_backup}"
docker cp "${database_container}":"${internal_backup}" "${BACKUP_DEST}"
# Files backup
files=("attachments" "sends" "config.json" "rsa_key.pem" "rsa_key.pub.pem")
@ -23,4 +26,13 @@ for file in "${files[@]}"; do
docker cp -a "${base_container}":"/data/${file}" "${BACKUP_DEST}"
done
# Backup cleanup
# Only keep the 30 most recent backups => probably a month worth of backups, seems about right.
pushd "${BACKUP_DEST}"
rm $( ls -l |
grep sqlbkp | # Only cleanup database backups
sed -E 's/.*\s([a-z0-9_-]+\.bak)$/\1/' | # Take file part
head -n -30 ) # Keep 30 backups
popd
>&2 echo "Done"