Skip to content

Instantly share code, notes, and snippets.

@fbennett
Created April 20, 2023 08:40
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fbennett/dcb066fc71e116d3e35d2ec9e144ab71 to your computer and use it in GitHub Desktop.
Save fbennett/dcb066fc71e116d3e35d2ec9e144ab71 to your computer and use it in GitHub Desktop.
#!/bin/bash
## NOTE: to merge divergent backups use:
##
## rsync -aiuP <older>src/ <newer>dest/
##
set -e
BDIR=/path/to/plugged/usb/disk/BACKUPS
TARGETS="/paths /to /back /up"
OWNER_AND_GROUP="owner:owner"
if [ ! -d "${BDIR}" ]; then
echo "Insert backup USB"
exit 0
fi
# Report out old and new backup times
if [ -f "${BDIR}/info/time.txt" ]; then
OLD_TIME=$(cat "${BDIR}/info/time.txt")
else
echo "Failed to find file ${BDIR}/info/time.txt file, aborting."
echo "Touch it to create an empty file there to proceed."
exit 0
fi
NEW_TIME=$(LANG=en date +"%F:%H")
# Make the log file
# echo "" > "${BDIR}/info/rsync-$(cat ${BDIR}/info/time.txt).log"
# Make the target directory
if [ ! -d "${BDIR}/BACKUP-IN-PROGRESS" ]; then
sudo mkdir "${BDIR}/BACKUP-IN-PROGRESS"
sudo chown ${OWNER_AND_GROUP} "${BDIR}/BACKUP-IN-PROGRESS"
fi
cd ~/.
sudo rsync -avhPR \
--stats \
--log-file="${BDIR}/info/rsync-${NEW_TIME}.log" \
--exclude-from="${BDIR}/info/exclude.txt" \
--link-dest="${BDIR}/${OLD_TIME}" \
${TARGETS} \
"${BDIR}/BACKUP-IN-PROGRESS"
sudo echo "Last backup: ${OLD_TIME}"
echo "This backup: ${NEW_TIME}"
#move
sudo mv "${BDIR}/BACKUP-IN-PROGRESS" "${BDIR}/${NEW_TIME}"
#overwrite old time.txt file with new time
sudo echo ${NEW_TIME} > "${BDIR}/info/time.txt"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment