snap-pac/snap-pac
2016-04-26 14:35:02 -05:00

90 lines
2.6 KiB
Bash
Executable file

#!/bin/bash
# snap-pac
# https://github.com/wesbarnett/snap-pac
# Copyright (C) 2016 James W. Barnett
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
# You should have received a copy of the GNU General Public License along
# with this program; if not, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
# Main script.
set -e
if [[ $EUID -ne 0 ]]; then
echo "ERROR: Script must be run as root."
exit 1
fi
if [[ $# -ne 1 ]]; then
echo "ERROR: Only one argument should be passed to this script."
exit 1
fi
if [[ $1 != "pre" ]] && [[ $1 != "post" ]]; then
echo "ERROR: First argument should either be 'pre' or 'post'."
exit 1
fi
declare -r pre_or_post=$1
declare -r prefile_prefix="/tmp/snap-pac-pre"
declare -r pacman_cmd="$(sed 's./usr/bin/pacman.pacman.g' <(ps -C pacman -o args=))"
declare -r snapper_config_dir="/etc/snapper/configs"
declare -r configurations="$(find $snapper_config_dir/* -printf '%f\t\0')"
for x in $configurations; do
(
source $snapper_config_dir/$x
if [[ $x == "root" ]]; then
take_snapshot=${PACMAN_PRE_POST:-"yes"}
else
take_snapshot=${PACMAN_PRE_POST:-"no"}
fi
[[ $take_snapshot != "yes" ]] && exit 0
cleanupalgo=${PACMAN_CLEANUP_ALGORITHM:-"number"}
if [[ "$pre_or_post" == "pre" ]]; then
description=${PACMAN_PRE_DESCRIPTION:-"$pacman_cmd"}
snapper --config $x create --type $pre_or_post --cleanup-algorithm $cleanupalgo --print-number --description "$description" > $prefile_prefix"_"$x
exit 0
elif [[ "$pre_or_post" == "post" ]]; then
if [ -f $prefile_prefix"_"$x ]; then
description=${PACMAN_POST_DESCRIPTION:-"$pacman_cmd"}
snapper --config $x create --type $pre_or_post --cleanup-algorithm $cleanupalgo --pre-number $(< $prefile_prefix"_"$x) --description "$description"
rm $prefile_prefix"_"$x
exit 0
else
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."
exit 1
fi
fi
) &
done
exit 0