void-minecraft-usb/opt/backup-fs

84 lines
2.4 KiB
Plaintext
Raw Normal View History

#!/usr/bin/env bash
2023-06-26 17:04:28 +02:00
# used to work around cases where stdin is unavailable or not a terminal
if bash -c ': > /dev/tty' > /dev/null 2>&1; then
TTY_OR_CONSOLE="/dev/tty"
else
TTY_OR_CONSOLE="/dev/console"
fi
2023-06-26 17:04:28 +02:00
function yesno {
unset DISCARD_ME
while [ -z "$DISCARD_ME" ]; do
read -p "[y/n] " -n1 DISCARD_ME < $TTY_OR_CONSOLE
2023-06-26 17:04:28 +02:00
case "$DISCARD_ME" in
y)
echo ""
2023-06-26 17:04:28 +02:00
return 0
;;
n)
echo ""
2023-06-26 17:04:28 +02:00
return 1
;;
*)
echo " Please enter y for yes or n for no."
unset DISCARD_ME
;;
esac
done
}
# cutting off the leading slash allows us to just ignore empty lines below
readarray -t MOUNT_LIST <<< "$(findmnt --output TARGET --noheadings --raw | sed -e 's|^/||' | sort)"
# always ignore /tmp and the package cache
EXCLUDE_LIST=("tmp" "var/cache/xbps")
for I in ${!MOUNT_LIST[@]}; do
if [ ! -z "${MOUNT_LIST[$I]}" ]; then
FOUND=0
for J in ${!EXCLUDE_LIST[@]}; do
if grep "${EXCLUDE_LIST[$J]}" > /dev/null <<< "${MOUNT_LIST[$I]}"; then
FOUND=1
break
fi
done
# if no parent dir of ${MOUNT_LIST[$I]} is found in $EXCLUDE_LIST
if [ $FOUND -eq 0 ]; then
EXCLUDE_LIST+=("${MOUNT_LIST[$I]}")
fi
fi
done
touch /run/void-usb/backup-fs-excludes
touch /run/void-usb/backup-fs-pseudo
for I in ${!EXCLUDE_LIST[@]}; do
echo "/${EXCLUDE_LIST[$I]}" >> /run/void-usb/backup-fs-excludes
FILE_PERMS="$(stat -c '%a %u %g' "${EXCLUDE_LIST[$I]}")"
echo "\"/${EXCLUDE_LIST[$I]}\" d $FILE_PERMS" >> /run/void-usb/backup-fs-pseudo
done
2023-06-26 17:04:28 +02:00
#TODO: refuse to run if system is running
#TODO: add a way to force backing up without asking (for example a file in /run/void-usb)
2023-06-26 17:04:28 +02:00
#TODO: colors (bright white / light gray for readability)
2023-06-26 17:04:28 +02:00
echo "Back up system changes to disk?
This only affects things outside the /home directory.
Backing up the system will take some time.
"
if yesno; then
echo "Backing up..."
mksquashfs / /run/void-usb/container/new_squashfs.img -b 1M -comp xz -progress -noappend -pf /run/void-usb/backup-fs-pseudo -ef /run/void-usb/backup-fs-excludes
2023-06-26 17:04:28 +02:00
#TODO: check if enough disk space
# yes -> create new image next to old image
# no -> create new image in RAM
#TODO: figure out when appending to existing squashfs is a good idea
mv /run/void-usb/container/new_squashfs.img /run/void-usb/container/squashfs.img
2023-06-26 17:04:28 +02:00
else
echo ""
echo "Not backing up."
fi