Compare commits

..

10 Commits

Author SHA1 Message Date
3111605ce1 fix question 2021-01-24 19:19:08 +03:00
4a08cb45d4 fix chroot+fix question encrypted 2021-01-24 18:52:30 +03:00
31649a6948 i'm too stupid for bash... 2021-01-24 17:56:08 +03:00
55514aca5b fix error, fix msgs 2021-01-24 14:03:29 +03:00
b571133f3f fix swap selector 2021-01-24 13:47:29 +03:00
70b0087b89 backup selector changed 2021-01-24 13:45:30 +03:00
3d8327112d fix "encrypted" question 2021-01-24 13:41:30 +03:00
fb856ac8d5 changed location 2021-01-24 11:49:44 +03:00
9d369e8b0d readme 2021-01-14 12:55:00 +03:00
a248b3721b fix syntax error at "get swap size" 2021-01-14 12:53:10 +03:00
2 changed files with 158 additions and 196 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,4 +1,3 @@
#!/bin/bash
#not working encrypted setup #not working encrypted setup
#vars #vars
@ -6,8 +5,8 @@ IP="192.168.100.100"
source="rsync@"$IP"/mnt/" source="rsync@"$IP"/mnt/"
bootSize=256M bootSize=256M
#prepare
##check for root ##check for root
testVars() {
if [ `id -u` != 0 ] ; then if [ `id -u` != 0 ] ; then
echo "==> Need to be root" echo "==> Need to be root"
exit 1 exit 1
@ -19,24 +18,24 @@ testVars() {
echo "==> Host unavailable, exiting" echo "==> Host unavailable, exiting"
exit 1 exit 1
fi fi
}
chooseBackup() { ##check for space
##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)) 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 ##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"
setTargetDisk() {
##set target drive ##set target drive
lsblk -o NAME,LABEL,FSTYPE,SIZE,MOUNTPOINT,MODEL lsblk -o NAME,LABEL,FSTYPE,SIZE,MOUNTPOINT,MODEL
@ -45,8 +44,9 @@ setTargetDisk() {
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
@ -56,21 +56,23 @@ setTargetDisk() {
#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"
}
#DEPLOY #DEPLOY
warnDeploy() {
echo deployin\' ${rsync_dir_list[$backupNum]} on $targetDrive in 15s... echo deployin\' ${rsync_dir_list[$backupNum]} on $targetDrive in 15s...
sleep 15
trap 'echo "==> Interrupted by user"; exit 1' 2
}
partitionDisk() { sleep 15
trap 'echo "==> Interrupted by user"; exit 1' 2
##clear mbr for sure ##clear mbr for sure
dd if=/dev/zero of=$targetDrive count=512 dd if=/dev/zero of=$targetDrive count=512
##fdisk ##fdisk
@ -97,43 +99,36 @@ partitionDisk() {
echo w echo w
) | fdisk $targetDrive ) | fdisk $targetDrive
# IMHO this ^ is less obfuscated way # IMHO this ^ is less obfuscated way
}
makeFS() {
##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
}
getUUIDs() { 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
}
mountDisk() {
##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
@ -141,19 +136,16 @@ mountDisk() {
mkdir /mnt/boot mkdir /mnt/boot
mount $targetDrive"2" /mnt/boot mount $targetDrive"2" /mnt/boot
}
copyFiles() {
##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
fi fi
}
updateFstab() {
##update fstab ##update fstab
echo "==> Updating fstab..." echo "==> Updating fstab..."
mv /mnt/etc/fstab /mnt/etc/fstab.orig mv /mnt/etc/fstab /mnt/etc/fstab.orig
@ -164,22 +156,15 @@ updateFstab() {
echo "==> Result:" echo "==> Result:"
cat /mnt/etc/fstab cat /mnt/etc/fstab
}
configBootloader()
{
##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
}
updateBootloader()
{
##mount system stuff ##mount system stuff
echo "==> Mounting /dev, /sys, /proc..." echo "==> Mounting /dev, /sys, /proc..."
mount /dev /mnt/dev --bind mount /dev /mnt/dev --bind
@ -192,35 +177,16 @@ updateBootloader()
#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
exit exit
EOF EOF
}
deinit()
{
#deinit #deinit
echo "==> Syncing..." echo "==> Syncing..."
sync sync
echo "==> Unmounting..." echo "==> Unmounting..."
umount -R /mnt umount -R /mnt
echo "==> Done!" echo "==> Done!"
}
# funcions
testVars()
chooseBackup()
setTargetDisk()
warnDeploy()
partitionDisk()
makeFS()
getUUIDs()
mountDisk()
copyFiles()
updateFstab()
configBootloader()
updateBootloader()
deinit()