~q3cpma/scripts

5195feaa8dd0db3f106b05db1009e84c52c64730 — q3cpma 4 months ago 88ddc36
Modularize backup.sh and remove grub_partuuid.sh
4 files changed, 75 insertions(+), 138 deletions(-)

M backup.sh
D grub_partuuid.sh
M kernel_clean.sh
M kernel_update.sh
M backup.sh => backup.sh +71 -50
@@ 66,60 66,81 @@ requirebin findmnt vgimport vgexport lvchange
[ $(id -u) -ne 0 ] && die "Permission denied"
PATH=$selfdir:$PATH
home=$(eval echo ~$(logname))
backup_dir="$home"/mnt.backup/

backup_mount()
{
	if ! findmnt -- "$backup_dir" >/dev/null
	then
		vgimport backup_vg
		lvchange -a y backup_vg
		mkdir "$backup_dir"
		mount /dev/backup_vg/backup_lv "$backup_dir"
	fi
}

backup_dir="$home"/mnt.backup/
if ! findmnt -- "$backup_dir" >/dev/null
then
	vgimport backup_vg
	lvchange -a y backup_vg
	mkdir "$backup_dir"
	mount /dev/backup_vg/backup_lv "$backup_dir"
fi
backup_umount()
{
	if findmnt -- "$backup_dir" >/dev/null
	then
		umount "$backup_dir"
		rmdir "$backup_dir"
		lvchange -a n backup_vg
		vgexport backup_vg
	fi
}

# Cleanup before backup
rm -rf "$home"/.local/share/qutebrowser/webengine \
	"$home"/.local/share/qutebrowser/history.sqlite \
	"$home"/.cache/qutebrowser \
	"$home"/.local/share/qutebrowser/history.sqlite \
	"$home"/.cache/darktable \
	"$home"/.cache/sxiv \
	"$home"/.cache/thumbnails
if command -v emerge >/dev/null # Gentoo
then
	emerge --depclean
	command -v eclean-pkg  >/dev/null && eclean-pkg -d
	command -v eclean-dist >/dev/null && eclean-dist -d
	kernel_clean.sh
fi
backup_clean()
{
	# Cleanup before backup
	rm -rf "$home"/.local/share/qutebrowser/webengine \
		"$home"/.local/share/qutebrowser/history.sqlite \
		"$home"/.cache/qutebrowser \
		"$home"/.local/share/qutebrowser/history.sqlite \
		"$home"/.cache/darktable \
		"$home"/.cache/sxiv \
		"$home"/.cache/thumbnails
	if command -v emerge >/dev/null # Gentoo
	then
		emerge --depclean
		command -v eclean-pkg  >/dev/null && eclean-pkg -d
		command -v eclean-dist >/dev/null && eclean-dist -d
		kernel_clean.sh
	fi
}

command -v ionice >/dev/null && ionice='ionice -c idle'
set +e
${ionice:-} rsync \
	--archive --acls --xattrs --human-readable --partial --progress --verbose \
	--sparse --inplace --delete-before --info=progress2 \
	--exclude /dev				    \
	--exclude /proc					\
	--exclude /sys					\
	--exclude /tmp					\
	--exclude /run					\
	--exclude /mnt					\
	--exclude /media				\
	--exclude /var/cache			\
	--exclude /var/tmp				\
	--exclude lost+found			\
	--exclude "$home/mnt.*"			\
	/ "$backup_dir"
status=$?
set -e
backup_update()
{
	command -v ionice >/dev/null && ionice='ionice -c idle'
	set +e
	${ionice:-} rsync \
		--archive --acls --xattrs --human-readable --partial --progress --verbose \
		--sparse --inplace --delete-before --info=progress2 \
		--exclude /dev				    \
		--exclude /proc					\
		--exclude /sys					\
		--exclude /tmp					\
		--exclude /run					\
		--exclude /mnt					\
		--exclude /media				\
		--exclude /var/cache			\
		--exclude /var/tmp				\
		--exclude lost+found			\
		--exclude "$home/mnt.*"			\
		/ "$backup_dir"
	status=$?
	set -e
}

if [ $status -ne 0 ]
if [ $# -eq 0 ]
then
	backup_mount && backup_clean && backup_update && backup_umount
elif [ $# -eq 1 ]
then
	pecho "Unmount device ? y/n"
	read -r ans
	[ "$(tolower "$ans")" != y ] && exit
	case "$1" in
		mount|umount|clean|update) eval backup_"$1" ;;
		*) die "$1: unknown action";;
	esac
else
	die "Usage: $(basename "$(preadlinkf "$0")") [mount|umount|clean|update]"
fi
umount "$backup_dir"
rmdir "$backup_dir"
lvchange -a n backup_vg
vgexport backup_vg

D grub_partuuid.sh => grub_partuuid.sh +0 -84
@@ 1,84 0,0 @@
#!/bin/sh -eu
# Dependencies: util.sh, blkid, findmnt, grub
# Portability:  Linux

# Check if local or typeset is available
if ! sh -c 'fun() { local a=1; }; fun'
then
	if sh -c 'fun() { typeset a=1; }; fun'
	then
		alias local=typeset
	else
		echo "local/typeset not supported by sh, aborting"
		exit 1
	fi
fi

if command readlink --version 2>/dev/null | grep -qF '^readlink (GNU coreutils)'
then
	alias preadlinkf='readlink -f --'
elif command -v greadlink >/dev/null
then
	alias preadlinkf='greadlink -f --'
else
	# Portable readlink -f using GNU's semantics (last component need not exist)
	# Currently passes the tests from https://github.com/ko1nksm/preadlinkf
	# except loop detection (`getconf SYMLOOP_MAX` is undefined here, anyway)
	preadlinkf()
	{
		[ $# -eq 0 ] && return 1
		local i= status=0 pwd="$(pwd -P)" base= dir=
		for i
		do
			! [ "$i" ] && { status=1; continue; }
			if [ -d "$i" ]
			then
				CDPATH= cd -P -- "$i" >/dev/null 2>&1 || return 1
				pwd -P
			else
				case "$i" in */) [ -e "${i%/}" ] && return 1;; esac
				while true
				do
					CDPATH= cd -P -- "$(dirname -- "$i")" >/dev/null 2>&1 || return 1
					base=${i%/}
					base=${base##*/}
					if [ -L "$base" ]
					then
						i=$(ls -l -- "$base")
						i=${i#*"$base -> "}
					else
						dir=$(pwd -P)
						dir=${dir%/}
						printf '%s/%s\n' "$dir" "$base"
						break
					fi
				done
			fi
			cd -- "$pwd"
		done
		return $status
	}
fi
selfdir=$(dirname -- "$(preadlinkf "$0")")
. "$selfdir"/util.sh
requirebin lsblk grub-mkconfig
[ $(id -u) -ne 0 ] && die "Permission denied"


if [ $# -ne 0 ]
then
	name=$(basename "$(preadlinkf "$0")")
	cat <<EOF | text_format
**NAME**
    $name - grub-mkconfig using PARTUUID for the root device

**SYNOPSIS**
    $name [**-h**]

EOF
	exit 1
fi

puuid=$(blkid -o value -s PARTUUID "$(findmnt -o SOURCE -n /)")
grub-mkconfig | sed "s#\(linux.*root=\)[^ ]*#\1PARTUUID=$puuid#" \
	>/boot/grub/grub.cfg

M kernel_clean.sh => kernel_clean.sh +2 -2
@@ 1,5 1,5 @@
#!/bin/sh -eu
# Dependencies: util.sh, grub_partuuid.sh
# Dependencies: util.sh
# Portability:  Linux

# Check if local or typeset is available


@@ 90,4 90,4 @@ do
	rm -r -- "/lib/modules/$rel" "/usr/src/linux-$rel"
	rm -- "/boot/System.map-$rel" "/boot/config-$rel" "/boot/vmlinuz-$rel"
done
grub_partuuid.sh
grub-mkconfig -o /boot/grub/grub.cfg

M kernel_update.sh => kernel_update.sh +2 -2
@@ 1,5 1,5 @@
#!/bin/sh -eu
# Dependencies: util.sh, grub_partuuid.sh, eselect, patch (optional)
# Dependencies: util.sh, eselect, patch (optional)
# Portability:  Gentoo Linux

# Check if local or typeset is available


@@ 101,4 101,4 @@ make -j$(numcpu) LD=ld.bfd
make -j$(numcpu) LD=ld.bfd modules
make install
make modules_install
grub_partuuid.sh
grub-mkconfig -o /boot/grub/grub.cfg