158 lines
4.3 KiB
Bash
Executable File
158 lines
4.3 KiB
Bash
Executable File
#!/bin/bash
|
|
|
|
################################################################################
|
|
# Backup External Disk Script
|
|
# ---------------------------
|
|
#
|
|
# THIS SCRIPT IS WORK IN PROGRESS AND WILL PROBABLY BE REPLACED SOON!
|
|
#
|
|
# Author: Robin Meier - robin@meier.si
|
|
################################################################################
|
|
|
|
# Load configuration
|
|
script_dir=$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )
|
|
set -o allexport
|
|
source ${script_dir}/config/backup > /dev/null 2>&1
|
|
set +o allexport
|
|
|
|
# Import logging functionality
|
|
logfile=${script_dir}/log/backup.log
|
|
log_identifier="EXTERN"
|
|
source ${script_dir}/functions/logging.sh
|
|
|
|
# Exit if no env file is given
|
|
if [[ -z "$EXTERNAL_DISK_BACKUP_ENV_FILE" ]]; then
|
|
exit 0
|
|
fi
|
|
|
|
set -o allexport
|
|
source $EXTERNAL_DISK_BACKUP_ENV_FILE
|
|
set +o allexport
|
|
|
|
log "External Disk Backup Started"
|
|
|
|
# Check if required env vars are set
|
|
if [[ -z "$BORG_REPO" || -z "$BORG_PASSPHRASE" || -z "$BORG_DEBIAN_DIRS" || -z "$BORG_ZFS_DIRS" ]]; then
|
|
log "[ERROR] Required env var missing in $EXTERNAL_DISK_BACKUP_ENV_FILE"
|
|
exit 42
|
|
fi
|
|
|
|
if [[ -z "$ZFS_ARRAY_NAME" ]]; then
|
|
ZFS_ARRAY_NAME=zfs
|
|
fi
|
|
|
|
# Automated negative response to these questions
|
|
export BORG_RELOCATED_REPO_ACCESS_IS_OK=no
|
|
export BORG_UNKNOWN_UNENCRYPTED_REPO_ACCESS_IS_OK=no
|
|
|
|
# Error handling
|
|
trap 'echo $( date ) Backup interrupted >&2; exit 2' INT TERM
|
|
|
|
# Output current version
|
|
borg --version
|
|
|
|
|
|
# Backup debian
|
|
log "Creating debian backup"
|
|
|
|
borg create $BORG_OPTS \
|
|
--verbose \
|
|
--filter AME \
|
|
--list \
|
|
--show-rc \
|
|
--compression lz4 \
|
|
--one-file-system \
|
|
--exclude-caches \
|
|
--exclude 'root/.cache' \
|
|
--exclude 'home/*/.cache/*' \
|
|
--exclude 'var/tmp/*' \
|
|
--exclude 'var/lib/docker/*' \
|
|
::'{hostname}-debian-{now}' \
|
|
$BORG_DEBIAN_DIRS
|
|
|
|
backup_exit_1=$?
|
|
|
|
log "Finished creating debian backup ($backup_exit_1)"
|
|
|
|
|
|
# Backup zfs array
|
|
log "Creating $ZFS_ARRAY_NAME backup"
|
|
|
|
borg create $BORG_OPTS \
|
|
--verbose \
|
|
--filter AME \
|
|
--list \
|
|
--show-rc \
|
|
--compression lz4 \
|
|
--exclude-caches \
|
|
::"{hostname}-${ZFS_ARRAY_NAME}-{now}" \
|
|
$BORG_ZFS_DIRS
|
|
|
|
backup_exit_2=$?
|
|
|
|
log "Finished creating $ZFS_ARRAY_NAME backup ($backup_exit_2)"
|
|
|
|
backup_exit=$(( backup_exit_2 > backup_exit_1 ? backup_exit_2 : backup_exit_1 ))
|
|
|
|
|
|
# Prune no longer needed backups
|
|
log "Pruning debian backups from repository"
|
|
|
|
borg prune \
|
|
--list \
|
|
--glob-archives '{hostname}-debian-*' \
|
|
--show-rc \
|
|
--keep-daily 7 \
|
|
--keep-weekly 4 \
|
|
--keep-monthly 6
|
|
|
|
prune_exit_1=$?
|
|
|
|
log "Finished pruning debian backups from repository ($prune_exit_1)"
|
|
|
|
log "Pruning $ZFS_ARRAY_NAME backups from repository"
|
|
|
|
borg prune \
|
|
--list \
|
|
--glob-archives "{hostname}-${ZFS_ARRAY_NAME}-*" \
|
|
--show-rc \
|
|
--keep-daily 7 \
|
|
--keep-weekly 4 \
|
|
--keep-monthly 6
|
|
|
|
prune_exit_2=$?
|
|
|
|
log "Finished pruning $ZFS_ARRAY_NAME backups from repository ($prune_exit_2)"
|
|
|
|
prune_exit=$(( prune_exit_2 > prune_exit_1 ? prune_exit_2 : prune_exit_1 ))
|
|
|
|
|
|
# Compact repository
|
|
log "Compacting repository"
|
|
|
|
# TODO: Maybe first check if compacting is supported by the client? (Older versions)
|
|
|
|
borg compact
|
|
|
|
compact_exit=$?
|
|
|
|
log "Finished compacting repository ($compact_exit)"
|
|
|
|
|
|
# Calculate global exit code
|
|
global_exit=$(( backup_exit > prune_exit ? backup_exit : prune_exit ))
|
|
global_exit=$(( compact_exit > global_exit ? compact_exit : global_exit ))
|
|
|
|
if [ ${global_exit} -eq 0 ]; then
|
|
log "Backup, Prune, and Compact finished successfully"
|
|
elif [ ${global_exit} -eq 1 ]; then
|
|
log_echo "Backup, Prune, and/or Compact finished with warnings"
|
|
else
|
|
log_echo "Backup, Prune, and/or Compact finished with errors"
|
|
fi
|
|
|
|
|
|
log "External Disk Backup Finished"
|
|
|
|
exit ${global_exit}
|