greatly simplify script
This commit is contained in:
parent
2fb18c78f0
commit
825bbf89a7
4 changed files with 31 additions and 72 deletions
|
|
@ -27,4 +27,4 @@ Target = *
|
||||||
Description = snapper post snapshot
|
Description = snapper post snapshot
|
||||||
Depends = snapper
|
Depends = snapper
|
||||||
When = PostTransaction
|
When = PostTransaction
|
||||||
Exec = /usr/bin/bash -c ". /usr/share/libalpm/hooks.bin/snap-pac/snap-pac; post"
|
Exec = /usr/share/libalpm/hooks.bin/snap-pac/snap-pac post
|
||||||
|
|
|
||||||
|
|
@ -27,4 +27,4 @@ Target = *
|
||||||
Description = snapper pre snapshot
|
Description = snapper pre snapshot
|
||||||
Depends = snapper
|
Depends = snapper
|
||||||
When = PreTransaction
|
When = PreTransaction
|
||||||
Exec = /usr/bin/bash -c ". /usr/share/libalpm/hooks.bin/snap-pac/snap-pac; pre"
|
Exec = /usr/share/libalpm/hooks.bin/snap-pac/snap-pac pre
|
||||||
|
|
|
||||||
|
|
@ -24,4 +24,4 @@ Target = snap-pac
|
||||||
[Action]
|
[Action]
|
||||||
Description = snap-pac cleanup upon removal
|
Description = snap-pac cleanup upon removal
|
||||||
When = PreTransaction
|
When = PreTransaction
|
||||||
Exec = /usr/bin/bash -c ". /usr/share/libalpm/hooks.bin/snap-pac/snap-pac; clean"
|
Exec = /usr/bin/bash -c "rm /usr/share/libalpm/hooks.bin/snap-pac/.pre*; echo 'No post snapshot will be performed, since you are removing the pacman hooks.'"
|
||||||
|
|
|
||||||
83
snap-pac
83
snap-pac
|
|
@ -18,91 +18,50 @@
|
||||||
# with this program; if not, write to the Free Software Foundation, Inc.,
|
# with this program; if not, write to the Free Software Foundation, Inc.,
|
||||||
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
||||||
|
|
||||||
# Main script. Is sourced in hooks and functions are called.
|
# Main script.
|
||||||
# e.g.: bash -c ". snap-pac; pre"
|
|
||||||
|
|
||||||
set -e
|
set -e
|
||||||
|
|
||||||
if [[ $EUID -ne 0 ]]; then
|
if [[ "$1" != "pre" ]] && [[ "$1" != "post" ]]; then
|
||||||
echo "snap-pac must be sourced as root."
|
echo "ERROR: First argument should either be 'pre' or 'post'."
|
||||||
exit
|
exit
|
||||||
fi
|
fi
|
||||||
|
|
||||||
if [[ "${BASH_SOURCE[0]}" == "${0}" ]]; then
|
|
||||||
echo "This script is intended to be sourced from a pacman hook and not run on its own."
|
|
||||||
exit
|
|
||||||
fi
|
|
||||||
|
|
||||||
# Have to store pre snapshot number in a file, since the script is run in a
|
|
||||||
# subshell. Exporting a variable doesn't seem to work
|
|
||||||
declare -r prefile_prefix="/usr/share/libalpm/hooks.bin/snap-pac/.pre"
|
|
||||||
|
|
||||||
# find any variables set in the snapper configuration files as well as set
|
|
||||||
# pacman_cmd
|
|
||||||
function read_configs
|
|
||||||
{
|
|
||||||
declare -r pre_or_post=$1
|
declare -r pre_or_post=$1
|
||||||
|
declare -r prefile_prefix="/usr/share/libalpm/hooks.bin/snap-pac/.pre"
|
||||||
# pacman command that called the hook in the first place
|
|
||||||
declare -r pacman_cmd="$(echo $(ps -C pacman -o args=) | sed 'sX/usr/bin/pacmanXpacmanXg')"
|
declare -r pacman_cmd="$(echo $(ps -C pacman -o args=) | sed 'sX/usr/bin/pacmanXpacmanXg')"
|
||||||
|
|
||||||
declare -r snapper_config_dir="/etc/snapper/configs"
|
declare -r snapper_config_dir="/etc/snapper/configs"
|
||||||
configurations="$(ls $snapper_config_dir)"
|
declare -r configurations="$(ls $snapper_config_dir)"
|
||||||
|
|
||||||
declare -i i=0
|
|
||||||
for x in $configurations; do
|
for x in $configurations; do
|
||||||
|
|
||||||
source $snapper_config_dir/$x
|
source $snapper_config_dir/$x
|
||||||
|
|
||||||
if [[ $x == "root" ]]; then
|
if [[ $x == "root" ]]; then
|
||||||
take_snapshot[i]=${PACMAN_PRE_POST:-"yes"}
|
take_snapshot=${PACMAN_PRE_POST:-"yes"}
|
||||||
else
|
else
|
||||||
take_snapshot[i]=${PACMAN_PRE_POST:-"no"}
|
take_snapshot=${PACMAN_PRE_POST:-"no"}
|
||||||
fi
|
fi
|
||||||
|
cleanupalgo=${PACMAN_CLEANUP_ALGORITHM:-"number"}
|
||||||
|
pre_description=${PACMAN_PRE_DESCRIPTION:-"$pacman_cmd"}
|
||||||
|
post_description=${PACMAN_POST_DESCRIPTION:-"$pacman_cmd"}
|
||||||
|
|
||||||
cleanupalgo[i]=${PACMAN_CLEANUP_ALGORITHM:-"number"}
|
if [[ $take_snapshot == "yes" ]]; then
|
||||||
|
|
||||||
if [[ $pre_or_post == "pre" ]]; then
|
if [[ "$pre_or_post" == "pre" ]]; then
|
||||||
description[i]=${PACMAN_PRE_DESCRIPTION:-"$pacman_cmd"}
|
|
||||||
else
|
|
||||||
description[i]=${PACMAN_POST_DESCRIPTION:-"$pacman_cmd"}
|
|
||||||
fi
|
|
||||||
|
|
||||||
i=$(($i+1))
|
snapper --config $x create --type pre --cleanup-algorithm $cleanupalgo --print-number --description "$post_description" > $prefile_prefix"_"$x
|
||||||
done
|
|
||||||
}
|
|
||||||
|
|
||||||
function pre
|
elif [[ "$pre_or_post" == "post" ]]; then
|
||||||
{
|
|
||||||
read_configs pre
|
|
||||||
declare -i i=0
|
|
||||||
for x in $configurations; do
|
|
||||||
if [[ ${take_snapshot[i]} == "yes" ]]; then
|
|
||||||
snapper --config $x create --type pre --cleanup-algorithm ${cleanupalgo[i]} --print-number --description "${description[i]}" > $prefile_prefix"_"$x
|
|
||||||
fi
|
|
||||||
i=$(($i+1))
|
|
||||||
done
|
|
||||||
}
|
|
||||||
|
|
||||||
function post
|
|
||||||
{
|
|
||||||
read_configs post
|
|
||||||
declare -i i=0
|
|
||||||
for x in $configurations; do
|
|
||||||
if [[ ${take_snapshot[i]} == "yes" ]]; then
|
|
||||||
if [ -f $prefile_prefix"_"$x ]; then
|
if [ -f $prefile_prefix"_"$x ]; then
|
||||||
snapper --config $x create --cleanup-algorithm ${cleanupalgo[i]} --type post --pre-number $(cat $prefile_prefix"_"$x) --description "${description[i]}"
|
snapper --config $x create --cleanup-algorithm $cleanupalgo --type post --pre-number $(cat $prefile_prefix"_"$x) --description "$pre_description"
|
||||||
rm $prefile_prefix"_"$x
|
rm $prefile_prefix"_"$x
|
||||||
else
|
else
|
||||||
echo "WARNING: $prefile_prefix"_"$x does not exist, so no post snapshot for $x configuration will be taken. If you are initially installing snap-pac, this is normal."
|
echo "WARNING: $prefile_prefix"_"$x does not exist, so no post snapshot will be taken. If you are initially installing snap-pac, this is normal."
|
||||||
fi
|
fi
|
||||||
fi
|
|
||||||
i=$(($i+1))
|
|
||||||
done
|
|
||||||
}
|
|
||||||
|
|
||||||
function clean
|
fi
|
||||||
{
|
|
||||||
rm /usr/share/libalpm/hooks.bin/snap-pac/.pre*
|
fi
|
||||||
echo "NOTE: No post snapshot will be performed for this transaction, since you are removing the pacman hooks."
|
|
||||||
}
|
done
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue