Refactor rsync-backup

This commit is contained in:
Jeffrey Serio 2024-07-07 20:25:03 -05:00
parent d69f8fdbac
commit 6b44c86915
4 changed files with 9 additions and 17 deletions

20
bin/rsync-backup Normal file → Executable file
View File

@ -1,29 +1,21 @@
#!/usr/bin/env bash #!/usr/bin/env bash
# A script to perform incremental backups with rsync # A script to perform incremental backups with rsync
# Source: https://linuxconfig.org/how-to-create-incremental-backups-using-rsync-on-linux
set -o errexit
set -o nounset
set -o pipefail
readonly BACKUP_DIR="/mnt/backup"
readonly DATETIME
DATETIME="$(date '+%Y-%m-%d_%H:%M:%S')" DATETIME="$(date '+%Y-%m-%d_%H:%M:%S')"
readonly BACKUP_DIR="/mnt/backup"
readonly BACKUP_PATH="${BACKUP_DIR}/${DATETIME}" readonly BACKUP_PATH="${BACKUP_DIR}/${DATETIME}"
readonly LATEST_LINK="${BACKUP_DIR}/latest" readonly LATEST_LINK="${BACKUP_DIR}/latest"
readonly TEMP_DIR
TEMP_DIR=$(mktemp -d || echo "Failed to make temp dir"; exit 1) TEMP_DIR=$(mktemp -d || echo "Failed to make temp dir"; exit 1)
chmod 700 "${TEMP_DIR}" chmod 700 "${TEMP_DIR}"
trap 'rm -rf "${TEMP_DIR}"; exit 0' 0 1 2 3 15 trap 'rm -rf "${TEMP_DIR}"; exit 0' EXIT SIGHUP SIGINT SIGQUIT SIGTERM
mkdir -p "${BACKUP_DIR}"
while read -r line; do while read -r line; do
rsync -aAX "$line" "${TEMP_DIR}"; rsync -aAX "$line" "${TEMP_DIR}";
done </etc/rsync-includes.txt done </etc/rsync-includes.txt
rsync -av --delete "${TEMP_DIR}/" --link-dest "${LATEST_LINK}" --exclude=".cache" "${BACKUP_PATH}" rsync -av --delete "${TEMP_DIR}/" --link-dest="${LATEST_LINK}" "${BACKUP_PATH}"
rm -rf "${LATEST_LINK}" rm -f "${LATEST_LINK}"
ln -s "${BACKUP_PATH}" "${LATEST_LINK}" ln -sf "${BACKUP_PATH}" "${LATEST_LINK}"

View File

@ -1,8 +1,8 @@
[Unit] [Unit]
Description=rsync-backup Description=rsync-backup
AssertFileIsExecutable=/usr/local/bin/rsync-backup AssertFileIsExecutable=/usr/local/bin/rsync-backup
Requires=hyperreal.coffee-share.mount Requires=mnt-backup.mount
After=hyperreal.coffee-share.mount After=mnt-backup.mount
[Service] [Service]
Type=oneshot Type=oneshot

View File

@ -2,7 +2,7 @@
Description=rsync-backup Description=rsync-backup
[Timer] [Timer]
OnCalendar=weekly OnCalendar=Sun *-*-* 23:30:00
Persistent=true Persistent=true
[Install] [Install]