Hugo/backups/create_vaultwarden_backup.sh

37 lines
1.3 KiB
Bash
Raw Normal View History

2023-04-23 22:11:24 +02:00
#!/bin/bash
# Backup script for Vaultwarden in a kubernetes cluster
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
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-06-15 23:24:56 +02:00
>&2 echo 'Backing up database'
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
>&2 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-06-15 23:24:56 +02:00
>&2 echo '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-06-15 23:24:56 +02:00
>&2 echo 'Done'