2020-07-04 04:32:57 +02:00
#!/bin/bash
if [ $# -eq 0 ] || [ " $1 " = "-h" ] || [ " $1 " = "-help" ] ; then
echo "# adding and removing a backup device (usb thumbdrive)"
echo "# blitz.backupdevice.sh status"
2020-07-04 13:43:21 +02:00
echo "# blitz.backupdevice.sh on [?DEVICEUUID|DEVICENAME]"
2020-07-04 04:32:57 +02:00
echo "# blitz.backupdevice.sh off"
2020-07-04 13:00:35 +02:00
echo "# blitz.backupdevice.sh mount"
2020-07-04 04:32:57 +02:00
echo "error='missing parameters'"
exit 1
fi
2020-07-04 13:48:11 +02:00
echo "### blitz.backupdevice.sh ###"
2020-07-04 04:32:57 +02:00
source /mnt/hdd/raspiblitz.conf
#########################
# STATUS
#########################
if [ " $1 " = "status" ] ; then
2020-07-04 13:43:21 +02:00
if [ " ${ localBackupDeviceUUID } " != "" ] && [ " ${ localBackupDeviceUUID } " != "off" ] ; then
2020-07-04 13:00:35 +02:00
echo "backupdevice=1"
echo " UUID=' ${ localBackupDeviceUUID } ' "
2021-08-27 03:59:21 -04:00
# check if backup device is mounted
2020-07-04 13:00:35 +02:00
backupDeviceExists = $( df | grep -c "/mnt/backup" )
if [ ${ backupDeviceExists } -gt 0 ] ; then
echo "isMounted=1"
else
echo "isMounted=0"
fi
2020-07-04 04:32:57 +02:00
else
2020-07-04 13:00:35 +02:00
echo "backupdevice=0"
2020-11-11 13:53:09 +01:00
# get info on possible already existing BTRFS RAID1 usb drive
source <( sudo /home/admin/config.scripts/blitz.datadrive.sh status)
2020-07-04 13:43:21 +02:00
# get all the devices that are not mounted and possible candidates
drivecounter = 0
for disk in $( lsblk -o NAME,TYPE | grep "disk" | awk '$1=$1' | cut -d " " -f 1)
do
devMounted = $( lsblk -o MOUNTPOINT,NAME | grep " $disk " | grep -c "^/" )
2021-08-27 03:59:21 -04:00
# is raid candidate when: not mounted & not the data drive candidate (hdd/ssd) & not BTRFS RAID
2020-11-11 13:53:09 +01:00
if [ ${ devMounted } -eq 0 ] && [ " ${ disk } " != " ${ hdd } " ] && [ " ${ disk } " != " ${ raidUsbDev } " ] ; then
2020-07-04 13:43:21 +02:00
sizeBytes = $( lsblk -o NAME,SIZE -b | grep " ^ ${ disk } " | awk '$1=$1' | cut -d " " -f 2)
sizeGigaBytes = $( echo " scale=0; ${ sizeBytes } /1024/1024/1024 " | bc -l)
vedorname = $( lsblk -o NAME,VENDOR | grep " ^ ${ disk } " | awk '$1=$1' | cut -d " " -f 2)
mountoption = " ${ disk } ${ sizeGigaBytes } GB ${ vedorname } "
echo " backupCandidate[ ${ drivecounter } ]=' ${ mountoption } ' "
drivecounter = $(( $drivecounter + 1 ))
fi
done
echo " backupCandidates= ${ drivecounter } "
2020-07-04 04:32:57 +02:00
fi
2020-07-04 04:42:34 +02:00
exit 0
2020-07-04 04:32:57 +02:00
fi
2020-07-04 13:45:46 +02:00
# check if started with sudo
if [ " $EUID " -ne 0 ] ; then
echo "error='missing sudo'"
exit 1
fi
2020-07-04 04:32:57 +02:00
#########################
# TURN ON
#########################
if [ " $1 " = "on" ] ; then
echo "# BACKUP DEVICE ADD"
2020-07-04 15:28:32 +02:00
userinteraction = 0
2020-07-04 13:00:35 +02:00
# select and format device if UUID is not given
uuid = $2
if [ " ${ uuid } " = = "" ] ; then
2020-07-04 15:28:32 +02:00
userinteraction = 1
2020-07-04 14:03:41 +02:00
# get status data
source <( sudo /home/admin/config.scripts/blitz.backupdevice.sh status)
if [ ${ backupdevice } -eq 1 ] ; then
echo "error='already on'"
exit 1
fi
2021-08-27 03:59:21 -04:00
# check if backup device is already connected
2020-07-04 14:41:48 +02:00
if [ ${ backupCandidates } -eq 0 ] ; then
2020-07-04 16:06:14 +02:00
dialog --title ' Adding Backup Device ' --msgbox 'Please connect now the backup device\nFor example a thumb drive bigger than 120 MB.\nDont use a second HDD/SSD for that.\nBest on a USB2 port (not the blue ones).\nThen press OK.' 9 50
2020-07-04 15:06:21 +02:00
clear
2020-07-04 15:05:12 +02:00
echo
2020-07-04 15:05:46 +02:00
echo "detecting device ... (please wait)"
2020-07-04 14:58:56 +02:00
sleep 3
2020-07-04 14:41:48 +02:00
source <( sudo /home/admin/config.scripts/blitz.backupdevice.sh status)
if [ ${ backupCandidates } -eq 0 ] ; then
2021-08-27 03:59:21 -04:00
dialog --title ' FAIL ' --msgbox 'NOT able to detect a possible backup device.\nProcess was not successful.' 6 50
2020-07-04 15:08:51 +02:00
clear
2020-07-04 14:41:48 +02:00
exit 1
fi
fi
2020-07-04 14:03:41 +02:00
2020-07-04 14:41:48 +02:00
# check if there is only one candidate
if [ ${ backupCandidates } -gt 1 ] ; then
2020-07-04 14:58:56 +02:00
dialog --title ' FAIL ' --msgbox 'There is more then one possible backup target connected.\nMake sure that just that one device is connected and try again.' 8 40
2020-07-04 15:08:51 +02:00
clear
2020-07-04 14:41:48 +02:00
exit 1
fi
2020-07-04 14:03:41 +02:00
2020-07-04 15:33:19 +02:00
whiptail --title " FORMATTING DEVICE " --yes-button "FORMAT" --no-button "Cancel" --yesno " Will format the following device as Backup drive:
2020-07-04 15:34:51 +02:00
---> ${ backupCandidate [0] }
2020-07-04 15:28:32 +02:00
THIS WILL DELETE ALL DATA ON THAT DEVICE!
2020-07-04 15:33:19 +02:00
" 10 55
if [ $? -gt 0 ] ; then
2020-07-04 15:28:32 +02:00
echo "# CANCEL"
exit 1
fi
2020-07-04 14:58:56 +02:00
uuid = $( echo " ${ backupCandidate [0] } " | cut -d " " -f 1)
2020-07-04 15:34:51 +02:00
echo " # will format device ${ uuid } "
2020-07-04 13:00:35 +02:00
fi
# check that device is connected
uuidConnected = $( lsblk -o UUID | grep -c " ${ uuid } " )
if [ ${ uuidConnected } -eq 0 ] ; then
2020-07-04 13:43:21 +02:00
echo "# UUID not found - test is its a valid device name like sdb ..."
isDeviceName = $( lsblk -o NAME,TYPE | grep "disk" | awk '$1=$1' | cut -d " " -f 1 | grep -c " ${ uuid } " )
if [ ${ isDeviceName } -eq 1 ] ; then
hdd = " ${ uuid } "
2020-07-04 13:53:20 +02:00
# check if mounted
2020-09-23 17:15:09 +02:00
checkIfAlreadyMounted = $( lsblk -o NAME,UUID,MOUNTPOINT | grep " ${ hdd } " | grep -c '/mnt/' )
2020-07-04 13:53:20 +02:00
if [ ${ checkIfAlreadyMounted } -gt 0 ] ; then
echo "# cannot format a device that is mounted"
2020-07-04 13:54:05 +02:00
echo "error='device is in use'"
2020-07-04 13:53:20 +02:00
exit 1
fi
2020-07-04 13:43:21 +02:00
echo " # OK found device name ${ hdd } that will now be formatted ... "
echo "# Wiping all partitions (sfdisk/wipefs)"
sudo sfdisk --delete /dev/${ hdd } 1>& 2
2020-09-23 17:15:09 +02:00
sleep 4
2020-07-04 13:43:21 +02:00
sudo wipefs -a /dev/${ hdd } 1>& 2
2020-09-23 17:15:09 +02:00
sleep 4
2020-07-04 13:43:21 +02:00
partitions = $( lsblk | grep -c " ─ ${ hdd } " )
if [ ${ partitions } -gt 0 ] ; then
echo "# WARNING: partitions are still not clean"
2020-07-04 13:47:43 +02:00
echo "error='partitioning failed'"
2020-07-04 13:43:21 +02:00
exit 1
fi
# using FAT32 here so that the backup can be easily opened on Windows and Mac
echo "# Create on big partition"
2020-07-04 16:10:04 +02:00
sudo parted /dev/${ hdd } mklabel msdos 1>& 2
2020-07-04 13:43:21 +02:00
sudo parted /dev/${ hdd } mkpart primary fat32 0% 100% 1>& 2
echo "# Formatting FAT32"
sudo mkfs.vfat -F 32 -n 'BLITZBACKUP' /dev/${ hdd } 1 1>& 2
echo "# Getting new UUID"
uuid = $( lsblk -o UUID,NAME | grep " ${ hdd } 1 " | cut -d " " -f 1)
if [ " ${ uuid } " = = "" ] ; then
2020-07-04 13:47:43 +02:00
echo "error='formatting failed'"
2020-07-04 13:43:21 +02:00
exit 1
fi
echo " # OK device formatted --> UUID is ${ uuid } "
else
echo "error='device not found'"
exit 1
fi
2020-07-04 13:00:35 +02:00
fi
# change raspiblitz.conf
entryExists = $( cat /mnt/hdd/raspiblitz.conf | grep -c 'localBackupDeviceUUID=' )
2020-07-04 13:45:46 +02:00
if [ ${ entryExists } -eq 0 ] ; then
2020-07-04 13:00:35 +02:00
echo "localBackupDeviceUUID='off'" >> /mnt/hdd/raspiblitz.conf
fi
sudo sed -i " s/^localBackupDeviceUUID=.*/localBackupDeviceUUID=' ${ uuid } '/g " /mnt/hdd/raspiblitz.conf
2020-07-04 13:46:47 +02:00
echo "activated=1"
2020-07-04 13:00:35 +02:00
# mount device (so that no reboot is needed)
source <( sudo /home/admin/config.scripts/blitz.backupdevice.sh mount)
echo " isMounted= ${ isMounted } "
if [ ${ isMounted } -eq 0 ] ; then
echo "error='failed to mount'"
fi
2020-07-04 13:43:21 +02:00
2020-07-04 16:10:04 +02:00
# copy SCB over
cp /mnt/hdd/lnd/data/chain/${ network } /${ chain } net/channel.backup /mnt/backup/channel.backup 1>& 2
2020-07-04 15:28:32 +02:00
if [ ${ userinteraction } -eq 1 ] ; then
if [ ${ isMounted } -eq 0 ] ; then
sudo sed -i "s/^localBackupDeviceUUID=.*/localBackupDeviceUUID=off/g" /mnt/hdd/raspiblitz.conf
dialog --title ' Adding Backup Device ' --msgbox '\nFAIL - Not able to add device.' 7 40
else
dialog --title ' Adding Backup Device ' --msgbox '\nOK - Device added for Backup.' 7 40
fi
fi
2020-07-04 13:00:35 +02:00
exit 0
fi
#########################
# MOUNT
#########################
if [ " $1 " = "mount" ] ; then
echo "# BACKUP DEVICE MOUNT"
# check if feature is on
if [ " ${ localBackupDeviceUUID } " = = "" ] || [ " ${ localBackupDeviceUUID } " = = "off" ] ; then
echo "error='feature is off'"
exit 1
fi
checkIfAlreadyMounted = $( df | grep -c "/mnt/backup" )
if [ ${ checkIfAlreadyMounted } -gt 0 ] ; then
echo "# there is something already mounted on /mnt/backup"
echo "error='already mounted'"
exit 1
fi
sudo mkdir -p /mnt/backup 1>& 2
sudo mount --uuid ${ localBackupDeviceUUID } /mnt/backup 1>& 2
mountWorked = $( df | grep -c "/mnt/backup" )
if [ ${ mountWorked } -gt 0 ] ; then
echo "# OK BackupDrive mounted to: /mnt/backup"
echo "isMounted=1"
else
echo "# FAIL BackupDrive mount - check if device is connected & UUID is correct"
echo "isMounted=0"
echo "error='mount failed'"
fi
2020-07-04 04:42:34 +02:00
exit 0
2020-07-04 04:32:57 +02:00
fi
#########################
# TURN OFF
#########################
if [ " $1 " = "off" ] ; then
echo "# BACKUP DEVICE REMOVE"
2020-07-04 14:03:41 +02:00
sudo sed -i "s/^localBackupDeviceUUID=.*/localBackupDeviceUUID=off/g" /mnt/hdd/raspiblitz.conf
sudo umount /mnt/backup 2>/dev/null
echo "# OK backup device is off"
2020-07-04 04:42:34 +02:00
exit 0
2020-07-04 04:32:57 +02:00
fi
2021-08-27 03:59:21 -04:00
echo "error='unknown command'"
2020-07-04 04:32:57 +02:00
exit 1