Rollback cleanups implemented

Implemented systemd unit that removes the *.backup subvolumes leftover
from rollbacks
This commit is contained in:
Jarno Rankinen 2020-08-07 11:22:28 +03:00
parent c2750ab03d
commit b6e5132cb5
2 changed files with 45 additions and 2 deletions

35
snapsh
View File

@ -226,6 +226,34 @@ rollback() {
post-rollback() {
## This function is meant to be executed by the included systemd unit on every boot
## Outputs to systemd-journal, use journalctl -t snapsh to check output
EXIT_CODE=0
shopt -s nullglob
echo "Checking for leftover subvolumes..." | systemd-cat -t snapsh
BACKUPS=("${TOPLEVEL}/*.backup/")
if [[ -n "$BACKUPS" ]]; then
for backup in ${TOPLEVEL}/*.backup/; do
echo "${backup} found" | systemd-cat -t snapsh
echo "Deleting ${backup}..." | systemd-cat -t snapsh
${BTRFS_EXECUTABLE} subvolume delete ${backup} > /dev/null
let EXIT_CODE=${EXIT_CODE}+$?
done
exit ${EXIT_CODE}
else
echo "No leftovers found." | systemd-cat -t snapsh
exit 0
fi
}
# Check for root permissions # Check for root permissions
root_check() { root_check() {
if [[ "$UID" -ne 0 ]]; then if [[ "$UID" -ne 0 ]]; then
@ -244,7 +272,7 @@ fi
# Options parsing: # Options parsing:
OPTIONS=$(getopt -a -n snapsh -o hs:d:lr:t: --long help,snapshot:,description:,list,remove:,rollback:,type: -- "$@") OPTIONS=$(getopt -a -n snapsh -o hs:d:lr:t: --long help,snapshot:,description:,list,remove:,rollback:,type:,post-rollback -- "$@")
# Invalid options (getopt returns nonzero) # Invalid options (getopt returns nonzero)
if [[ "$?" -ne 0 ]]; then if [[ "$?" -ne 0 ]]; then
@ -293,6 +321,11 @@ while true; do
shift 2 shift 2
;; ;;
--post-rollback)
post-rollback
shift
;;
-t | --type) -t | --type)
case "$2" in case "$2" in
manual) manual)

View File

@ -0,0 +1,10 @@
[Unit]
Description=Remove leftover *.backup subvolumes
[Service]
Type=oneshot
ExecStart=/usr/sbin/snapsh --post-rollback
RemainAfterExit=yes
[Install]
WantedBy=default.target