Compare commits

..

1 commit

Author SHA1 Message Date
ZoopaMario 2caa2302b5 Refactor code to make linter runs succeed
Some checks failed
Lint Bash Scripts / lint-bash (push) Failing after 25s
Check Commit Messages / check-commit-message (pull_request) Successful in 16s
Lint Bash Scripts / lint-bash (pull_request) Failing after 46s
This commit refactors the codebase in order to make the shellcheck
workflow pass.
2024-12-29 19:14:46 +01:00
7 changed files with 85 additions and 11 deletions

View file

@ -11,6 +11,9 @@ source "$SCRIPT_ABS_LOCATION/../logger.sh"
CONTAINER="cryptpad" CONTAINER="cryptpad"
BACKUP_CONTAINER="duplicati" BACKUP_CONTAINER="duplicati"
# Log file
LOG_FILE="$SCRIPT_ABS_LOCATION/cryptpad-backup.log"
SOURCE_DIR="/mnt/data/cryptpad" SOURCE_DIR="/mnt/data/cryptpad"
# Cleanup inactive and archive files # Cleanup inactive and archive files

View file

@ -7,11 +7,65 @@ source "$SCRIPT_ABS_LOCATION/duplicati-backup.env"
# shellcheck disable=SC1090 # shellcheck disable=SC1090
source "$SCRIPT_ABS_LOCATION/../logger.sh" source "$SCRIPT_ABS_LOCATION/../logger.sh"
# Default values
SOURCE_DIR="/mnt/data/duplicati"
MAX_BACKUPS=7
BACKUP_FOLDER=""
SSH_KEY="${SSH_KEY:-$HOME/.ssh/id_rsa}"
LOG_FILE="$SCRIPT_ABS_LOCATION/duplicati-backup.log"
# Override with command-line arguments
while getopts ":f:h:u:p:m:b:" opt; do
case $opt in
f) SOURCE_DIR="$OPTARG";;
h) SSH_DESTINATION="$OPTARG";;
u) SSH_USERNAME="$OPTARG";;
p) SSH_PASSWORD="$OPTARG";;
m) MAX_BACKUPS="$OPTARG";;
b) BACKUP_FOLDER="$OPTARG";;
\?) echo "Invalid option: -$OPTARG"; exit 1;;
esac
done
TMP_FILENAME="duplicati_db-$(date +"%Y%m%d").bak"
TMP_FILEPATH="$SCRIPT_ABS_LOCATION"
log "Compressing and encrypting the Duplicati Databases"
sudo tar --absolute-names -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
}
log "Fetching number of backups in destination folder"
EXISTING_BACKUPS=$(ssh -p 23 -i "$SSH_KEY" "$SSH_USERNAME@$SSH_DESTINATION" "ls '$BACKUP_FOLDER'" | wc -l) || {
log "Error: Failed to count existing backups."
exit 1
}
if (( $EXISTING_BACKUPS > $MAX_BACKUPS )); then
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."; ex
it 1; }
fi
log "Transferring archive to SSH Destination"
scp -v -P 23 -i "$SSH_KEY" "$TMP_FILEPATH/$TMP_FILENAME" "$SSH_USERNAME@$SSH_DESTINATION:$BACKUP_FOLDER$TMP_FILENAME" || {
log "Error: Failed to transfer the encrypted archive."
exit 1
}
log "Cleaning up temporary files"
rm "$TMP_FILEPATH/$TMP_FILENAME" || {
log "Error: Failed to remove the temporary file."
exit 1
}
log "Backup completed successfully."
SOURCE_DIR="/mnt/data/duplicati" SOURCE_DIR="/mnt/data/duplicati"
# Set default values for parameters # Set default values for parameters
MAX_BACKUPS=7 MAX_BACKUPS=7
BACKUP_FOLDER=""
# 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
@ -27,23 +81,25 @@ 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"
TMP_FILEPATH="$SCRIPT_ABS_LOCATION" || { log "Error: Failed to create a temporary file."; exit 1; } if [ -z "$TMP_FILEPATH" ]; then
log "Error: TMP_FILEPATH is not set."; exit 1
fi
# 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 --absolute-names -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

@ -11,6 +11,9 @@ source "$SCRIPT_ABS_LOCATION/../logger.sh"
APP_CONTAINER="forgejo" APP_CONTAINER="forgejo"
BACKUP_CONTAINER="duplicati" BACKUP_CONTAINER="duplicati"
# Log file
LOG_FILE="$SCRIPT_ABS_LOCATION/forgejo-backup.log"
SOURCE_DIR="/mnt/data/forgejo" SOURCE_DIR="/mnt/data/forgejo"
# Stop forgejo container # Stop forgejo container

View file

@ -11,6 +11,9 @@ source "$SCRIPT_ABS_LOCATION/../logger.sh"
DB_CONTAINER="immich_postgres" DB_CONTAINER="immich_postgres"
BACKUP_CONTAINER="duplicati" BACKUP_CONTAINER="duplicati"
# Log file
LOG_FILE="$SCRIPT_ABS_LOCATION/immich-backup.log"
# Database Settings # Database Settings
DB_USER="postgres" DB_USER="postgres"

View file

@ -12,6 +12,9 @@ APP_CONTAINER="nextcloud-app"
DB_CONTAINER="nextcloud-db" DB_CONTAINER="nextcloud-db"
BACKUP_CONTAINER="duplicati" BACKUP_CONTAINER="duplicati"
# Log file
LOG_FILE="$SCRIPT_ABS_LOCATION/nextcloud-backup.log"
# Database Settings # Database Settings
DB_USER="nextcloud" DB_USER="nextcloud"

View file

@ -11,6 +11,9 @@ source "$SCRIPT_ABS_LOCATION/../logger.sh"
APP_CONTAINER="portainer" APP_CONTAINER="portainer"
BACKUP_CONTAINER="duplicati" BACKUP_CONTAINER="duplicati"
# Log file
LOG_FILE="$SCRIPT_ABS_LOCATION/portainer-backup.log"
SOURCE_DIR="/volumes/portainer" SOURCE_DIR="/volumes/portainer"
# Stop portainer container # Stop portainer container

View file

@ -10,12 +10,15 @@ source "$SCRIPT_ABS_LOCATION/../logger.sh"
# Container Names # Container Names
BACKUP_CONTAINER="duplicati" BACKUP_CONTAINER="duplicati"
# Log file
LOG_FILE="$SCRIPT_ABS_LOCATION/vaultwarden-backup.log"
SOURCE_DIR="/mnt/data/vaultwarden" SOURCE_DIR="/mnt/data/vaultwarden"
# 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..."