it works at least #1

Manually merged
lulzette merged 10 commits from old into master 2021-01-24 20:09:32 +03:00
2 changed files with 32 additions and 34 deletions

View File

@ -1,8 +1,4 @@
# This is... # Что это?
Script that can install OS from previously rsynced backup on NAS Это скрипт, который восстанавливает систему из бекапа, расположенного на rsync сервере, оптимизирован под ArchLinux
# TODO
* some checks
* config file support

View File

@ -1,8 +1,8 @@
#not working encrypted setup #not working encrypted setup
#vars #vars
IP="192.168.100.16" IP="192.168.100.100"
source="rsync@"$IP"/rsync/" source="rsync@"$IP"/mnt/"
bootSize=256M bootSize=256M
#prepare #prepare
@ -26,13 +26,15 @@ rsync_dir_list=($(rsync --password-file=rsync_pass rsync://$source | awk '{print
##choose backup ##choose backup
echo "==>" ${#rsync_dir_list[@]} backups: ${rsync_dir_list[@]} echo "==>" ${#rsync_dir_list[@]} backups: ${rsync_dir_list[@]}
read -p "Input backup number:" backupNum read -p "Input backup name:" backupName
until [ $backupNum -le ${#rsync_dir_list[@]} ] echo "Selected: " $backupName
do
read -p "Input backup number:" backupNum #until [ $backupNum -le ${#rsync_dir_list[@]} ]
done #do
let "backupNum -= 1" #read -p "Input backup name:" backupName
#done
#let "backupNum -= 1"
##set target drive ##set target drive
@ -42,8 +44,9 @@ do
read -p "Target drive is " targetDrive read -p "Target drive is " targetDrive
targetDrive=/dev/$targetDrive targetDrive=/dev/$targetDrive
done done
echo "Encrypted?" #encrypted?
while read -r -n 1 -s answer ; do echo "Encrypted? (Y/N)"
while read -r -n 1 answer ; do
if [[ $answer = [YyNn] ]]; then if [[ $answer = [YyNn] ]]; then
[[ $answer = [Yy] ]] && encryptedDevice=true && echo "WARNING You will be asked for password later" #read -p "Password: " encryptedPassword [[ $answer = [Yy] ]] && encryptedDevice=true && echo "WARNING You will be asked for password later" #read -p "Password: " encryptedPassword
[[ $answer = [Nn] ]] && encryptedDevice=false [[ $answer = [Nn] ]] && encryptedDevice=false
@ -53,8 +56,9 @@ done
#get swap size #get swap size
swapSize=$(free -m | grep "Mem" | awk '{print $2}') swapSize=$(free -m | grep "Mem" | awk '{print $2}')
echo ""
read -p "Enter extra amount of swap (empty == 512):" swapExtra read -p "Enter extra amount of swap (empty == 512):" swapExtra
if [ -z "$swapExtra" ] && swapExtra=512 [[ -z "$swapExtra" ]] && swapExtra=512
let "swapSize += swapExtra" #add 512 mbs to swap for swap, not only hibernation let "swapSize += swapExtra" #add 512 mbs to swap for swap, not only hibernation
swapSize=$swapSize"M" swapSize=$swapSize"M"
@ -98,19 +102,18 @@ echo w
##mkfs's ##mkfs's
echo "==> Making filesystems..." echo "==> Making filesystems..."
echo "mkswap" echo "=> swap"
mkswap $targetDrive"1" mkswap $targetDrive"1"
echo "mkfs.vfat" echo "=> /boot"
mkfs.vfat $targetDrive"2" mkfs.vfat $targetDrive"2"
echo "mkfs.ext4" echo "=> rootfs"
if $encryptedDevice if $encryptedDevice ; then
then
cryptsetup luksFormat -v $targetDrive"3" cryptsetup luksFormat -v $targetDrive"3"
cryptsetup open $targetDrive"3" targetLuks cryptsetup open $targetDrive"3" targetLuks
mkfs.ext4 /dev/mapper/targetLuks mkfs.ext4 -q /dev/mapper/targetLuks
else else
mkfs.ext4 $targetDrive"3" mkfs.ext4 -q $targetDrive"3"
fi fi
sleep 15 sleep 15
@ -118,16 +121,14 @@ sleep 15
##get disk's uuid ##get disk's uuid
echo "==> Got UUIDs!" echo "==> Got UUIDs!"
UUIDS=($(blkid $targetDrive"1" $targetDrive"2" $targetDrive"3" -o value -s UUID)) UUIDS=($(blkid $targetDrive"1" $targetDrive"2" $targetDrive"3" -o value -s UUID))
if $encryptedDevice if $encryptedDevice ; then
then
UUIDS[3]=UUIDS[2] UUIDS[3]=UUIDS[2]
UUIDS[2]=`blkid /dev/mapper/targetLuks -o value -s UUID` UUIDS[2]=`blkid /dev/mapper/targetLuks -o value -s UUID`
fi fi
##mount disks ##mount disks
echo "==> Mounting..." echo "==> Mounting..."
if $encryptedDevice if $encryptedDevice ; then
then
mount /dev/mapper/targetLuks /mnt mount /dev/mapper/targetLuks /mnt
else else
mount $targetDrive"3" /mnt mount $targetDrive"3" /mnt
@ -138,7 +139,8 @@ mount $targetDrive"2" /mnt/boot
##rsync ##rsync
echo "==> Copying from NAS..." echo "==> Copying from NAS..."
rsync --archive --password-file=rsync_pass rsync://$source${rsync_dir_list[$backupNum]}/ /mnt/ #rsync --archive --password-file=rsync_pass rsync://$source${rsync_dir_list[$backupNum]}/ /mnt/
rsync --archive --password-file=rsync_pass rsync://$source$backupName/ /mnt/
retVal=$? retVal=$?
if [ $retVal -ne 0 ]; then if [ $retVal -ne 0 ]; then
exit 1 exit 1
@ -155,10 +157,10 @@ rm /mnt/etc/fstab.orig #TODO OFC we need to check mountpoint, not only type of F
echo "==> Result:" echo "==> Result:"
cat /mnt/etc/fstab cat /mnt/etc/fstab
##update grub linux options ##update grub linux options
if $encryptedDevice if $encryptedDevice ; then
cat /mnt/etc/default/grub | sed 's/cryptdevice=UUID=[A-Fa-f0-9-]*:cryptroot /cryptdevice=UUID='${UUIDS[3]}':cryptroot/' > /mnt/etc/default/grub cat /mnt/etc/default/grub | sed 's/cryptdevice=UUID=[A-Fa-f0-9-]*:cryptroot /cryptdevice=UUID='${UUIDS[3]}':cryptroot/' > /mnt/etc/default/grub
else else
cat /mnt/etc/default/grub | sed 's/cryptdevice=UUID=[A-Fa-f0-9-]*:cryptroot //' > /mnt/etc/default/grub cat /mnt/etc/default/grub | sed 's/cryptdevice=UUID=[A-Fa-f0-9-]*:cryptroot //' > /mnt/etc/default/grub
fi fi
cat /mnt/etc/default/grub | sed 's/resume=UUID=[A-Fa-f0-9-]*/resume=UUID='${UUIDS[0]}'/' > /mnt/etc/default/grub cat /mnt/etc/default/grub | sed 's/resume=UUID=[A-Fa-f0-9-]*/resume=UUID='${UUIDS[0]}'/' > /mnt/etc/default/grub
@ -175,7 +177,7 @@ echo "==> mkinitcpio and update grub..."
#cat /mnt/etc/mkinitcpio.conf | sed 's/encrypt //' > /mnt/etc/mkinitcpio.conf #remove cryptdevice, no encrypted fs today :c #cat /mnt/etc/mkinitcpio.conf | sed 's/encrypt //' > /mnt/etc/mkinitcpio.conf #remove cryptdevice, no encrypted fs today :c
#chroot env #chroot env
cat << EOF | chroot /mnt cat << EOF | chroot /mnt /bin/sh
mkinitcpio -P mkinitcpio -P
grub-mkconfig -o /boot/grub/grub.cfg grub-mkconfig -o /boot/grub/grub.cfg
grub-install $targetDrive grub-install $targetDrive