vaultwarden-backup/vaultwarden-backup.sh

53 lines
1.4 KiB
Bash
Raw Normal View History

#!/usr/bin/env bash
2021-04-29 13:47:12 +02:00
2022-04-19 00:05:30 +02:00
# All executed commands are printed to stdout
2021-04-29 13:47:12 +02:00
set -x
2022-04-19 00:05:30 +02:00
# Redirect stdout (and stderr to stdout) to a file
exec 1>/var/log/backup/vaultwarden-backup/vaultwarden-backup-"$(date +%F)".log 2>&1
2021-04-29 13:47:12 +02:00
2022-04-19 00:05:30 +02:00
# Abort on nonzero exit status
2021-04-29 13:47:12 +02:00
set -o errexit
2022-04-19 00:05:30 +02:00
# Abort on unbound variable
2021-04-29 13:47:12 +02:00
set -o nounset
2022-04-19 00:05:30 +02:00
# Don't hide errors within pipes
2021-04-29 13:47:12 +02:00
set -o pipefail
2022-11-23 10:19:47 +01:00
# set your API key here
2021-04-29 13:47:12 +02:00
key=
2022-11-23 10:19:47 +01:00
# set your chat id here
2021-04-29 13:47:12 +02:00
chat_id=
function send_message() {
if ! [ $# -eq 0 ]; then
2022-04-19 00:05:30 +02:00
curl --silent --show-error --fail --request POST "https://api.telegram.org/$key/sendMessage" --data chat_id="$chat_id" --data text="$1" --output /dev/null
2022-11-23 10:19:47 +01:00
else
echo "No argument supplied, please specify the message to send"
exit 1
2022-04-19 00:05:30 +02:00
fi
2021-04-29 13:47:12 +02:00
}
function backup_vaultwarden {
2022-04-19 00:05:30 +02:00
# Retrieve the date in YYYY-MM-DD format and the timestamp in H-M-S format
backup_time=$(date +%Y-%m-%d_%I-%M-%S)
# Set the destination directory for backups
destination_directory=/var/local/vaultwarden/backups/${backup_time}
# Create the destination directory
mkdir --parents "$destination_directory"
# Save the SQLite 3 database
if ! sqlite3 /var/lib/vaultwarden/data/db.sqlite3 ".backup $destination_directory/backup.sqlite3"; then
2022-04-19 00:05:30 +02:00
send_message "[Backup] - Error during Vaultwarden backup, please log in as soon as possible and see what went wrong"
exit 1
2022-04-19 00:05:30 +02:00
fi
send_message "[Backup] - Vaultwarden has just been backed up"
2021-04-29 13:47:12 +02:00
}
backup_vaultwarden