#!/bin/bash backupcfg="/etc/backup.cfg" if [ $UID -ne 0 ]; then echo "Keine Rootrechte." exit 1 fi #test -f "${backupcfg}" && echo "${backupcfg} existiert" && exit 1 # find all disks declare -A disk_tmp=() declare -A disk=() index=0 for i in $(find /dev/disk/by-id/ -type l -printf "%P\n" | grep usb | grep -v part | tac ); do name="$(readlink -f /dev/disk/by-id/"${i}")" if [[ "${name}" =~ *"^[0-9]+$"* ]]; then continue fi if [[ "${name}" == *"dm"* ]]; then continue fi if [[ "${name}" == *"/dev/sr"* ]]; then continue fi if blkid "${name}"|grep -q UUID; then size="$(fdisk -l "${name}" | head -n1 | awk '{print $3}')" else continue fi size=$(awk "BEGIN { printf(\"%.0f\n\", ${size}); }") uuid=$(blkid -o value -s UUID ${name}) index=$(( index + 1 )) disk_tmp+=( [${index}.id]="${i}" [${index}.name]="${name}" [${index}.uuid]="${uuid}" [${index}.size]="${size}" ) done disk_tmp+=( [count]="${index}" ) if [[ "${disk_tmp[count]}" -eq 0 ]]; then echo "No Disk attached." exit 1 fi # show devices echo "Devices:" for i in $(seq 1 "${disk_tmp[count]}"); do echo "${disk_tmp[${i}.id]}" echo " - uuid: ${disk_tmp[${i}.uuid]}" echo " - name: ${disk_tmp[${i}.name]}" echo " - size: ${disk_tmp[${i}.size]}" done # choose device found= while read -p "Which Device? [${disk_tmp[1.uuid]}]: " output; do test -z "${output}" && output="${disk_tmp[1.uuid]}" for i in $(seq 1 "${disk_tmp[count]}"); do if [[ "${disk_tmp[${i}.id]}" == "${output}" ]] || [[ "${disk_tmp[${i}.name]}" == "${output}" ]] || [[ "${disk_tmp[${i}.uuid]}" == "${output}" ]]; then found=1 id="${disk_tmp[${i}.id]}" break fi done [[ "${found}" ]] && break echo "${output} not found" done echo "g n 1 w q" | fdisk "/dev/disk/by-id/${id}" UUID=$(blkid -o value -s UUID "/dev/disk/by-id/${id}-part1") 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."