Skip to content

Instantly share code, notes, and snippets.

@apsun
Last active March 4, 2023 18:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save apsun/02c1dbc65f82f370e52af3446e0b407c to your computer and use it in GitHub Desktop.
Save apsun/02c1dbc65f82f370e52af3446e0b407c to your computer and use it in GitHub Desktop.
Backup files using rsync to an external disk encrypted with LUKS
#!/bin/bash
set -euo pipefail
EXTDRV_FILE='/dev/disk/by-uuid/408acd50-8b18-470c-b4f3-39d8e6351107'
EXTDRV_LABEL='extdrv'
EXTDRV_MOUNT='/mnt/extdrv'
SRC_ROOT='/home/andrew'
DEST_ROOT='/mnt/extdrv/andrew'
mount() {
sudo mkdir -p "${EXTDRV_MOUNT}"
sudo cryptsetup open "${EXTDRV_FILE}" "${EXTDRV_LABEL}"
sudo mount "/dev/mapper/${EXTDRV_LABEL}" "${EXTDRV_MOUNT}"
}
unmount() {
sudo umount "${EXTDRV_MOUNT}"
sudo cryptsetup close "${EXTDRV_LABEL}"
sudo rmdir "${EXTDRV_MOUNT}"
}
rsync_with_exclude() {
rsync \
--exclude "/.cache" \
--exclude "/.go" \
--exclude "/.cargo" \
--exclude "/.rustup" \
--exclude "/.java" \
--exclude "/.android" \
--exclude "/.gradle" \
--exclude "/.m2" \
--exclude "/.minikube/cache" \
--exclude "/.kube/cache" \
--exclude "/.local/share/Trash" \
--exclude "/Downloads" \
"$@"
}
backup() {
rsync_with_exclude \
--archive \
--progress \
--inplace \
--delete \
"${SRC_ROOT}/." \
"${DEST_ROOT}"
}
verify() {
rsync_with_exclude \
--dry-run \
--archive \
--checksum \
--itemize-changes \
--verbose \
"${SRC_ROOT}/." \
"${DEST_ROOT}"
}
all() {
mount
backup
unmount
}
"$@"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment