grub-btrfsd: watch snapshots directory recursively

Fix: #262

Signed-off-by: Pascal Jäger <pascal.jaeger@leimstift.de>
This commit is contained in:
Pascal Jäger
2023-03-26 20:44:50 +02:00
parent e2937f6bff
commit 879bec3697
5 changed files with 35 additions and 23 deletions
+2
View File
@@ -103,6 +103,8 @@ E.g. for Snapper this would be `/.snapshots`
Disable colors in output. Disable colors in output.
* `-l / --log-file` * `-l / --log-file`
This arguments specifies a file where grub-btrfsd should write log messages. This arguments specifies a file where grub-btrfsd should write log messages.
* `-r / --recursive`
Watch the snapshots directory recursively
* `-s / --syslog` * `-s / --syslog`
* `-o / --timeshift-old` * `-o / --timeshift-old`
Look for snapshots in `/run/timeshift/backup/timeshift-btrfs` instead of `/run/timeshift/$PID/backup/timeshift-btrfs.` This is to be used for Timeshift versions <22.06. Look for snapshots in `/run/timeshift/backup/timeshift-btrfs` instead of `/run/timeshift/$PID/backup/timeshift-btrfs.` This is to be used for Timeshift versions <22.06.
+1 -9
View File
@@ -1,15 +1,7 @@
#!/usr/bin/env bash #!/usr/bin/env bash
<<<<<<< HEAD GRUB_BTRFS_VERSION=4.12-master-2023-03-26T18:44:50+00:00
<<<<<<< HEAD
GRUB_BTRFS_VERSION=4.12-HEAD-2023-03-26T17:46:23+00:00
=======
GRUB_BTRFS_VERSION=4.12-HEAD-2023-03-26T17:46:23+00:00
>>>>>>> 02513bb (grub-btrfs: get bach the grub_btrfs_search_directory functionality)
=======
GRUB_BTRFS_VERSION=4.12-HEAD-2023-03-26T17:46:23+00:00
>>>>>>> 4b564c1 (grub-btrfs: update comments)
# Disable grub-btrfs. # Disable grub-btrfs.
# Default: "false" # Default: "false"
+23 -8
View File
@@ -1,5 +1,5 @@
#!/usr/bin/env bash #!/usr/bin/env bash
# Copyright 2022 Pascal Jaeger # Copyright 2023 Pascal Jaeger
# Distributed under the terms of the GNU General Public License v3 # Distributed under the terms of the GNU General Public License v3
# Update GRUB when new BTRFS snapshots are created. # Update GRUB when new BTRFS snapshots are created.
@@ -12,6 +12,7 @@ timeshift_auto=false
timeshift_old=false timeshift_old=false
verbose=false verbose=false
syslog=false syslog=false
recursive=false
setcolors() { setcolors() {
if [ "${1}" = true ]; then if [ "${1}" = true ]; then
@@ -37,13 +38,14 @@ grub_btrfs_config="${sysconfdir}/default/grub-btrfs/config"
print_help() { print_help() {
echo "${CYAN}[?] Usage:" echo "${CYAN}[?] Usage:"
echo "${0##*/} [-h, --help] [-c, --no-color] [-l, --log-file LOG_FILE] [-s, --syslog] [-t, --timeshift-auto] [-v, --verbose] SNAPSHOTS_DIR" echo "${0##*/} [-h, --help] [-c, --no-color] [-l, --log-file LOG_FILE] [-r, --recursive] [-s, --syslog] [-t, --timeshift-auto] [-v, --verbose] SNAPSHOTS_DIR"
echo echo
echo "SNAPSHOTS_DIR Snapshot directory to watch, without effect when --timeshift-auto" echo "SNAPSHOTS_DIR Snapshot directory to watch, without effect when --timeshift-auto"
echo echo
echo "Optional arguments:" echo "Optional arguments:"
echo "-c, --no-color Disable colors in output" echo "-c, --no-color Disable colors in output"
echo "-l, --log-file Specify a logfile to write to" echo "-l, --log-file Specify a logfile to write to"
echo "-r, --recursive Watch snapshots directory recursively"
echo "-s, --syslog Write to syslog" echo "-s, --syslog Write to syslog"
echo "-o, --timeshift-old Look for snapshots in directory of Timeshift <v22.06 (requires --timeshift-auto)" echo "-o, --timeshift-old Look for snapshots in directory of Timeshift <v22.06 (requires --timeshift-auto)"
echo "-t, --timeshift-auto Automatically detect Timeshifts snapshot directory" echo "-t, --timeshift-auto Automatically detect Timeshifts snapshot directory"
@@ -59,7 +61,7 @@ log() {
logger -p user.notice -t ${0##*/}"["$$"]" "$1" logger -p user.notice -t ${0##*/}"["$$"]" "$1"
fi fi
if [ ${#logfile} -gt 1 ]; then if [ ${#logfile} -gt 1 ]; then
echo "$(date) ${1}" >> ${logfile} echo "$(date) ${1}" >> "${logfile}"
fi fi
} }
@@ -70,7 +72,7 @@ vlog() {
logger -p user.notice -t ${0##*/} "$1" logger -p user.notice -t ${0##*/} "$1"
fi fi
if [ ${#logfile} -gt 1 ]; then if [ ${#logfile} -gt 1 ]; then
echo "$(date) ${1}" >> ${logfile} echo "$(date) ${1}" >> "${logfile}"
fi fi
fi fi
} }
@@ -81,12 +83,12 @@ err() {
logger -p user.error -t ${0##*/} "$1" logger -p user.error -t ${0##*/} "$1"
fi fi
if [ ${#logfile} -gt 1 ]; then if [ ${#logfile} -gt 1 ]; then
echo "$(date) error: ${1}" >> ${logfile} echo "$(date) error: ${1}" >> "${logfile}"
fi fi
} }
# parse arguments # parse arguments
while getopts :l:ctvsh-: opt; do while getopts :l:ctvrsh-: opt; do
case "$opt" in case "$opt" in
-) -)
case "${OPTARG}" in case "${OPTARG}" in
@@ -105,6 +107,9 @@ while getopts :l:ctvsh-: opt; do
verbose) verbose)
verbose=true verbose=true
;; ;;
recursive)
recursive=true
;;
syslog) syslog)
syslog=true syslog=true
;; ;;
@@ -136,6 +141,9 @@ while getopts :l:ctvsh-: opt; do
v) v)
verbose=true verbose=true
;; ;;
r)
recursive=true
;;
s) s)
syslog=true syslog=true
;; ;;
@@ -176,6 +184,12 @@ else
inotify_qiet_flag=" -q -q " inotify_qiet_flag=" -q -q "
fi fi
if [ ${recursive} = true ]; then
inotify_recursive_flag=" -r "
else
inotify_recursive_flag=""
fi
if [ ${timeshift_auto} = false ] && [ ${timeshift_old} = true ]; then if [ ${timeshift_auto} = false ] && [ ${timeshift_old} = true ]; then
err "[!] Flag --timeshift-old requires flag --timeshift-auto" "${RED}" >&2 err "[!] Flag --timeshift-old requires flag --timeshift-auto" "${RED}" >&2
exit 1 exit 1
@@ -186,6 +200,7 @@ vlog "Snapshot directory: $snapshots"
vlog "Timestift autodetection: $timeshift_auto" vlog "Timestift autodetection: $timeshift_auto"
vlog "Timeshift old: $timeshift_old" vlog "Timeshift old: $timeshift_old"
vlog "Logfile: $logfile" vlog "Logfile: $logfile"
vlog "Recursive: $recursive"
if ! [ -d "$snapshots" ] && ! [ ${timeshift_auto} = true ]; then if ! [ -d "$snapshots" ] && ! [ ${timeshift_auto} = true ]; then
err "[!] No directory found at ${snapshots}" "${RED}" >&2 err "[!] No directory found at ${snapshots}" "${RED}" >&2
@@ -210,7 +225,7 @@ create_grub_menu() {
err "[!] Error during grub submenu creation (grub-btrfs error)" "${RED}" err "[!] Error during grub submenu creation (grub-btrfs error)" "${RED}"
fi fi
else else
if ${GRUB_BTRFS_MKCONFIG:-grub-mkconfig} -o ${GRUB_BTRFS_GRUB_DIRNAME:-/boot/grub}/grub.cfg; then if ${GRUB_BTRFS_MKCONFIG:-grub-mkconfig} -o "${GRUB_BTRFS_GRUB_DIRNAME:-/boot/grub}"/grub.cfg; then
log "Grub menu recreated" "${GREEN}" log "Grub menu recreated" "${GREEN}"
else else
err "[!] Error during grub menu creation (grub/ grub-btrfs error)" "${RED}" err "[!] Error during grub menu creation (grub/ grub-btrfs error)" "${RED}"
@@ -264,7 +279,7 @@ while true; do
vlog "Watching $snapshots for new snapshots..." "${CYAN}" vlog "Watching $snapshots for new snapshots..." "${CYAN}"
fi fi
runs=true runs=true
inotifywait ${inotify_qiet_flag} -e create -e delete -e unmount -t "$watchtime" "$snapshots" && { inotifywait ${inotify_qiet_flag} ${inotify_recursive_flag} -e create -e delete -e unmount -t "$watchtime" "$snapshots" && {
log "Detected snapshot creation/ deletion, recreating Grub menu" "${CYAN}" log "Detected snapshot creation/ deletion, recreating Grub menu" "${CYAN}"
sleep 5 sleep 5
create_grub_menu create_grub_menu
+5 -3
View File
@@ -9,7 +9,7 @@ when a new btrfs snapshot is created.
.SH "SYNOPSIS" .SH "SYNOPSIS"
.PP .PP
\fCgrub\-btrfsd [\-h, \-\-help] [\-c, \-\-no\-color] [\-l, \-\-log\-file LOG_FILE] [\-s, \-\-syslog] [\-t, \-\-timeshift\-auto] [\-o, \-\-timeshift\-old] [\-v, \-\-verbose] SNAPSHOTS_DIR\fP \fCgrub\-btrfsd [\-h, \-\-help] [\-c, \-\-no\-color] [\-l, \-\-log\-file LOG_FILE] [\-r, \-\-recursive] [\-s, \-\-syslog] [\-t, \-\-timeshift\-auto] [\-o, \-\-timeshift\-old] [\-v, \-\-verbose] SNAPSHOTS_DIR\fP
.SH "DESCRIPTION" .SH "DESCRIPTION"
.PP .PP
@@ -30,6 +30,10 @@ Disable colors in output.
.PP .PP
This arguments specifies a file where grub-btrfsd should write log messages. This arguments specifies a file where grub-btrfsd should write log messages.
.SS "\fC\-r / \-\-recursive\fP"
.PP
Watch snapshot directory recursively
.SS "\fC\-s / \-\-syslog\fP" .SS "\fC\-s / \-\-syslog\fP"
.PP .PP
Write to syslog Write to syslog
@@ -50,8 +54,6 @@ Let the log of the daemon be more verbose
.PP .PP
Displays a short help message. Displays a short help message.
.SH "CONFIGURATION" .SH "CONFIGURATION"
.PP .PP
The daemon is usually configured via the file \fC/etc/conf.d/grub\-btrfsd\fP on openrc-init systems and \fCsudo systemctl edit \-\-full grub\-btrfsd\fP on systemd systems. In this file the arguments (See OPTIONS), that OpenRC passes to the daemon when it is started, can be configured. The daemon is usually configured via the file \fC/etc/conf.d/grub\-btrfsd\fP on openrc-init systems and \fCsudo systemctl edit \-\-full grub\-btrfsd\fP on systemd systems. In this file the arguments (See OPTIONS), that OpenRC passes to the daemon when it is started, can be configured.
+4 -3
View File
@@ -10,7 +10,7 @@ grub-btrfsd - An OpenRC daemon to automatically update the grub menu with
when a new btrfs snapshot is created. when a new btrfs snapshot is created.
* SYNOPSIS * SYNOPSIS
~grub-btrfsd [-h, --help] [-c, --no-color] [-l, --log-file LOG_FILE] [-s, --syslog] [-t, --timeshift-auto] [-o, --timeshift-old] [-v, --verbose] SNAPSHOTS_DIR~ ~grub-btrfsd [-h, --help] [-c, --no-color] [-l, --log-file LOG_FILE] [-r, --recursive] [-s, --syslog] [-t, --timeshift-auto] [-o, --timeshift-old] [-v, --verbose] SNAPSHOTS_DIR~
* DESCRIPTION * DESCRIPTION
Grub-btrfsd is a shell script which is meant to be run as a daemon. Grub-btrfsd is a shell script which is meant to be run as a daemon.
@@ -27,6 +27,9 @@ Disable colors in output.
** ~-l / --log-file~ ** ~-l / --log-file~
This arguments specifies a file where grub-btrfsd should write log messages. This arguments specifies a file where grub-btrfsd should write log messages.
** ~-r / --recursive~
Watch snapshot directory recursively
** ~-s / --syslog~ ** ~-s / --syslog~
Write to syslog Write to syslog
@@ -42,8 +45,6 @@ Let the log of the daemon be more verbose
** ~-h / --help~ ** ~-h / --help~
Displays a short help message. Displays a short help message.
* CONFIGURATION * CONFIGURATION
The daemon is usually configured via the file ~/etc/conf.d/grub-btrfsd~ on openrc-init systems and ~sudo systemctl edit --full grub-btrfsd~ on systemd systems. In this file the arguments (See OPTIONS), that OpenRC passes to the daemon when it is started, can be configured. The daemon is usually configured via the file ~/etc/conf.d/grub-btrfsd~ on openrc-init systems and ~sudo systemctl edit --full grub-btrfsd~ on systemd systems. In this file the arguments (See OPTIONS), that OpenRC passes to the daemon when it is started, can be configured.