Hugo/backups/create_vaultwarden_backup.sh

42 lines
1.4 KiB
Bash
Raw Normal View History

2023-04-23 22:11:24 +02:00
#!/bin/bash
2023-09-03 12:25:37 +02:00
# Backup script for Vaultwarden in a Kubernetes cluster
PROGRAM_NAME='backup_vaultwarden'
printlog () {
printf '%s: %s\n' "${PROGRAM_NAME}" "${1}"
}
2023-04-23 22:11:24 +02:00
2023-06-15 23:24:56 +02:00
BACKUP_DEST='/mnt/PRIVATE_DOCS/BACKUPS/vaultwarden'
PASSFILE='./vaultwarden_pass.txt'
2023-06-15 23:24:56 +02:00
# Create filename for database
2023-09-03 12:25:37 +02:00
database_backupfile="vaultwarden-sqlbkp_$( date +'%Y%m%d' ).bak"
2023-04-23 22:11:24 +02:00
# Retrieve container names
2023-06-15 23:24:56 +02:00
base_container="$( docker ps --format '{{.Names}}' | grep vaultwarden_vaultwarden )"
database_container="$( docker ps --format '{{.Names}}' | grep vaultwarden-postgresql_vaultwarden-postgresql )"
2023-04-23 22:11:24 +02:00
# Abort entire script if any command fails
set -e
# Database backup
2023-09-03 12:25:37 +02:00
printlog 'Backing up database'
2023-06-15 23:24:56 +02:00
internal_database_backupfile="/tmp/${database_backupfile}"
# Create backup file in docker container
docker exec --env-file "${PASSFILE}" "${database_container}" pg_dump 'vaultwarden' -cwv -h 'localhost' -U 'vaultwarden' -f "${internal_database_backupfile}"
# Copy backup outside container
docker cp "${database_container}":"${internal_database_backupfile}" "${BACKUP_DEST}"
2023-04-23 22:11:24 +02:00
# Files backup
for file in 'attachments' 'sends' 'config.json' 'rsa_key.pem' 'rsa_key.pub.pem'; do
2023-09-03 12:25:37 +02:00
printlog "$( printf 'Copying %s\n' "${file}" )"
2023-04-23 22:11:24 +02:00
docker cp -a "${base_container}":"/data/${file}" "${BACKUP_DEST}"
done
# Backup cleanup
# Only keep 30 days of backups, seems about right.
2023-09-03 12:25:37 +02:00
printlog 'Cleaning up old database backups'
2023-06-28 18:08:24 +02:00
find "${BACKUP_DEST}" -name '*sqlbkp*' -type f -mtime +30 -print -delete
2023-09-03 12:25:37 +02:00
printlog 'Done'