#!/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="${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."