functions!
This commit is contained in:
parent
a64f373006
commit
87c4b0073b
62
restore.sh
62
restore.sh
@ -1,12 +1,13 @@
|
||||
#!/bin/bash
|
||||
#not working encrypted setup
|
||||
|
||||
#vars
|
||||
IP="192.168.100.16"
|
||||
source="rsync@"$IP"/rsync/"
|
||||
IP="192.168.100.100"
|
||||
source="rsync@"$IP"/mnt/"
|
||||
bootSize=256M
|
||||
|
||||
#prepare
|
||||
##check for root
|
||||
testVars() {
|
||||
if [ `id -u` != 0 ] ; then
|
||||
echo "==> Need to be root"
|
||||
exit 1
|
||||
@ -18,13 +19,13 @@ then
|
||||
echo "==> Host unavailable, exiting"
|
||||
exit 1
|
||||
fi
|
||||
}
|
||||
|
||||
##check for space
|
||||
|
||||
chooseBackup() {
|
||||
##get list of backups
|
||||
rsync_dir_list=($(rsync --password-file=rsync_pass rsync://$source | awk '{print $5}' | grep -v .DS_Store | grep -v "@Recently-Snapshot" | sort -r | head -n -1))
|
||||
|
||||
##choose backup
|
||||
|
||||
echo "==>" ${#rsync_dir_list[@]} backups: ${rsync_dir_list[@]}
|
||||
read -p "Input backup number:" backupNum
|
||||
|
||||
@ -33,7 +34,9 @@ do
|
||||
read -p "Input backup number:" backupNum
|
||||
done
|
||||
let "backupNum -= 1"
|
||||
}
|
||||
|
||||
setTargetDisk() {
|
||||
|
||||
##set target drive
|
||||
lsblk -o NAME,LABEL,FSTYPE,SIZE,MOUNTPOINT,MODEL
|
||||
@ -57,18 +60,17 @@ read -p "Enter extra amount of swap (empty == 512):" swapExtra
|
||||
if [ -z "$swapExtra" ] && swapExtra=512
|
||||
let "swapSize += swapExtra" #add 512 mbs to swap for swap, not only hibernation
|
||||
swapSize=$swapSize"M"
|
||||
|
||||
}
|
||||
#DEPLOY
|
||||
|
||||
warnDeploy() {
|
||||
echo deployin\' ${rsync_dir_list[$backupNum]} on $targetDrive in 15s...
|
||||
|
||||
sleep 15
|
||||
|
||||
|
||||
trap 'echo "==> Interrupted by user"; exit 1' 2
|
||||
}
|
||||
|
||||
partitionDisk() {
|
||||
##clear mbr for sure
|
||||
|
||||
dd if=/dev/zero of=$targetDrive count=512
|
||||
|
||||
##fdisk
|
||||
@ -95,7 +97,9 @@ echo
|
||||
echo w
|
||||
) | fdisk $targetDrive
|
||||
# IMHO this ^ is less obfuscated way
|
||||
}
|
||||
|
||||
makeFS() {
|
||||
##mkfs's
|
||||
echo "==> Making filesystems..."
|
||||
echo "mkswap"
|
||||
@ -112,9 +116,9 @@ mkfs.ext4 /dev/mapper/targetLuks
|
||||
else
|
||||
mkfs.ext4 $targetDrive"3"
|
||||
fi
|
||||
}
|
||||
|
||||
sleep 15
|
||||
|
||||
getUUIDs() {
|
||||
##get disk's uuid
|
||||
echo "==> Got UUIDs!"
|
||||
UUIDS=($(blkid $targetDrive"1" $targetDrive"2" $targetDrive"3" -o value -s UUID))
|
||||
@ -123,7 +127,9 @@ then
|
||||
UUIDS[3]=UUIDS[2]
|
||||
UUIDS[2]=`blkid /dev/mapper/targetLuks -o value -s UUID`
|
||||
fi
|
||||
}
|
||||
|
||||
mountDisk() {
|
||||
##mount disks
|
||||
echo "==> Mounting..."
|
||||
if $encryptedDevice
|
||||
@ -135,7 +141,9 @@ fi
|
||||
|
||||
mkdir /mnt/boot
|
||||
mount $targetDrive"2" /mnt/boot
|
||||
}
|
||||
|
||||
copyFiles() {
|
||||
##rsync
|
||||
echo "==> Copying from NAS..."
|
||||
rsync --archive --password-file=rsync_pass rsync://$source${rsync_dir_list[$backupNum]}/ /mnt/
|
||||
@ -143,7 +151,9 @@ retVal=$?
|
||||
if [ $retVal -ne 0 ]; then
|
||||
exit 1
|
||||
fi
|
||||
}
|
||||
|
||||
updateFstab() {
|
||||
##update fstab
|
||||
echo "==> Updating fstab..."
|
||||
mv /mnt/etc/fstab /mnt/etc/fstab.orig
|
||||
@ -154,6 +164,10 @@ rm /mnt/etc/fstab.orig #TODO OFC we need to check mountpoint, not only type of F
|
||||
|
||||
echo "==> Result:"
|
||||
cat /mnt/etc/fstab
|
||||
}
|
||||
|
||||
configBootloader()
|
||||
{
|
||||
##update grub linux options
|
||||
if $encryptedDevice
|
||||
cat /mnt/etc/default/grub | sed 's/cryptdevice=UUID=[A-Fa-f0-9-]*:cryptroot /cryptdevice=UUID='${UUIDS[3]}':cryptroot/' > /mnt/etc/default/grub
|
||||
@ -162,7 +176,10 @@ cat /mnt/etc/default/grub | sed 's/cryptdevice=UUID=[A-Fa-f0-9-]*:cryptroot //'
|
||||
fi
|
||||
|
||||
cat /mnt/etc/default/grub | sed 's/resume=UUID=[A-Fa-f0-9-]*/resume=UUID='${UUIDS[0]}'/' > /mnt/etc/default/grub
|
||||
}
|
||||
|
||||
updateBootloader()
|
||||
{
|
||||
##mount system stuff
|
||||
echo "==> Mounting /dev, /sys, /proc..."
|
||||
mount /dev /mnt/dev --bind
|
||||
@ -182,9 +199,28 @@ grub-install $targetDrive
|
||||
exit
|
||||
EOF
|
||||
|
||||
}
|
||||
|
||||
deinit()
|
||||
{
|
||||
#deinit
|
||||
echo "==> Syncing..."
|
||||
sync
|
||||
echo "==> Unmounting..."
|
||||
umount -R /mnt
|
||||
echo "==> Done!"
|
||||
}
|
||||
# funcions
|
||||
testVars()
|
||||
chooseBackup()
|
||||
setTargetDisk()
|
||||
warnDeploy()
|
||||
partitionDisk()
|
||||
makeFS()
|
||||
getUUIDs()
|
||||
mountDisk()
|
||||
copyFiles()
|
||||
updateFstab()
|
||||
configBootloader()
|
||||
updateBootloader()
|
||||
deinit()
|
Loading…
Reference in New Issue
Block a user