snap-pac/scripts/snap-pac
James Barnett 4b84410780 reorganize
2016-06-09 16:09:42 -05:00

92 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
if [[ -f /etc/conf.d/snapper ]]; then
source /etc/conf.d/snapper
else
echo "ERROR: /etc/conf.d/snapper does not exist!"
exit 1
fi
if [[ -z "$SNAPPER_CONFIGS" ]]; then
echo "WARNING: No snapper configurations found, so not taking any snapshots!"
exit 1
fi
declare -r pre_or_post=$1
declare pacman_cmd
pacman_cmd="$(sed 's./usr/bin/pacman.pacman.g' <(ps -C pacman -o args=))"
declare -i x=0
for CONFIG in $SNAPPER_CONFIGS; do
PACMAN_PRE_POST="no"
if [[ $CONFIG == "root" ]]; then
PACMAN_PRE_POST="yes"
fi
PACMAN_PRE_DESCRIPTION="$pacman_cmd"
PACMAN_POST_DESCRIPTION="$pacman_cmd"
PACMAN_CLEANUP_ALGORITHM="number"
# shellcheck source=/dev/null
source /etc/snapper/configs/"$CONFIG"
[[ $PACMAN_PRE_POST != "yes" ]] && continue
prefile="/tmp/snap-pac-pre_$CONFIG"
snapper_cmd="snapper --config $CONFIG create --type $pre_or_post --cleanup-algorithm $PACMAN_CLEANUP_ALGORITHM"
x=$((x+1))
if [[ "$pre_or_post" == "pre" ]]; then
$snapper_cmd --description "$PACMAN_PRE_DESCRIPTION" --print-number > "$prefile"
elif [[ -f $prefile ]]; then
$snapper_cmd --description "$PACMAN_POST_DESCRIPTION" --pre-number "$(< "$prefile")"
rm "$prefile"
else
echo "WARNING: $prefile does not exist, so no post snapshot will be taken. If you are initially installing snap-pac, this is normal."
fi
done
if [[ $x -eq 0 ]]; then
echo "WARNING: No snapper configurations are set up for snapshots to be taken!"
exit 1
fi
exit 0