94 lines
2.6 KiB
Bash
94 lines
2.6 KiB
Bash
#!/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
|
|
}
|
|
|
|
clear
|
|
echo "Bitte Backupgerät noch nicht anschliessen oder wieder entfernen!"
|
|
echo "Dücke Enter"
|
|
read
|
|
DISKS_DETAILS=$(lsblk -l -o KNAME,TYPE,SIZE,MODEL|grep disk)
|
|
echo "${DISKS_DETAILS}"
|
|
echo ""
|
|
echo "Nun das Gerät anschliessen"
|
|
echo "Vergleiche diese Ausgabe mit der folgenden"
|
|
echo "Drücke Enter"
|
|
read
|
|
DISKS_DETAILS=$(lsblk -l -o KNAME,TYPE,SIZE,MODEL|grep disk)
|
|
echo "${DISKS_DETAILS}"
|
|
echo ""
|
|
echo "Welches kam neu hinzu?"
|
|
DIFF=$(diff <(echo "$a") <(echo "$b")|tail -n-1|awk '{print $2}')
|
|
echo "Vermutlich '${DIFF}'"
|
|
multiplechoice $(echo "${DISKS_DETAILS}"|awk '{print $1}')
|
|
TARGET_PHY_DISK="${output}"
|
|
|
|
|
|
parted "${TARGET_PHY_ID}" -s -- mklabel gpt
|
|
parted "${TARGET_PHY_ID}" unit mib -s -- mkpart root btrfs 1 100%
|
|
|
|
UUID=$(blkid -o value -s UUID "/dev/${TARGET_PHY_ID}1")
|
|
|
|
if ! cryptsetup luksFormat UUID="${UUID}"; then
|
|
echo "Konnte /dev/disk/by-uuid/${UUID} nicht verschluesseln."
|
|
exit 1
|
|
else
|
|
if ! cryptsetup open UUID="${UUID}" "luks-${UUID}"; then
|
|
echo "Konnte /dev/disk/by-uuid/${UUID} nicht verschluesseln."
|
|
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."
|