Compare commits

..

1 commit

Author SHA1 Message Date
ZoopaMario 52fee22f04 Refactor code base focusing on extendability
All checks were successful
Check Commit Messages / check-commit-message (pull_request) Successful in 13s
Lint Bash Scripts / lint-bash (pull_request) Successful in 23s
Lint Bash Scripts / lint-bash (push) Successful in 1m0s
- Add a library file containing common logic to be sourced by individual backup scripts
- Integrate logger file as it so far only contained a single method
- Make sure all relevant variables are defined within the related .env file
- Remove the option to pass command line  arguments to the script
2024-12-30 06:50:33 +01:00
3 changed files with 80 additions and 45 deletions

View file

@ -49,14 +49,14 @@ dump_mariadb_db() {
local pass="$3"
local db="$4"
local path="$5"
log "Dumping MariaDB/MySQL db for $c..."
log "Dumping MariaDB/MySQL db..."
docker exec "$c" mariadb-dump --single-transaction -h localhost -u "$user" -p"$pass" "$db" > "$path" || { log "Error dumping MariaDB/MySQL"; exit 1; }
}
dump_sqlite_db() {
local src="$1"
local dst="$2"
log "Dumping SQLite db for $c..."
log "Dumping SQLite db..."
sqlite3 "$src" ".backup $dst" || { log "Error dumping SQLite"; exit 1; }
}
@ -82,3 +82,53 @@ run_duplicati_backup() {
log "$name backup complete."
}
tar_and_encrypt() {
local source_dir="$1"
local output_file="$2"
local passphrase="$3"
log "Compressing and encrypting $source_dir..."
sudo tar --absolute-names -czf - "$source_dir" | \
openssl enc -aes-256-cbc -pbkdf2 -pass "pass:$passphrase" > "$output_file" || {
log "Error: Failed to create encrypted archive."
exit 1
}
}
scp_transfer() {
local local_file="$1"
local ssh_user="$2"
local ssh_host="$3"
local ssh_key="$4"
local ssh_port="$5"
local remote_path="$6"
log "Transferring $local_file to $ssh_user@$ssh_host:$remote_path"
scp -v -P "$ssh_port" -i "$ssh_key" "$local_file" \
"$ssh_user@$ssh_host:$remote_path" || {
log "Error: Failed to transfer file."
exit 1
}
}
remove_old_backups() {
local ssh_user="$1"
local ssh_host="$2"
local ssh_key="$3"
local ssh_port="$4"
local backup_folder="$5"
local max_backups="$6"
log "Checking for old backups..."
local existing_backups
existing_backups=$(ssh -p "$ssh_port" -i "$ssh_key" "$ssh_user@$ssh_host" \
"ls -1 $backup_folder | wc -l") || {
log "Error: Failed to count existing backups."
exit 1
}
if (( existing_backups > max_backups )); then
log "Removing old backups..."
ssh -p "$ssh_port" -i "$ssh_key" "$ssh_user@$ssh_host" \
"ls -t $backup_folder | tail -n +$((max_backups+1)) | xargs rm" || {
log "Error: Failed to remove old backups."
exit 1
}
fi
}

View file

@ -2,61 +2,45 @@
SCRIPT_ABS_LOCATION=$(realpath "$(dirname "${0}")")
# shellcheck disable=SC1090
source "$SCRIPT_ABS_LOCATION/duplicati-backup.env"
source "$SCRIPT_ABS_LOCATION/../common-backup.sh"
# shellcheck disable=SC1090
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}"
source "$SCRIPT_ABS_LOCATION/duplicati-backup.env"
# shellcheck disable=SC2034
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_KEY="$OPTARG";;
m) MAX_BACKUPS="$OPTARG";;
b) BACKUP_FOLDER="$OPTARG";;
\?) echo "Invalid option: -$OPTARG"; exit 1;;
esac
done
# Create a temporary file for the encrypted archive
TMP_FILENAME="duplicati_db-$(date +"%Y%m%d").bak"
TMP_FILEPATH="$SCRIPT_ABS_LOCATION"
if [ -z "$TMP_FILEPATH" ]; then
log "Error: TMP_FILEPATH is not set."; exit 1
log "Error: TMP_FILEPATH is not set."
exit 1
fi
# Create the encrypted archive using tar and openssl
log "Compressing and excrypting 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; }
stop_container duplicati
tar_and_encrypt "$SOURCE_DIR" "$TMP_FILEPATH/$TMP_FILENAME" "$BACKUP_ENCR_PASSPHRASE"
# Connect to the backup host and count the number of existing backups
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; }
remove_old_backups \
"$SSH_USERNAME" \
"$SSH_DESTINATION" \
"$SSH_KEY" \
"$SSH_PORT" \
"$BACKUP_FOLDER" \
"$MAX_BACKUPS"
# Remove old backups if there are too many
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."; exit 1; }
fi
scp_transfer \
"$TMP_FILEPATH/$TMP_FILENAME" \
"$SSH_USERNAME" \
"$SSH_DESTINATION" \
"$SSH_KEY" \
"$SSH_PORT" \
"${BACKUP_FOLDER}${TMP_FILENAME}"
# Transfer the encrypted archive to the backup host using scp
log "Transfering 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 archive..."
rm "$TMP_FILEPATH/$TMP_FILENAME" || {
log "Error: Failed to remove the temporary file."
exit 1
}
# Remove the temporary file
log "Cleaning up files"
rm "$TMP_FILEPATH/$TMP_FILENAME" || { log "Error: Failed to remove the temporary file."; exit 1; }
start_container duplicati
log "Backup completed successfully."
log "duplicati backup completed."

View file

@ -14,5 +14,6 @@ backup_app() {
# Iterate over each app in the array and call the backup function
for app in "${apps[@]}"; do
backup_app "$app"
sleep 5
done