Skip to content

Instantly share code, notes, and snippets.

@quagly
Created January 11, 2024 13:28
Show Gist options
  • Save quagly/b9076f6dafe00d70a6fd64a6075f49dd to your computer and use it in GitHub Desktop.
Save quagly/b9076f6dafe00d70a6fd64a6075f49dd to your computer and use it in GitHub Desktop.
copy a couple of files to mount point dir
#!/usr/bin/env bash
# backup a couple of files into time machine backups
# main purpose it to make it easier to reinstall gentoo with similar setup
# run under mwest crontab
set -euf -o pipefail
# I don't want to leave /mnt/share mounted as it prevents hibernation of UTM
# So mount it if it is not mounted, and unmount it at the end if it wasn't mounted
declare IS_MOUNTED
declare -r MOUNT_POINT="/mnt/share"
declare -r -a BACKUP_FILES=(
"/home/mwest/.config/modprobed.db"
"/var/lib/portage/world"
)
declare -r BACKUP_DIR="${MOUNT_POINT}/gentoo_backup/"
# && true avoids set -e exiting the script on non-zero return code
# see https://www.gnu.org/software/bash/manual/html_node/The-Set-Builtin.html
IS_MOUNTED=$(findmnt --mountpoint "${MOUNT_POINT}") && true
echo "IS_MOUNTED is ${IS_MOUNTED}"
# mount if not already mounted
if [[ ${IS_MOUNTED} ]];then
echo "${MOUNT_POINT} is already mounted"
else
echo "mounting ${MOUNT_POINT}"
mount "${MOUNT_POINT}"
fi
for file in "${BACKUP_FILES[@]}"
do
cmd="cp "
cmd+="$file "
cmd+="${BACKUP_DIR}"
cmd+=$(basename "$file")
echo "$cmd"
$cmd
done
# umount if not initially mounted
if [[ ${IS_MOUNTED} ]];then
echo "leaving ${MOUNT_POINT} mounted"
else
echo "unmounting ${MOUNT_POINT}"
umount "${MOUNT_POINT}"
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment