Fixed rsa.pub.pem name.

Added db backup cleanup.
Added db backup file to the rsync list so that it actually gets backed up.
This commit is contained in:
2026-09-21 20:21:12 -05:00
parent 48b84da2c6
commit 1d004fadad
+47 -47
View File
@@ -6,26 +6,21 @@
# mounted drive using rsync. It includes pre-run checks to ensure
# the source files and destination are valid, and sends ntfy
# notifications for each task.
# Usage: sudo ./hass_backup.sh
# Usage: sudo ./vaultwarden_backup.sh
# Notes: This script may require sudo/root privileges to access files.
# ==============================================================================
# ==============================================================================
# Define Variables for Source and Destination
# ==============================================================================
# This makes the script easier to read and modify.
BACKUP_MOUNT_POINT="/mnt/5TB-Disk1"
BACKUP_DESTINATION="$BACKUP_MOUNT_POINT/backup"
# Vaultwarden backup directories
VAULTWARDEN_SOURCE_DIR="/srv/docker/vaultwarden/data/"
VAULTWARDEN_SOURCE_DIR="/srv/docker/vaultwarden/data"
VAULTWARDEN_DESTINATION="$BACKUP_MOUNT_POINT/backup/services/vaultwarden"
# ntfy Notification Configuration
# To use this feature, you must have the 'curl' command installed.
# NTFY_TOPIC is the topic you wish to send notifications to.
# NTFY_SERVER is the URL of your ntfy server.
# NTFY_TOKEN is the authorization token for your ntfy server.
NTFY_TOPIC="Server"
NTFY_SERVER="https://ntfy.speerfam.net"
NTFY_TOKEN="tk_3xo9mejjtgemyowzckqw2souo3mux"
@@ -33,51 +28,12 @@ NTFY_TOKEN="tk_3xo9mejjtgemyowzckqw2souo3mux"
# ==============================================================================
# Pre-flight Checks (for the destination drive)
# ==============================================================================
# Check if the mount point directory exists and is a mounted filesystem.
# This is a critical check to ensure we are writing to the intended air-gapped
# drive and not to a local directory if the mount failed.
if ! mountpoint -q "$BACKUP_MOUNT_POINT"; then
echo "Error: '$BACKUP_MOUNT_POINT' is not a mount point. Please ensure the drive is mounted." >&2
# Send a failure notification
curl -H "Authorization: Bearer $NTFY_TOKEN" -d "Error: Script failed. Destination '$BACKUP_MOUNT_POINT' is not mounted." "$NTFY_SERVER/$NTFY_TOPIC" > /dev/null 2>&1
exit 1
fi
# Check if the destination directories exist and create them if they don't.
# This prevents rsync from failing on the first run.
# mkdir -p "$BACKUP_DESTINATION"
# mkdir -p "$PHOTOS_DESTINATION"
# ==============================================================================
# Vaultwarden backup
# ==============================================================================
echo "Starting Vaultwarden backup process..."
# Step 1: Execute the Docker command to create the database backup.
echo "Running Vaultwarden backup via Docker..."
sudo docker exec -it vaultwarden /vaultwarden backup
if [ $? -ne 0 ]; then
echo "Error: Docker command failed to create Vaultwarden database backup." >&2
curl -H "Authorization: Bearer $NTFY_TOKEN" -d "Error: Docker command failed to create Vaultwarden database backup." "$NTFY_SERVER/$NTFY_TOPIC" > /dev/null 2>&1
exit 1
fi
echo "Vaultwarden database backup created successfully."
curl -H "Authorization: Bearer $NTFY_TOKEN" -d "Vaultwarden database backup created. Starting rsync." "$NTFY_SERVER/$NTFY_TOPIC" > /dev/null 2>&1
# Step 2: Rsync the Vaultwarden directories and files to the destination.
echo "Starting rsync of Vaultwarden data to '$VAULTWARDEN_DESTINATION'..."
# Define the Vaultwarden source files and directories to be backed up
# This approach makes the script more readable and scalable.
VAULTWARDEN_SOURCES=(
"$VAULTWARDEN_SOURCE_DIR/attachments"
"$VAULTWARDEN_SOURCE_DIR/sends"
"$VAULTWARDEN_SOURCE_DIR/rsa_key.pem"
"$VAULTWARDEN_SOURCE_DIR/rsa_key.pub"
"$VAULTWARDEN_SOURCE_DIR/icon_cache"
)
# Check if the Vaultwarden source directory exists.
if [ ! -d "$VAULTWARDEN_SOURCE_DIR" ]; then
echo "Error: Vaultwarden backup source directory '$VAULTWARDEN_SOURCE_DIR' not found." >&2
@@ -85,9 +41,48 @@ if [ ! -d "$VAULTWARDEN_SOURCE_DIR" ]; then
exit 1
fi
# ==============================================================================
# Vaultwarden backup
# ==============================================================================
echo "Starting Vaultwarden backup process..."
# Step 1: Clean up any existing database dump files before generating a new one
echo "Cleaning up old local database backup dumps..."
rm -f "$VAULTWARDEN_SOURCE_DIR"/db_*.sqlite3
# Step 2: Execute the Docker command to create the new database backup.
# Note: Removed '-it' flags so this executes properly under automated cron jobs.
echo "Running Vaultwarden backup via Docker..."
sudo docker exec vaultwarden /vaultwarden backup
if [ $? -ne 0 ]; then
echo "Error: Docker command failed to create Vaultwarden database backup." >&2
curl -H "Authorization: Bearer $NTFY_TOKEN" -d "Error: Docker command failed to create Vaultwarden database backup." "$NTFY_SERVER/$NTFY_TOPIC" > /dev/null 2>&1
exit 1
fi
echo "Vaultwarden database backup created successfully."
curl -H "Authorization: Bearer $NTFY_TOKEN" -d "Vaultwarden database backup created. Starting rsync." "$NTFY_SERVER/$NTFY_TOPIC" > /dev/null 2>&1
# Step 3: Rsync the Vaultwarden directories and files to the destination.
echo "Starting rsync of Vaultwarden data to '$VAULTWARDEN_DESTINATION'..."
# Define source files/directories including the newly generated database dump file
VAULTWARDEN_SOURCES=(
"$VAULTWARDEN_SOURCE_DIR"/db_*.sqlite3
"$VAULTWARDEN_SOURCE_DIR/attachments"
"$VAULTWARDEN_SOURCE_DIR/sends"
"$VAULTWARDEN_SOURCE_DIR/rsa_key.pem"
"$VAULTWARDEN_SOURCE_DIR/rsa_key.pub.pem"
"$VAULTWARDEN_SOURCE_DIR/icon_cache"
)
# Ensure destination directory exists
mkdir -p "$VAULTWARDEN_DESTINATION"
# Loop through each source and rsync it.
for source in "${VAULTWARDEN_SOURCES[@]}"; do
rsync -av --progress "$source" "$VAULTWARDEN_DESTINATION"
if [ -e "$source" ]; then
rsync -av --no-owner --no-group "$source" "$VAULTWARDEN_DESTINATION"
if [ $? -eq 0 ]; then
echo "Success: '$source' copied to '$VAULTWARDEN_DESTINATION'."
else
@@ -95,8 +90,13 @@ for source in "${VAULTWARDEN_SOURCES[@]}"; do
curl -H "Authorization: Bearer $NTFY_TOKEN" -d "Error: rsync failed to copy Vaultwarden data." "$NTFY_SERVER/$NTFY_TOPIC" > /dev/null 2>&1
exit 1
fi
fi
done
# Step 4: Clean up local database backup dump file to conserve space on primary disk
echo "Cleaning up temporary database backup dump from source directory..."
rm -f "$VAULTWARDEN_SOURCE_DIR"/db_*.sqlite3
echo "Success: Vaultwarden backup has been successfully copied to '$VAULTWARDEN_DESTINATION'."
curl -H "Authorization: Bearer $NTFY_TOKEN" -d "Success: Vaultwarden backup copied to $VAULTWARDEN_DESTINATION" "$NTFY_SERVER/$NTFY_TOPIC" > /dev/null 2>&1