Update incremental backup script

This commit is contained in:
ItsDrike 2023-11-19 00:12:13 +01:00
parent 77a8cced84
commit 1a3327a37c
Signed by: ItsDrike
GPG key ID: FA2745890B7048C0

View file

@ -8,9 +8,9 @@
if [ $# -lt 2 ]; then if [ $# -lt 2 ]; then
echo "Invalid amount of arguments passed!" echo "Invalid amount of arguments passed!"
echo "Arguments: [Source path] [Backup path]" echo "Arguments: <Source path> <Backup path> [--note]"
echo " Source path: directory to be backed up, usually '/'" echo " <Source path>: directory to be backed up, usually '/'"
echo " Backup path: directory to back up to (destination), usually mounted drive" echo " <Backup path>: directory to back up to (destination), usually mounted drive"
exit exit
fi fi
@ -19,6 +19,7 @@ BACKUP_DIR="$2"
DATETIME="$(date '+%Y-%m-%d_%H:%M:%S')" DATETIME="$(date '+%Y-%m-%d_%H:%M:%S')"
BACKUP_PATH="${BACKUP_DIR}/${DATETIME}" BACKUP_PATH="${BACKUP_DIR}/${DATETIME}"
LATEST_LINK="${BACKUP_DIR}/latest" LATEST_LINK="${BACKUP_DIR}/latest"
NOTE_FILE="${BACKUP_PATH}/note.md"
mkdir -p "$BACKUP_DIR" mkdir -p "$BACKUP_DIR"
@ -33,5 +34,22 @@ rsync -avHAXS \
# want to override latest symlink to non-existent location # want to override latest symlink to non-existent location
if [ -d "${BACKUP_PATH}" ]; then if [ -d "${BACKUP_PATH}" ]; then
rm "${LATEST_LINK}" 2>/dev/null rm "${LATEST_LINK}" 2>/dev/null
ln -s "$(basename "$BACKUP_DIR")" "${LATEST_LINK}" ln -s "$(basename "$BACKUP_PATH")" "${LATEST_LINK}"
fi
# Lastly, open a note file with $EDITOR insite of the backup dir,
# so the user can put in any personal notes about this backup (such
# as reason it was made, making it easy to identify it).
#
# If this command is ran with sudo (like for system wide backups),
# open the note file with user's editor, not root's
if [ "${SUDO_USER:-$USER}" != "$USER" ]; then
# Run command as the original user via sudo, with -s argument, to
# also load the user's shell, so the user's env variables ($EDITOR)
# gets set. Then make the user run sudoedit, opening a temp file,
# which when saved, will get written to the note file as user we're
# sudoed as. Note: this will re-prompt for sudo password.
NOTE_FILE="$NOTE_FILE" sudo -s --preserve-env="NOTE_FILE" -u "$SUDO_USER" sh -c 'sudoedit -u "$SUDO_USER" "$NOTE_FILE"'
else
$EDITOR "$NOTE_FILE"
fi fi