Doubling down on safety in bash

This commit is contained in:
Tibo De Peuter 2023-06-15 23:24:56 +02:00
parent 41de09bb97
commit 4fe1ef2091
2 changed files with 43 additions and 22 deletions

View file

@ -1,19 +1,33 @@
#!/bin/bash #!/bin/bash
# Backup script for Calibre-web in a kubernetes cluster # Backup script for Calibre-web in a kubernetes cluster
BACKUP_DEST="/mnt/PRIVATE_DOCS/BACKUPS/calibre-web" BACKUP_DEST='/mnt/PRIVATE_DOCS/BACKUPS/calibre-web'
DATABASE_FILE="/config/app.db" DATABASE_FILE='/config/app.db'
backup_filename="calibre-web-app_$(date +'%Y%m%d').db" # Create filename for database backup
database_backupfile="calibre-web-app_$(date +'%Y%m%d').db"
# Retrieve container name # Retrieve container name
base_container=$( docker ps --format "{{.Names}}" | grep tkioskje-calibre-web_tkioskje-calibre-web ) base_container="$( docker ps --format '{{.Names}}' | grep tkioskje-calibre-web_tkioskje-calibre-web )"
# Abort entire script if any command fails # Abort entire script if any command fails
set -e set -e
# Database backup # Database backup
>&2 echo "Backing up database" >&2 echo 'Backing up database'
docker cp "${base_container}":"${DATABASE_FILE}" "${BACKUP_DEST}/${backup_filename}" docker cp "${base_container}":"${DATABASE_FILE}" "${BACKUP_DEST}/${database_backupfile}"
>&2 echo "Done" # Backup cleanup
# Only keep 30 most recent backups
>&2 echo 'Cleaning up old database backups'
pushd "${BACKUP_DEST}"
excess="$( ls -x | head -n -30 )"
if [ -n "${excess}" ]; then
>&2 echo "Removing ${excess}"
rm "${excess}"
else
>&2 echo 'Skipping: nothing to remove'
fi
popd
>&2 echo 'Done'

View file

@ -1,26 +1,29 @@
#!/bin/bash #!/bin/bash
# Backup script for Vaultwarden in a kubernetes cluster # Backup script for Vaultwarden in a kubernetes cluster
BACKUP_DEST="/mnt/PRIVATE_DOCS/BACKUPS/vaultwarden" BACKUP_DEST='/mnt/PRIVATE_DOCS/BACKUPS/vaultwarden'
PASSFILE="./vaultwarden_pass.txt" PASSFILE='./vaultwarden_pass.txt'
BACKUP_FILENAME=vaultwarden-sqlbkp_`date +"%Y%m%d"`.bak # Create filename for database
database_backupfile="vaultwarden-sqlbkp_$(date +'%Y%m%d').bak"
# Retrieve container names # Retrieve container names
base_container=$( docker ps --format "{{.Names}}" | grep vaultwarden_vaultwarden ) base_container="$( docker ps --format '{{.Names}}' | grep vaultwarden_vaultwarden )"
database_container=$( docker ps --format "{{.Names}}" | grep vaultwarden-postgresql_vaultwarden-postgresql ) database_container="$( docker ps --format '{{.Names}}' | grep vaultwarden-postgresql_vaultwarden-postgresql )"
# Abort entire script if any command fails # Abort entire script if any command fails
set -e set -e
# Database backup # Database backup
>&2 echo "Backing up database" >&2 echo 'Backing up database'
internal_backup="/tmp/${BACKUP_FILENAME}" internal_database_backupfile="/tmp/${database_backupfile}"
docker exec --env-file "${PASSFILE}" "${database_container}" pg_dump "vaultwarden" -cwv -h "localhost" -U "vaultwarden" -f "${internal_backup}" # Create backup file in docker container
docker cp "${database_container}":"${internal_backup}" "${BACKUP_DEST}" 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}"
# Files backup # Files backup
files=("attachments" "sends" "config.json" "rsa_key.pem" "rsa_key.pub.pem") files=('attachments' 'sends' 'config.json' 'rsa_key.pem' 'rsa_key.pub.pem')
for file in "${files[@]}"; do for file in "${files[@]}"; do
>&2 echo "Copying ${file}" >&2 echo "Copying ${file}"
docker cp -a "${base_container}":"/data/${file}" "${BACKUP_DEST}" docker cp -a "${base_container}":"/data/${file}" "${BACKUP_DEST}"
@ -28,11 +31,15 @@ done
# Backup cleanup # Backup cleanup
# Only keep the 30 most recent backups => probably a month worth of backups, seems about right. # Only keep the 30 most recent backups => probably a month worth of backups, seems about right.
>&2 echo 'Cleaning up old database backups'
pushd "${BACKUP_DEST}" pushd "${BACKUP_DEST}"
rm $( ls -l | excess="$( ls -1 | grep sqlbkp | head -n -30 )"
grep sqlbkp | # Only cleanup database backups if [ -n "${excess}" ]; then
sed -E 's/.*\s([a-z0-9_-]+\.bak)$/\1/' | # Take file part >&2 echo "Removing ${excess}"
head -n -30 ) # Keep 30 backups rm "${excess}"
else
>&2 echo 'Skipping: nothing to remove'
fi
popd popd
>&2 echo "Done" >&2 echo 'Done'