Refactor code to make linter runs succeed
All checks were successful
Lint Bash Scripts / lint-bash (push) Successful in 28s
Check Commit Messages / check-commit-message (pull_request) Successful in 14s
Lint Bash Scripts / lint-bash (pull_request) Successful in 21s

This commit refactors the codebase in order to make the shellcheck
workflow pass.
This commit is contained in:
ZoopaMario 2024-12-29 15:08:09 +01:00
parent 6a4d145ac7
commit aaf9ecfc1d
9 changed files with 95 additions and 157 deletions

View file

@ -18,14 +18,27 @@ jobs:
- name: Checkout Code - name: Checkout Code
uses: https://code.forgejo.org/actions/checkout@v4 uses: https://code.forgejo.org/actions/checkout@v4
with: with:
fetch-depth: 0 # Fetch the entire history to avoid REST API reliance fetch-depth: 0
- name: Install ShellCheck - name: Install ShellCheck
run: | run: |
apt-get update && apt-get install -y shellcheck apt-get update && apt-get install -y shellcheck
- name: Lint Bash Scripts - name: Find Shell Scripts
id: find-scripts
run: | run: |
echo "Linting Bash scripts..." echo "Searching for shell scripts..."
find . -name '*.sh' -print0 | xargs -0 shellcheck find . -name '*.sh' -print0 > shell-scripts.list
- name: Run ShellCheck in Script Directories
run: |
echo "Running ShellCheck in each script's directory..."
while IFS= read -r -d '' script; do
echo "Checking $script"
script_dir=$(dirname "$script")
script_name=$(basename "$script")
# Change to the script's directory and run ShellCheck
(cd "$script_dir" && shellcheck -x "$script_name")
done < shell-scripts.list

View file

@ -1,24 +1,14 @@
#!/bin/bash #!/bin/bash
SCRIPT_ABS_LOCATION=$(realpath "$(dirname "${0}")") SCRIPT_ABS_LOCATION=$(realpath "$(dirname "${0}")")
source $SCRIPT_ABS_LOCATION/cryptpad-backup.env source "$SCRIPT_ABS_LOCATION/cryptpad-backup.env"
source $SCRIPT_ABS_LOCATION/../logger.sh source "$SCRIPT_ABS_LOCATION/../logger.sh"
# Container Names # Container Names
CONTAINER="cryptpad" CONTAINER="cryptpad"
BACKUP_CONTAINER="duplicati" BACKUP_CONTAINER="duplicati"
SOURCE_DIR="/mnt/data/cryptpad" SOURCE_DIR="/mnt/data/cryptpad"
BACKUP_DESTINATION="$BACKUP_DESTINATION"
BACKUP_ENCR_PASSPHRASE="$BACKUP_ENCR_PASSPHRASE"
# Cloud Storage Authentication
SFTP_USERNAME="$SFTP_USERNAME"
SFTP_PASSWORD="$SFTP_PASSWORD"
SFTP_FINGERPRINT="$SFTP_FINGERPRINT"
# Log file
LOG_FILE="$SCRIPT_ABS_LOCATION/cryptpad-backup.log"
# Cleanup inactive and archive files # Cleanup inactive and archive files
log "Cleaning up inactive accounts and files to save space..." log "Cleaning up inactive accounts and files to save space..."
@ -33,20 +23,20 @@ docker exec cryptpad \
# Backup all files to target destination # Backup all files to target destination
log "Backing up cryptpad files and database..." log "Backing up cryptpad files and database..."
docker exec $BACKUP_CONTAINER \ docker exec "$BACKUP_CONTAINER" \
duplicati-cli backup ssh://$BACKUP_DESTINATION \ duplicati-cli backup "ssh://$BACKUP_DESTINATION" \
$SOURCE_DIR/data \ "$SOURCE_DIR/data" \
$SOURCE_DIR/datastore \ "$SOURCE_DIR/datastore" \
$SOURCE_DIR/block \ "$SOURCE_DIR/block" \
$SOURCE_DIR/blob \ "$SOURCE_DIR/blob" \
$SOURCE_DIR/config/config.js \ "$SOURCE_DIR/config/config.js" \
$SOURCE_DIR/customize \ "$SOURCE_DIR/customize" \
$SOURCE_DIR/customize.dist \ "$SOURCE_DIR/customize.dist" \
--backup-name="$CONTAINER backup" \ --backup-name="$CONTAINER backup" \
--keep-versions=7 \ --keep-versions=7 \
--auth-username=$SFTP_USERNAME \ --auth-username="$SFTP_USERNAME" \
--auth-password=$SFTP_PASSWORD \ --auth-password="$SFTP_PASSWORD" \
--passphrase=$BACKUP_ENCR_PASSPHRASE \ --passphrase="$BACKUP_ENCR_PASSPHRASE" \
--ssh-fingerprint="$SFTP_FINGERPRINT" \ --ssh-fingerprint="$SFTP_FINGERPRINT" \
--prefix="cryptpad" || { log "Error: Failed to backup cryptpad files and database."; exit 1; } --prefix="cryptpad" || { log "Error: Failed to backup cryptpad files and database."; exit 1; }
log "cryptpad backup completed successfully." log "cryptpad backup completed successfully."

View file

@ -1,35 +1,22 @@
#!/bin/bash #!/bin/bash
SCRIPT_ABS_LOCATION=$(realpath "$(dirname "${0}")") SCRIPT_ABS_LOCATION=$(realpath "$(dirname "${0}")")
source $SCRIPT_ABS_LOCATION/duplicati-backup.env source "$SCRIPT_ABS_LOCATION/duplicati-backup.env"
source $SCRIPT_ABS_LOCATION/../logger.sh source "$SCRIPT_ABS_LOCATION/../logger.sh"
# Container Name
CONTAINER="duplicati"
SOURCE_DIR="/mnt/data/duplicati" SOURCE_DIR="/mnt/data/duplicati"
SSH_DESTINATION="$SSH_DESTINATION"
BACKUP_ENCR_PASSPHRASE="$BACKUP_ENCR_PASSPHRASE"
# Cloud Storage Authentication
SSH_USERNAME="$SSH_USERNAME"
SSH_PASSWORD="$SSH_PASSWORD"
SSH_KEY="$SSH_KEY"
# Set default values for parameters # Set default values for parameters
MAX_BACKUPS=7 MAX_BACKUPS=7
BACKUP_FOLDER="" BACKUP_FOLDER=""
# Log file
LOG_FILE="$SCRIPT_ABS_LOCATION/duplicati-backup.log"
# Override default values with command-line arguments # Override default values with command-line arguments
while getopts ":f:h:u:p:m:b:" opt; do while getopts ":f:h:u:p:m:b:" opt; do
case $opt in case $opt in
f) SOURCE_DIR="$OPTARG";; f) SOURCE_DIR="$OPTARG";;
h) SSH_DESTINATION="$OPTARG";; h) SSH_DESTINATION="$OPTARG";;
u) SSH_USERNAME="$OPTARG";; u) SSH_USERNAME="$OPTARG";;
p) SSH_PASSWORD="$OPTARG";; p) SSH_KEY="$OPTARG";;
m) MAX_BACKUPS="$OPTARG";; m) MAX_BACKUPS="$OPTARG";;
b) BACKUP_FOLDER="$OPTARG";; b) BACKUP_FOLDER="$OPTARG";;
\?) echo "Invalid option: -$OPTARG"; exit 1;; \?) echo "Invalid option: -$OPTARG"; exit 1;;
@ -37,22 +24,23 @@ while getopts ":f:h:u:p:m:b:" opt; do
done done
# Create a temporary file for the encrypted archive # Create a temporary file for the encrypted archive
# shellcheck disable=SC2015
TMP_FILENAME=duplicati_db-$(date +"%Y%m%d").bak && TMP_FILENAME=duplicati_db-$(date +"%Y%m%d").bak &&
TMP_FILEPATH=$SCRIPT_ABS_LOCATION || { log "Error: Failed to create a temporary file."; exit 1; } TMP_FILEPATH="$SCRIPT_ABS_LOCATION" || { log "Error: Failed to create a temporary file."; exit 1; }
# Create the encrypted archive using tar and openssl # Create the encrypted archive using tar and openssl
log "Compressing and excrypting the Duplicati Databases" log "Compressing and excrypting the Duplicati Databases"
sudo tar -czf - "$SOURCE_DIR" | openssl enc -aes-256-cbc -pbkdf2 -pass pass:$BACKUP_ENCR_PASSPHRASE > $TMP_FILEPATH/$TMP_FILENAME || { log "Error: Failed to create encrypted archive."; exit 1; } sudo tar -czf - "$SOURCE_DIR" | openssl enc -aes-256-cbc -pbkdf2 -pass "pass:$BACKUP_ENCR_PASSPHRASE" > "$TMP_FILEPATH/$TMP_FILENAME" || { log "Error: Failed to create encrypted archive."; exit 1; }
# Connect to the backup host and count the number of existing backups # Connect to the backup host and count the number of existing backups
log "Fetching number of backups in destination folder" log "Fetching number of backups in destination folder"
EXISTING_BACKUPS=$(ssh $SSH_USERNAME@$SSH_DESTINATION -p 23 -i $SSH_KEY "ls" | sudo wc -l) || { log "Error: Failed to count existing backups."; exit 1; } EXISTING_BACKUPS=$(ssh "$SSH_USERNAME@$SSH_DESTINATION" -p 23 -i "$SSH_KEY ls" | sudo wc -l) || { log "Error: Failed to count existing backups."; exit 1; }
# Remove old backups if there are too many # Remove old backups if there are too many
if (( $EXISTING_BACKUPS > $MAX_BACKUPS )); then if (( EXISTING_BACKUPS > MAX_BACKUPS )); then
log "Removing old backups in order to save space" log "Removing old backups in order to save space"
ssh $SSH_USERNAME@$SSH_DESTINATION -p 23 -i $SSH_KEY "ls -t $BACKUP_FOLDER | tail -n +$((MAX_BACKUPS+1)) | xargs rm" || { log "Error: Failed to remove old backups."; exit 1; } ssh "$SSH_USERNAME@$SSH_DESTINATION" -p 23 -i "$SSH_KEY ls -t $BACKUP_FOLDER | tail -n +$((MAX_BACKUPS+1)) | xargs rm" || { log "Error: Failed to remove old backups."; exit 1; }
fi fi
# Transfer the encrypted archive to the backup host using scp # Transfer the encrypted archive to the backup host using scp

View file

@ -1,24 +1,14 @@
#!/bin/bash #!/bin/bash
SCRIPT_ABS_LOCATION=$(realpath "$(dirname "${0}")") SCRIPT_ABS_LOCATION=$(realpath "$(dirname "${0}")")
source $SCRIPT_ABS_LOCATION/forgejo-backup.env source "$SCRIPT_ABS_LOCATION/forgejo-backup.env"
source $SCRIPT_ABS_LOCATION/../logger.sh source "$SCRIPT_ABS_LOCATION/../logger.sh"
# Container Names # Container Names
APP_CONTAINER="forgejo" APP_CONTAINER="forgejo"
BACKUP_CONTAINER="duplicati" BACKUP_CONTAINER="duplicati"
SOURCE_DIR="/mnt/data/forgejo" SOURCE_DIR="/mnt/data/forgejo"
BACKUP_DESTINATION="$BACKUP_DESTINATION"
BACKUP_ENCR_PASSPHRASE="$BACKUP_ENCR_PASSPHRASE"
# Cloud Storage Authentication
SFTP_USERNAME="$SFTP_USERNAME"
SFTP_PASSWORD="$SFTP_PASSWORD"
SFTP_FINGERPRINT="$SFTP_FINGERPRINT"
# Log file
LOG_FILE="$SCRIPT_ABS_LOCATION/forgejo-backup.log"
# Stop forgejo container # Stop forgejo container
log "Stopping forgejo container..." log "Stopping forgejo container..."
@ -28,18 +18,18 @@ docker stop $APP_CONTAINER || { log "Error: Failed to stop forgejo."; exit 1; }
log "Backing up forgejo files (including the SQLite database)..." log "Backing up forgejo files (including the SQLite database)..."
docker exec $BACKUP_CONTAINER \ docker exec $BACKUP_CONTAINER \
duplicati-cli backup \ duplicati-cli backup \
ssh://$BACKUP_DESTINATION \ "ssh://$BACKUP_DESTINATION" \
$SOURCE_DIR \ "$SOURCE_DIR" \
--backup-name="forgejo backup" \ --backup-name="forgejo backup" \
--keep-versions=7 \ --keep-versions=7 \
--auth-username=$SFTP_USERNAME \ --auth-username="$SFTP_USERNAME" \
--auth-password=$SFTP_PASSWORD \ --auth-password="$SFTP_PASSWORD" \
--passphrase=$BACKUP_ENCR_PASSPHRASE \ --passphrase="$BACKUP_ENCR_PASSPHRASE" \
--ssh-fingerprint="$SFTP_FINGERPRINT" \ --ssh-fingerprint="$SFTP_FINGERPRINT" \
--prefix="forgejo" || { log "Error: Failed to backup forgejo files and database."; exit 1; } --prefix="forgejo" || { log "Error: Failed to backup forgejo files and database."; exit 1; }
# Turn off Maintenance Mode # Turn off Maintenance Mode
log "Starting forgejo container..." log "Starting forgejo container..."
docker start $APP_CONTAINER || { log "Error: Failed to start forgejo container."; exit 1; } docker start "$APP_CONTAINER" || { log "Error: Failed to start forgejo container."; exit 1; }
log "Forgejo backup completed successfully." log "Forgejo backup completed successfully."

View file

@ -8,7 +8,7 @@ declare -a apps=("cryptpad" "immich" "duplicati" "nextcloud" "vaultwarden" "forg
# Function to execute the backup script for each application # Function to execute the backup script for each application
backup_app() { backup_app() {
local app=$1 local app=$1
$SCRIPT_ABS_LOCATION/$app/${app}-backup.sh "$SCRIPT_ABS_LOCATION/$app/${app}-backup.sh"
} }
# Iterate over each app in the array and call the backup function # Iterate over each app in the array and call the backup function

View file

@ -1,11 +1,10 @@
#!/bin/bash #!/bin/bash
SCRIPT_ABS_LOCATION=$(realpath "$(dirname "${0}")") SCRIPT_ABS_LOCATION=$(realpath "$(dirname "${0}")")
source $SCRIPT_ABS_LOCATION/immich-backup.env source "$SCRIPT_ABS_LOCATION/immich-backup.env"
source $SCRIPT_ABS_LOCATION/../logger.sh source "$SCRIPT_ABS_LOCATION/../logger.sh"
# Container Names # Container Names
APP_CONTAINER="immich_server"
DB_CONTAINER="immich_postgres" DB_CONTAINER="immich_postgres"
BACKUP_CONTAINER="duplicati" BACKUP_CONTAINER="duplicati"
@ -13,43 +12,33 @@ BACKUP_CONTAINER="duplicati"
DB_USER="postgres" DB_USER="postgres"
SOURCE_DIR="/mnt/data/immich" SOURCE_DIR="/mnt/data/immich"
BACKUP_DESTINATION="$BACKUP_DESTINATION"
BACKUP_ENCR_PASSPHRASE="$BACKUP_ENCR_PASSPHRASE"
# Cloud Storage Authentication
SFTP_USERNAME="$SFTP_USERNAME"
SFTP_PASSWORD="$SFTP_PASSWORD"
SFTP_FINGERPRINT="$SFTP_FINGERPRINT"
# Log file
LOG_FILE="$SCRIPT_ABS_LOCATION/immich-backup.log"
# Dump Database # Dump Database
log "Dumping immich postgresql database..." log "Dumping immich postgresql database..."
DB_TMP_BAK_NAME="postgres_$(date +"%Y%m%d").bak" DB_TMP_BAK_NAME="postgres_$(date +"%Y%m%d").bak"
docker exec -t $DB_CONTAINER pg_dumpall \ docker exec -t "$DB_CONTAINER" pg_dumpall \
--clean \ --clean \
--if-exists \ --if-exists \
--username=postgres \ --username="$DB_USER" \
--file=/var/lib/postgresql/data/$DB_TMP_BAK_NAME || { log "Error: Failed to dump the immich database."; exit 1; } --file="/var/lib/postgresql/data/$DB_TMP_BAK_NAME" || { log "Error: Failed to dump the immich database."; exit 1; }
# Backup all files to target destination # Backup all files to target destination
log "Backing up immich files and database..." log "Backing up immich files and database..."
docker exec $BACKUP_CONTAINER duplicati-cli backup ssh://$BACKUP_DESTINATION \ docker exec "$BACKUP_CONTAINER" duplicati-cli backup "ssh://$BACKUP_DESTINATION" \
$SOURCE_DIR/data/library \ "$SOURCE_DIR/data/library" \
$SOURCE_DIR/data/upload \ "$SOURCE_DIR/data/upload" \
$SOURCE_DIR/data/profile \ "$SOURCE_DIR/data/profile" \
$SOURCE_DIR/postgres/$DB_TMP_BAK_NAME \ "$SOURCE_DIR/postgres/$DB_TMP_BAK_NAME" \
--backup-name="immich backup" \ --backup-name="immich backup" \
--keep-versions=7 \ --keep-versions=7 \
--auth-username=$SFTP_USERNAME \ --auth-username="$SFTP_USERNAME" \
--auth-password=$SFTP_PASSWORD \ --auth-password="$SFTP_PASSWORD" \
--passphrase="$BACKUP_ENCR_PASSPHRASE" \ --passphrase="$BACKUP_ENCR_PASSPHRASE" \
--ssh-fingerprint="$SFTP_FINGERPRINT" \ --ssh-fingerprint="$SFTP_FINGERPRINT" \
--prefix="immich" || { log "Error: Failed to backup immich files and database."; exit 1; } --prefix="immich" || { log "Error: Failed to backup immich files and database."; exit 1; }
# Delete temporary backup files # Delete temporary backup files
log "Deleting temporary backup files..." log "Deleting temporary backup files..."
docker exec $DB_CONTAINER rm /var/lib/postgresql/data/$DB_TMP_BAK_NAME || { log "Error: Failed to delete temporary backup files."; exit 1; } docker exec "$DB_CONTAINER" rm "/var/lib/postgresql/data/$DB_TMP_BAK_NAME" || { log "Error: Failed to delete temporary backup files."; exit 1; }
log "immich backup completed successfully." log "immich backup completed successfully."

View file

@ -1,8 +1,8 @@
#!/bin/bash #!/bin/bash
SCRIPT_ABS_LOCATION=$(realpath "$(dirname "${0}")") SCRIPT_ABS_LOCATION=$(realpath "$(dirname "${0}")")
source $SCRIPT_ABS_LOCATION/nextcloud-backup.env source "$SCRIPT_ABS_LOCATION/nextcloud-backup.env"
source $SCRIPT_ABS_LOCATION/../logger.sh source "$SCRIPT_ABS_LOCATION/../logger.sh"
# Container Names # Container Names
APP_CONTAINER="nextcloud-app" APP_CONTAINER="nextcloud-app"
@ -11,57 +11,46 @@ BACKUP_CONTAINER="duplicati"
# Database Settings # Database Settings
DB_USER="nextcloud" DB_USER="nextcloud"
DB_PASSWORD="$DB_PASSWORD"
SOURCE_DIR="/mnt/data/nextcloud" SOURCE_DIR="/mnt/data/nextcloud"
BACKUP_DESTINATION="$BACKUP_DESTINATION"
BACKUP_ENCR_PASSPHRASE="$BACKUP_ENCR_PASSPHRASE"
# Cloud Storage Authentication
SFTP_USERNAME="$SFTP_USERNAME"
SFTP_PASSWORD="$SFTP_PASSWORD"
SFTP_FINGERPRINT="$SFTP_FINGERPRINT"
# Log file
LOG_FILE="$SCRIPT_ABS_LOCATION/nextcloud-backup.log"
# Put Nextcloud in Maintenance Mode # Put Nextcloud in Maintenance Mode
log "Putting Nextcloud in maintenance mode..." log "Putting Nextcloud in maintenance mode..."
docker exec -u www-data $APP_CONTAINER php occ maintenance:mode --on || { log "Error: Failed to put Nextcloud in maintenance mode."; exit 1; } docker exec -u www-data "$APP_CONTAINER" php occ maintenance:mode --on || { log "Error: Failed to put Nextcloud in maintenance mode."; exit 1; }
# Dump Database # Dump Database
log "Dumping Nextcloud database..." log "Dumping Nextcloud database..."
DB_TMP_BAK_NAME="nextcloud-db_$(date +"%Y%m%d").bak" DB_TMP_BAK_NAME="nextcloud-db_$(date +"%Y%m%d").bak"
docker exec $DB_CONTAINER /usr/bin/mariadb-dump \ docker exec "$DB_CONTAINER" /usr/bin/mariadb-dump \
--single-transaction \ --single-transaction \
-h localhost \ -h localhost \
-u $DB_USER \ -u "$DB_USER" \
-p"$DB_PASSWORD" \ -p"$DB_PASSWORD" \
nextcloud > $SOURCE_DIR/$DB_TMP_BAK_NAME || { log "Error: Failed to dump the Nextcloud database."; exit 1; } nextcloud > "$SOURCE_DIR/$DB_TMP_BAK_NAME" || { log "Error: Failed to dump the Nextcloud database."; exit 1; }
# Backup all files to target destination # Backup all files to target destination
log "Backing up Nextcloud files and database..." log "Backing up Nextcloud files and database..."
docker exec $BACKUP_CONTAINER \ docker exec "$BACKUP_CONTAINER" \
duplicati-cli backup \ duplicati-cli backup \
ssh://$BACKUP_DESTINATION \ "ssh://$BACKUP_DESTINATION" \
"$SOURCE_DIR/html/data" \ "$SOURCE_DIR/html/data" \
"$SOURCE_DIR/html/config" \ "$SOURCE_DIR/html/config" \
"$SOURCE_DIR/html/themes" \ "$SOURCE_DIR/html/themes" \
"$SOURCE_DIR/$DB_TMP_BAK_NAME" \ "$SOURCE_DIR/$DB_TMP_BAK_NAME" \
--backup-name="nextcloud backup" \ --backup-name="nextcloud backup" \
--keep-versions=7 \ --keep-versions=7 \
--auth-username=$SFTP_USERNAME \ --auth-username="$SFTP_USERNAME" \
--auth-password=$SFTP_PASSWORD \ --auth-password="$SFTP_PASSWORD" \
--passphrase=$BACKUP_ENCR_PASSPHRASE \ --passphrase="$BACKUP_ENCR_PASSPHRASE" \
--ssh-fingerprint="$SFTP_FINGERPRINT" \ --ssh-fingerprint="$SFTP_FINGERPRINT" \
--prefix="nextcloud" || { log "Error: Failed to backup Nextcloud files and database."; exit 1; } --prefix="nextcloud" || { log "Error: Failed to backup Nextcloud files and database."; exit 1; }
# Turn off Maintenance Mode # Turn off Maintenance Mode
log "Turning off Nextcloud maintenance mode..." log "Turning off Nextcloud maintenance mode..."
docker exec -u www-data $APP_CONTAINER php occ maintenance:mode --off || { log "Error: Failed to turn off Nextcloud maintenance mode."; exit 1; } docker exec -u www-data "$APP_CONTAINER" php occ maintenance:mode --off || { log "Error: Failed to turn off Nextcloud maintenance mode."; exit 1; }
# Delete temporary backup files # Delete temporary backup files
log "Deleting temporary backup files..." log "Deleting temporary backup files..."
rm $SOURCE_DIR/$DB_TMP_BAK_NAME || { log "Error: Failed to delete temporary databse file"; exit 1; } rm "$SOURCE_DIR/$DB_TMP_BAK_NAME" || { log "Error: Failed to delete temporary databse file"; exit 1; }
log "Nextcloud backup completed successfully." log "Nextcloud backup completed successfully."

View file

@ -1,45 +1,35 @@
#!/bin/bash #!/bin/bash
SCRIPT_ABS_LOCATION=$(realpath "$(dirname "${0}")") SCRIPT_ABS_LOCATION=$(realpath "$(dirname "${0}")")
source $SCRIPT_ABS_LOCATION/portainer-backup.env source "$SCRIPT_ABS_LOCATION/portainer-backup.env"
source $SCRIPT_ABS_LOCATION/../logger.sh source "$SCRIPT_ABS_LOCATION/../logger.sh"
# Container Names # Container Names
APP_CONTAINER="portainer" APP_CONTAINER="portainer"
BACKUP_CONTAINER="duplicati" BACKUP_CONTAINER="duplicati"
SOURCE_DIR="/volumes/portainer" SOURCE_DIR="/volumes/portainer"
BACKUP_DESTINATION="$BACKUP_DESTINATION"
BACKUP_ENCR_PASSPHRASE="$BACKUP_ENCR_PASSPHRASE"
# Cloud Storage Authentication
SFTP_USERNAME="$SFTP_USERNAME"
SFTP_PASSWORD="$SFTP_PASSWORD"
SFTP_FINGERPRINT="$SFTP_FINGERPRINT"
# Log file
LOG_FILE="$SCRIPT_ABS_LOCATION/portainer-backup.log"
# Stop portainer container # Stop portainer container
log "Stopping portainer container..." log "Stopping portainer container..."
docker stop $APP_CONTAINER || { log "Error: Failed to stop portainer."; exit 1; } docker stop "$APP_CONTAINER" || { log "Error: Failed to stop portainer."; exit 1; }
# Backup all files to target destination # Backup all files to target destination
log "Backing up portainer files (including the SQLite database)..." log "Backing up portainer files (including the SQLite database)..."
docker exec $BACKUP_CONTAINER \ docker exec "$BACKUP_CONTAINER" \
duplicati-cli backup \ duplicati-cli backup \
ssh://$BACKUP_DESTINATION \ "ssh://$BACKUP_DESTINATION" \
$SOURCE_DIR \ "$SOURCE_DIR" \
--backup-name="portainer backup" \ --backup-name="portainer backup" \
--keep-versions=7 \ --keep-versions=7 \
--auth-username=$SFTP_USERNAME \ --auth-username="$SFTP_USERNAME" \
--auth-password=$SFTP_PASSWORD \ --auth-password="$SFTP_PASSWORD" \
--passphrase=$BACKUP_ENCR_PASSPHRASE \ --passphrase="$BACKUP_ENCR_PASSPHRASE" \
--ssh-fingerprint="$SFTP_FINGERPRINT" \ --ssh-fingerprint="$SFTP_FINGERPRINT" \
--prefix="portainer" || { log "Error: Failed to backup portainer files and database."; exit 1; } --prefix="portainer" || { log "Error: Failed to backup portainer files and database."; exit 1; }
# Turn off Maintenance Mode # Turn off Maintenance Mode
log "Starting portainer container..." log "Starting portainer container..."
docker start $APP_CONTAINER || { log "Error: Failed to start portainer container."; exit 1; } docker start "$APP_CONTAINER" || { log "Error: Failed to start portainer container."; exit 1; }
log "portainer backup completed successfully." log "portainer backup completed successfully."

View file

@ -1,45 +1,34 @@
#!/bin/bash #!/bin/bash
SCRIPT_ABS_LOCATION=$(realpath "$(dirname "${0}")") SCRIPT_ABS_LOCATION=$(realpath "$(dirname "${0}")")
source $SCRIPT_ABS_LOCATION/vaultwarden-backup.env source "$SCRIPT_ABS_LOCATION/vaultwarden-backup.env"
source $SCRIPT_ABS_LOCATION/../logger.sh source "$SCRIPT_ABS_LOCATION/../logger.sh"
# Container Names # Container Names
CONTAINER="vaultwarden"
BACKUP_CONTAINER="duplicati" BACKUP_CONTAINER="duplicati"
SOURCE_DIR="/mnt/data/vaultwarden" SOURCE_DIR="/mnt/data/vaultwarden"
BACKUP_DESTINATION="$BACKUP_DESTINATION"
BACKUP_ENCR_PASSPHRASE="$BACKUP_ENCR_PASSPHRASE"
# Cloud Storage Authentication
SFTP_USERNAME="$SFTP_USERNAME"
SFTP_PASSWORD="$SFTP_PASSWORD"
SFTP_FINGERPRINT="$SFTP_FINGERPRINT"
# Log file
LOG_FILE="$SCRIPT_ABS_LOCATION/vaultwarden-backup.log"
# Dump Database # Dump Database
log "Dumping vaultwarden database..." log "Dumping vaultwarden database..."
DB_TMP_BAK_NAME=vaultwarden-db_$(date +"%Y%m%d").sqlite3 DB_TMP_BAK_NAME=vaultwarden-db_$(date +"%Y%m%d").sqlite3
sqlite3 $SOURCE_DIR/db.sqlite3 ".backup '$SOURCE_DIR/$DB_TMP_BAK_NAME'" || { log "Error: Failed to dump the Nextcloud database."; exit 1; } sqlite3 "$SOURCE_DIR/db.sqlite3 .backup '$SOURCE_DIR/$DB_TMP_BAK_NAME'" || { log "Error: Failed to dump the Nextcloud database."; exit 1; }
# Backup all files to target destination # Backup all files to target destination
log "Backing up vaultwarden files and database..." log "Backing up vaultwarden files and database..."
docker exec $BACKUP_CONTAINER duplicati-cli backup \ docker exec "$BACKUP_CONTAINER" duplicati-cli backup \
ssh://$BACKUP_DESTINATION \ "ssh://$BACKUP_DESTINATION" \
$SOURCE_DIR \ "$SOURCE_DIR" \
--backup-name="vaultwarden backup" \ --backup-name="vaultwarden backup" \
--keep-versions=7 \ --keep-versions=7 \
--auth-username=$SFTP_USERNAME \ --auth-username="$SFTP_USERNAME" \
--auth-password=$SFTP_PASSWORD \ --auth-password="$SFTP_PASSWORD" \
--passphrase="$BACKUP_ENCR_PASSPHRASE" \ --passphrase="$BACKUP_ENCR_PASSPHRASE" \
--ssh-fingerprint="$SFTP_FINGERPRINT" \ --ssh-fingerprint="$SFTP_FINGERPRINT" \
--prefix="vaultwarden" || { log "Error: Failed to backup vaultwarden files and database."; exit 1; } --prefix="vaultwarden" || { log "Error: Failed to backup vaultwarden files and database."; exit 1; }
# Delete temporary backup files # Delete temporary backup files
log "Deleting temporary backup files..." log "Deleting temporary backup files..."
rm $SOURCE_DIR/$DB_TMP_BAK_NAME || { log "Error: Failed to delete temporary backup files."; exit 1; } rm "$SOURCE_DIR/$DB_TMP_BAK_NAME" || { log "Error: Failed to delete temporary backup files."; exit 1; }
log "vaultwarden backup completed successfully." log "vaultwarden backup completed successfully."