rsync_restore/restore.sh

193 lines
4.6 KiB
Bash
Raw Normal View History

2018-09-11 23:17:25 +03:00
#not working encrypted setup
#vars
2021-01-24 11:49:44 +03:00
IP="192.168.100.100"
source="rsync@"$IP"/mnt/"
2018-09-11 23:17:25 +03:00
bootSize=256M
#prepare
##check for root
if [ `id -u` != 0 ] ; then
echo "==> Need to be root"
exit 1
fi
##check for network
ping -c 1 $IP &> /dev/null
if [ "$?" != 0 ]
then
echo "==> Host unavailable, exiting"
exit 1
fi
##check for space
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[@]}
2021-01-24 13:45:30 +03:00
read -p "Input backup name:" backupName
2018-09-11 23:17:25 +03:00
2021-01-24 13:45:30 +03:00
echo "Selected: " $backupName
#until [ $backupNum -le ${#rsync_dir_list[@]} ]
#do
#read -p "Input backup name:" backupName
#done
#let "backupNum -= 1"
2018-09-11 23:17:25 +03:00
##set target drive
lsblk -o NAME,LABEL,FSTYPE,SIZE,MOUNTPOINT,MODEL
until [ ! -f $targetDrive ]
do
read -p "Target drive is " targetDrive
targetDrive=/dev/$targetDrive
done
2021-01-24 18:52:30 +03:00
#encrypted?
2021-01-24 19:19:08 +03:00
echo "Encrypted? (Y/N)"
while read -r -n 1 answer ; do
2018-09-11 23:17:25 +03:00
if [[ $answer = [YyNn] ]]; then
[[ $answer = [Yy] ]] && encryptedDevice=true && echo "WARNING You will be asked for password later" #read -p "Password: " encryptedPassword
[[ $answer = [Nn] ]] && encryptedDevice=false
break
fi
done
#get swap size
swapSize=$(free -m | grep "Mem" | awk '{print $2}')
2021-01-24 13:47:29 +03:00
echo ""
2018-09-11 23:17:25 +03:00
read -p "Enter extra amount of swap (empty == 512):" swapExtra
2021-01-14 12:53:10 +03:00
[[ -z "$swapExtra" ]] && swapExtra=512
let "swapSize += swapExtra" #add 512 mbs to swap for swap, not only hibernation
2018-09-11 23:17:25 +03:00
swapSize=$swapSize"M"
#DEPLOY
echo deployin\' ${rsync_dir_list[$backupNum]} on $targetDrive in 15s...
sleep 15
trap 'echo "==> Interrupted by user"; exit 1' 2
##clear mbr for sure
dd if=/dev/zero of=$targetDrive count=512
##fdisk
echo "==> Partitioning..."
(
echo o # Create a new empty DOS partition table
echo n # Add a new partition
echo # Primary partition
echo # Partition number
echo # First sector (Accept default: 1)
echo +$swapSize # Last sector (Accept default: varies)
echo y #remove sign
echo n
echo
echo
echo
echo +$bootSize
echo y
echo n
echo
echo
echo
echo
echo w
) | fdisk $targetDrive
# IMHO this ^ is less obfuscated way
##mkfs's
echo "==> Making filesystems..."
2021-01-24 14:03:29 +03:00
echo "=> swap"
2018-09-11 23:17:25 +03:00
mkswap $targetDrive"1"
2021-01-24 14:03:29 +03:00
echo "=> /boot"
2018-09-11 23:17:25 +03:00
mkfs.vfat $targetDrive"2"
2021-01-24 14:03:29 +03:00
echo "=> rootfs"
2021-01-24 17:56:08 +03:00
if $encryptedDevice ; then
2018-09-11 23:17:25 +03:00
cryptsetup luksFormat -v $targetDrive"3"
cryptsetup open $targetDrive"3" targetLuks
2021-01-24 14:03:29 +03:00
mkfs.ext4 -q /dev/mapper/targetLuks
2018-09-11 23:17:25 +03:00
else
2021-01-24 14:03:29 +03:00
mkfs.ext4 -q $targetDrive"3"
2018-09-11 23:17:25 +03:00
fi
sleep 15
##get disk's uuid
echo "==> Got UUIDs!"
UUIDS=($(blkid $targetDrive"1" $targetDrive"2" $targetDrive"3" -o value -s UUID))
2021-01-24 17:56:08 +03:00
if $encryptedDevice ; then
2018-09-11 23:17:25 +03:00
UUIDS[3]=UUIDS[2]
UUIDS[2]=`blkid /dev/mapper/targetLuks -o value -s UUID`
fi
##mount disks
echo "==> Mounting..."
2021-01-24 17:56:08 +03:00
if $encryptedDevice ; then
2018-09-11 23:17:25 +03:00
mount /dev/mapper/targetLuks /mnt
else
mount $targetDrive"3" /mnt
fi
mkdir /mnt/boot
mount $targetDrive"2" /mnt/boot
##rsync
echo "==> Copying from NAS..."
2021-01-24 13:45:30 +03:00
#rsync --archive --password-file=rsync_pass rsync://$source${rsync_dir_list[$backupNum]}/ /mnt/
rsync --archive --password-file=rsync_pass rsync://$source$backupName/ /mnt/
2018-09-11 23:17:25 +03:00
retVal=$?
if [ $retVal -ne 0 ]; then
exit 1
fi
##update fstab
echo "==> Updating fstab..."
mv /mnt/etc/fstab /mnt/etc/fstab.orig
cat /mnt/etc/fstab.orig | grep "ext4" | sed 's/UUID=[A-Fa-f0-9-]*/UUID='${UUIDS[2]}'/' > /mnt/etc/fstab
cat /mnt/etc/fstab.orig | grep "vfat" | sed 's/UUID=[A-Fa-f0-9-]*/UUID='${UUIDS[1]}'/' >> /mnt/etc/fstab
cat /mnt/etc/fstab.orig | grep "swap" | sed 's/UUID=[A-Fa-f0-9-]*/UUID='${UUIDS[0]}'/' >> /mnt/etc/fstab
rm /mnt/etc/fstab.orig #TODO OFC we need to check mountpoint, not only type of FS
2018-09-11 23:17:25 +03:00
echo "==> Result:"
cat /mnt/etc/fstab
##update grub linux options
2021-01-24 17:56:08 +03:00
if $encryptedDevice ; then
2021-01-24 14:03:29 +03:00
cat /mnt/etc/default/grub | sed 's/cryptdevice=UUID=[A-Fa-f0-9-]*:cryptroot /cryptdevice=UUID='${UUIDS[3]}':cryptroot/' > /mnt/etc/default/grub
2018-09-11 23:17:25 +03:00
else
2021-01-24 14:03:29 +03:00
cat /mnt/etc/default/grub | sed 's/cryptdevice=UUID=[A-Fa-f0-9-]*:cryptroot //' > /mnt/etc/default/grub
2018-09-11 23:17:25 +03:00
fi
cat /mnt/etc/default/grub | sed 's/resume=UUID=[A-Fa-f0-9-]*/resume=UUID='${UUIDS[0]}'/' > /mnt/etc/default/grub
##mount system stuff
echo "==> Mounting /dev, /sys, /proc..."
mount /dev /mnt/dev --bind
mount /sys /mnt/sys --bind
mount /proc /mnt/proc --bind
##chroot, mkinitcpio and update grub
echo "==> mkinitcpio and update grub..."
#cat /mnt/etc/mkinitcpio.conf | sed 's/encrypt //' > /mnt/etc/mkinitcpio.conf #remove cryptdevice, no encrypted fs today :c
#chroot env
2021-01-24 18:52:30 +03:00
cat << EOF | chroot /mnt /bin/sh
2018-09-11 23:17:25 +03:00
mkinitcpio -P
grub-mkconfig -o /boot/grub/grub.cfg
grub-install $targetDrive
exit
EOF
#deinit
echo "==> Syncing..."
sync
echo "==> Unmounting..."
umount -R /mnt
2021-01-24 11:49:44 +03:00
echo "==> Done!"