Compare commits

..

1 commit

Author SHA1 Message Date
ZoopaMario 2eadbafa38 WIP Refactor code base
All checks were successful
Lint Bash Scripts / lint-bash (push) Successful in 26s
Check Commit Messages / check-commit-message (pull_request) Successful in 16s
Lint Bash Scripts / lint-bash (pull_request) Successful in 22s
Add a library file containing common logic to be sourced by individual backup scripts
2024-12-30 05:37:21 +01:00
3 changed files with 45 additions and 80 deletions

View file

@ -49,14 +49,14 @@ dump_mariadb_db() {
local pass="$3" local pass="$3"
local db="$4" local db="$4"
local path="$5" local path="$5"
log "Dumping MariaDB/MySQL db..." log "Dumping MariaDB/MySQL db for $c..."
docker exec "$c" mariadb-dump --single-transaction -h localhost -u "$user" -p"$pass" "$db" > "$path" || { log "Error dumping MariaDB/MySQL"; exit 1; } 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() { dump_sqlite_db() {
local src="$1" local src="$1"
local dst="$2" local dst="$2"
log "Dumping SQLite db..." log "Dumping SQLite db for $c..."
sqlite3 "$src" ".backup $dst" || { log "Error dumping SQLite"; exit 1; } sqlite3 "$src" ".backup $dst" || { log "Error dumping SQLite"; exit 1; }
} }
@ -82,53 +82,3 @@ run_duplicati_backup() {
log "$name backup complete." 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

@ -1,46 +1,62 @@
#!/bin/bash #!/bin/bash
SCRIPT_ABS_LOCATION=$(realpath "$(dirname "${0}")") SCRIPT_ABS_LOCATION=$(realpath "$(dirname "${0}")")
# shellcheck disable=SC1090
source "$SCRIPT_ABS_LOCATION/../common-backup.sh"
# shellcheck disable=SC1090 # shellcheck disable=SC1090
source "$SCRIPT_ABS_LOCATION/duplicati-backup.env" source "$SCRIPT_ABS_LOCATION/duplicati-backup.env"
# 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}"
# shellcheck disable=SC2034 # shellcheck disable=SC2034
LOG_FILE="$SCRIPT_ABS_LOCATION/duplicati-backup.log" 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" TMP_FILEPATH="$SCRIPT_ABS_LOCATION"
if [ -z "$TMP_FILEPATH" ]; then if [ -z "$TMP_FILEPATH" ]; then
log "Error: TMP_FILEPATH is not set." log "Error: TMP_FILEPATH is not set."; exit 1
exit 1
fi fi
stop_container duplicati # 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; }
tar_and_encrypt "$SOURCE_DIR" "$TMP_FILEPATH/$TMP_FILENAME" "$BACKUP_ENCR_PASSPHRASE"
remove_old_backups \ # Connect to the backup host and count the number of existing backups
"$SSH_USERNAME" \ log "Fetching number of backups in destination folder"
"$SSH_DESTINATION" \ 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; }
"$SSH_KEY" \
"$SSH_PORT" \
"$BACKUP_FOLDER" \
"$MAX_BACKUPS"
scp_transfer \ # Remove old backups if there are too many
"$TMP_FILEPATH/$TMP_FILENAME" \ if (( EXISTING_BACKUPS > MAX_BACKUPS )); then
"$SSH_USERNAME" \ log "Removing old backups in order to save space"
"$SSH_DESTINATION" \ 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_KEY" \ fi
"$SSH_PORT" \
"${BACKUP_FOLDER}${TMP_FILENAME}"
log "Cleaning up temporary archive..." # Transfer the encrypted archive to the backup host using scp
rm "$TMP_FILEPATH/$TMP_FILENAME" || { log "Transfering archive to SSH Destination"
log "Error: Failed to remove the temporary file." 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; }
exit 1
}
start_container duplicati # Remove the temporary file
log "Cleaning up files"
rm "$TMP_FILEPATH/$TMP_FILENAME" || { log "Error: Failed to remove the temporary file."; exit 1; }
log "duplicati backup completed." log "Backup completed successfully."

View file

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