vinstaller/files/bin/void-createbackupcontainer
2022-02-21 10:55:12 +01:00

87 lines
2.4 KiB
Bash
Executable File

#!/bin/bash
backupcfg="/etc/backup.cfg"
if [ $UID -ne 0 ]; then
echo "Keine Rootrechte."
exit 1
fi
input() {
output=""
echo "${1}"
read -r -p "[${2}]: " output
test -z "${output}" && output="${2}"
return 0
}
multiplechoice() {
output=""
local done=""
local input=("${@}")
#for i in $(seq 1 "$(( ${#input[@]} - 1 ))"); do
for i in $(seq 0 "$(( ${#input[@]} - 1 ))"); do
if [[ "${i}" == "0" ]]; then
choices="${input[$i]}"
use="${input[$i]}"
else
choices="${choices}|${input[$i]}"
use+=" ${input[$i]}"
fi
done
while input "[${choices}]: " "${input[0]}"; do
for i in $(seq 0 "${#input[@]}"); do
if [[ "${output}" == "${input[$i]}" ]] || [[ "${output}" == "${input[$i]:0:2}" ]]; then
output="${input[$i]}"
done=1
fi
done
if [[ -z "${done}" ]]; then
echo wronginput "${output}"
echo use these: "${use}"
else
break
fi
done
}
echo "Bitte Backupgerät noch nicht anschliessen!"
read
DISKS_DETAILS=$(lsblk -l -o KNAME,TYPE,SIZE,MODEL|grep disk)
echo "${DISKS_DETAILS}"
echo ""
echo "Nun das Gerät anschliessen und das neue in der Liste auswaehlen"
read
DISKS_DETAILS=$(lsblk -l -o KNAME,TYPE,SIZE,MODEL|grep disk)
echo "${DISKS_DETAILS}"
multiplechoice $(echo "${DISKS_DETAILS}"|awk '{print $1}')
TARGET_PHY_DISK="/dev/${output}"
parted -a optimal "${TARGET_PHY_DISK}" -s -- mklabel gpt
sleep 5
parted -a optimal "${TARGET_PHY_DISK}" unit mib -s -- mkpart backup btrfs 1 100%
sleep 5
sync
if ! cryptsetup luksFormat "${TARGET_PHY_DISK}1"; then
echo "Konnte ${TARGET_PHY_DISK} nicht verschluesseln."
exit 1
else
UUID="$(lsblk -l -o KNAME,UUID | grep "$(basename ${TARGET_PHY_DISK})1"|awk '{print $2}')"
if ! cryptsetup open "/dev/disk/by-uuid/${UUID}" "luks-${UUID}"; then
echo "Konnte /dev/disk/by-uuid/${UUID} nicht oeffnen."
exit 1
fi
fi
if ! mkfs.btrfs -f "/dev/mapper/luks-${UUID}"; then
echo "Konnte /dev/mapper/luks-${UUID} nicht formatieren"
exit 1
fi
cryptsetup close "luks-${UUID}"
echo "UUID=\"${UUID}\"" > "${backupcfg}"
echo "Erstellen des Containers fertig."