Improve vaultwarden backup shell scripting

This commit is contained in:
Tibo De Peuter 2023-06-28 17:41:46 +02:00
parent 4fe1ef2091
commit 1a1d6685b8

View file

@ -23,23 +23,14 @@ docker exec --env-file "${PASSFILE}" "${database_container}" pg_dump 'vaultwarde
docker cp "${database_container}":"${internal_database_backupfile}" "${BACKUP_DEST}"
# Files backup
files=('attachments' 'sends' 'config.json' 'rsa_key.pem' 'rsa_key.pub.pem')
for file in "${files[@]}"; do
>&2 echo "Copying ${file}"
for file in 'attachments' 'sends' 'config.json' 'rsa_key.pem' 'rsa_key.pub.pem'; do
>&2 printf 'Copying %s\n' "${file}"
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.
# Only keep 30 days of backups, seems about right.
>&2 echo 'Cleaning up old database backups'
pushd "${BACKUP_DEST}"
excess="$( ls -1 | grep sqlbkp | head -n -30 )"
if [ -n "${excess}" ]; then
>&2 echo "Removing ${excess}"
rm "${excess}"
else
>&2 echo 'Skipping: nothing to remove'
fi
popd
find "${BACKUP_DEST}" -name '*sqlbkp*' -type f -mtime +30 -print # -delete
>&2 echo 'Done'