133 lines
3.1 KiB
Bash
133 lines
3.1 KiB
Bash
#!/bin/bash
|
|
export LANG="en_US.UTF-8"
|
|
backupcfg="/etc/backup.cfg"
|
|
ziel="/var/lib/backup/ziel"
|
|
cfg="/etc/btrbk/btrbk.conf.void"
|
|
|
|
fhelp() {
|
|
echo "Nutze es so:"
|
|
echo "sudo backup"
|
|
echo "sudo backup poweroff (um den Rechner nach dem Backup herunterzufahren.)"
|
|
#echo "sudo backup update (um den Rechner nach dem Backup up zu daten.)"
|
|
#echo "update und poweroff sind mixbar"
|
|
echo "sudo backup passwd (um das Passwort für die Backupfestplatte zu ändern.)"
|
|
}
|
|
|
|
if [ "${UID}" -ne "0" ]; then
|
|
echo "Keine Rootrechte."
|
|
exit 1
|
|
fi
|
|
|
|
for argval in "$@"
|
|
do
|
|
case "${argval}" in
|
|
power|poweroff|p)
|
|
poweroff=y
|
|
;;
|
|
help|-h|--help|h)
|
|
fhelp
|
|
exit
|
|
;;
|
|
update) update=y ;;
|
|
passwd) passwd=y ;;
|
|
clean) do=clean ;;
|
|
run) do=run ;;
|
|
esac
|
|
done
|
|
|
|
test -z "${do}" && do=run
|
|
|
|
if [[ ! -f /etc/btrbk/btrbk.conf ]]; then
|
|
if [[ -f "${backupcfg}" ]]; then
|
|
source "${backupcfg}"
|
|
else
|
|
echo "${backupcfg} fehlt."
|
|
echo "Diese Datei wird normalerweise durch \`void-createbackupcontainer\`"
|
|
echo "angelegt. Bitte Sprich mit deinem Systemadministrator."
|
|
fi
|
|
|
|
if [ -z "${UUID}" ]; then
|
|
echo "Keine Backupplatte angegeben."
|
|
exit 1
|
|
fi
|
|
|
|
if [ ! -e "/dev/disk/by-uuid/${UUID}" ]; then
|
|
echo "Bitte Backupfestplatte anschliessen."
|
|
exit 1
|
|
fi
|
|
|
|
if [ -e /tmp/backup ]; then
|
|
echo "Es läuft schon ein Backupvorgang oder wurde nicht richtig beendet."
|
|
echo "Bei letzterem: 'sudo rm -rf /tmp/backup'"
|
|
exit 1
|
|
fi
|
|
touch /tmp/backup
|
|
|
|
if [[ "${passwd}" == "y" ]]; then
|
|
cryptsetup luksChangeKey "/dev/disk/by-uuid/${UUID}"
|
|
rm -rf /tmp/backup
|
|
exit 0
|
|
fi
|
|
|
|
function finish {
|
|
sync
|
|
if [ "${poweroff}" = "y" ]; then
|
|
shutdown -h now
|
|
fi
|
|
sleep 4
|
|
umount "${ziel}"
|
|
echo " "
|
|
echo " "
|
|
if [[ "${MOUNTEDBY}" == "script" ]]; then
|
|
cryptsetup close "luks-${UUID}"
|
|
echo "Festplatte kann nun sicher entfernt werden."
|
|
elif [[ "${MOUNTEDBY}" == "de" ]]; then
|
|
echo "Festplatte bitte mit der grafischen Oberflaeche auswerfen (wie ein USB-Stick)"
|
|
echo ""
|
|
echo "oder:"
|
|
echo "sudo umount /dev/mapper/luks-${UUID}"
|
|
echo "sudo cryptsetup close luks-${UUID}"
|
|
echo ""
|
|
fi
|
|
rm -rf /tmp/backup
|
|
echo "FERTIG"
|
|
|
|
}
|
|
trap finish EXIT
|
|
|
|
if mountpoint -q -x "/dev/mapper/luks-${UUID}" > /dev/null; then
|
|
MOUNTEDBY="de"
|
|
else
|
|
if ! cryptsetup open UUID="${UUID}" "luks-${UUID}"; then
|
|
echo "Konnte /dev/disk/by-uuid/${UUID} nicht öffnen."
|
|
exit
|
|
fi
|
|
MOUNTEDBY="script"
|
|
fi
|
|
|
|
if ! mount "/dev/mapper/luks-${UUID}" "${ziel}"; then
|
|
echo "Konnte /dev/mapper/luks-${UUID} in ${ziel} nicht mounten."
|
|
exit
|
|
fi
|
|
else
|
|
cfg="/etc/btrbk/btrbk.conf"
|
|
fi
|
|
|
|
if mountpoint -q /var/lib/backup/quelle/bootfs; then
|
|
mkdir -p /var/lib/backup/quelle/bootfs/system/backup "/var/lib/backup/ziel/void/bootfs"
|
|
boot="backupboot"
|
|
fi
|
|
|
|
mkdir -p /var/lib/backup/quelle/rootfs/system/backup "/var/lib/backup/ziel/void/rootfs" "/var/lib/backup/ziel/void/homefs"
|
|
for t in backup "${boot}"; do
|
|
if [[ "${t}" ]]; then
|
|
btrbk --config="${cfg}" --progress --quiet clean "${t}"
|
|
if ! btrbk --config="${cfg}" --progress --quiet "${do}" "${t}"; then
|
|
btrbk --config="${cfg}" --progress --quiet clean
|
|
echo "Wegen Fehler nicht herunterfahren."
|
|
poweroff=n
|
|
exit
|
|
fi
|
|
fi
|
|
done
|