Compare commits

...

1 Commits

Author SHA1 Message Date
87c4b0073b functions! 2021-01-14 12:11:43 +03:00

View File

@ -1,180 +1,197 @@
#!/bin/bash
#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
##check for root ##check for root
if [ `id -u` != 0 ] ; then testVars() {
if [ `id -u` != 0 ] ; then
echo "==> Need to be root" echo "==> Need to be root"
exit 1 exit 1
fi fi
##check for network ##check for network
ping -c 1 $IP &> /dev/null ping -c 1 $IP &> /dev/null
if [ "$?" != 0 ] if [ "$?" != 0 ]
then then
echo "==> Host unavailable, exiting" echo "==> Host unavailable, exiting"
exit 1 exit 1
fi 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))
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
##choose backup until [ $backupNum -le ${#rsync_dir_list[@]} ]
do
read -p "Input backup number:" backupNum
done
let "backupNum -= 1"
}
echo "==>" ${#rsync_dir_list[@]} backups: ${rsync_dir_list[@]} setTargetDisk() {
read -p "Input backup number:" backupNum
until [ $backupNum -le ${#rsync_dir_list[@]} ] ##set target drive
do lsblk -o NAME,LABEL,FSTYPE,SIZE,MOUNTPOINT,MODEL
read -p "Input backup number:" backupNum until [ ! -f $targetDrive ]
done do
let "backupNum -= 1" read -p "Target drive is " targetDrive
targetDrive=/dev/$targetDrive
done
##set target drive echo "Encrypted?"
lsblk -o NAME,LABEL,FSTYPE,SIZE,MOUNTPOINT,MODEL while read -r -n 1 -s answer ; do
until [ ! -f $targetDrive ]
do
read -p "Target drive is " targetDrive
targetDrive=/dev/$targetDrive
done
echo "Encrypted?"
while read -r -n 1 -s 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
break break
fi fi
done done
#get swap size
swapSize=$(free -m | grep "Mem" | awk '{print $2}')
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"
#get swap size
swapSize=$(free -m | grep "Mem" | awk '{print $2}')
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 #DEPLOY
echo deployin\' ${rsync_dir_list[$backupNum]} on $targetDrive in 15s... warnDeploy() {
echo deployin\' ${rsync_dir_list[$backupNum]} on $targetDrive in 15s...
sleep 15
trap 'echo "==> Interrupted by user"; exit 1' 2
}
sleep 15 partitionDisk() {
##clear mbr for sure
dd if=/dev/zero of=$targetDrive count=512
##fdisk
trap 'echo "==> Interrupted by user"; exit 1' 2 echo "==> Partitioning..."
(
##clear mbr for sure echo o # Create a new empty DOS partition table
echo n # Add a new partition
dd if=/dev/zero of=$targetDrive count=512 echo # Primary partition
echo # Partition number
##fdisk echo # First sector (Accept default: 1)
echo "==> Partitioning..." echo +$swapSize # Last sector (Accept default: varies)
( echo y #remove sign
echo o # Create a new empty DOS partition table echo n
echo n # Add a new partition echo
echo # Primary partition echo
echo # Partition number echo
echo # First sector (Accept default: 1) echo +$bootSize
echo +$swapSize # Last sector (Accept default: varies) echo y
echo y #remove sign echo n
echo n echo
echo echo
echo echo
echo echo
echo +$bootSize echo w
echo y ) | fdisk $targetDrive
echo n
echo
echo
echo
echo
echo w
) | fdisk $targetDrive
# IMHO this ^ is less obfuscated way # IMHO this ^ is less obfuscated way
}
##mkfs's makeFS() {
echo "==> Making filesystems..." ##mkfs's
echo "mkswap" echo "==> Making filesystems..."
mkswap $targetDrive"1" echo "mkswap"
echo "mkfs.vfat" mkswap $targetDrive"1"
mkfs.vfat $targetDrive"2" echo "mkfs.vfat"
mkfs.vfat $targetDrive"2"
echo "mkfs.ext4" echo "mkfs.ext4"
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 /dev/mapper/targetLuks
else else
mkfs.ext4 $targetDrive"3" mkfs.ext4 $targetDrive"3"
fi fi
}
sleep 15 getUUIDs() {
##get disk's uuid
echo "==> Got UUIDs!"
UUIDS=($(blkid $targetDrive"1" $targetDrive"2" $targetDrive"3" -o value -s UUID))
if $encryptedDevice
then
UUIDS[3]=UUIDS[2]
UUIDS[2]=`blkid /dev/mapper/targetLuks -o value -s UUID`
fi
}
##get disk's uuid mountDisk() {
echo "==> Got UUIDs!" ##mount disks
UUIDS=($(blkid $targetDrive"1" $targetDrive"2" $targetDrive"3" -o value -s UUID)) echo "==> Mounting..."
if $encryptedDevice if $encryptedDevice
then then
UUIDS[3]=UUIDS[2] mount /dev/mapper/targetLuks /mnt
UUIDS[2]=`blkid /dev/mapper/targetLuks -o value -s UUID` else
fi mount $targetDrive"3" /mnt
fi
##mount disks mkdir /mnt/boot
echo "==> Mounting..." mount $targetDrive"2" /mnt/boot
if $encryptedDevice }
then
mount /dev/mapper/targetLuks /mnt
else
mount $targetDrive"3" /mnt
fi
mkdir /mnt/boot copyFiles() {
mount $targetDrive"2" /mnt/boot ##rsync
echo "==> Copying from NAS..."
##rsync rsync --archive --password-file=rsync_pass rsync://$source${rsync_dir_list[$backupNum]}/ /mnt/
echo "==> Copying from NAS..." retVal=$?
rsync --archive --password-file=rsync_pass rsync://$source${rsync_dir_list[$backupNum]}/ /mnt/ if [ $retVal -ne 0 ]; then
retVal=$?
if [ $retVal -ne 0 ]; then
exit 1 exit 1
fi fi
}
##update fstab updateFstab() {
echo "==> Updating fstab..." ##update fstab
mv /mnt/etc/fstab /mnt/etc/fstab.orig echo "==> Updating fstab..."
cat /mnt/etc/fstab.orig | grep "ext4" | sed 's/UUID=[A-Fa-f0-9-]*/UUID='${UUIDS[2]}'/' > /mnt/etc/fstab mv /mnt/etc/fstab /mnt/etc/fstab.orig
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 "ext4" | sed 's/UUID=[A-Fa-f0-9-]*/UUID='${UUIDS[2]}'/' > /mnt/etc/fstab
cat /mnt/etc/fstab.orig | grep "swap" | sed 's/UUID=[A-Fa-f0-9-]*/UUID='${UUIDS[0]}'/' >> /mnt/etc/fstab cat /mnt/etc/fstab.orig | grep "vfat" | sed 's/UUID=[A-Fa-f0-9-]*/UUID='${UUIDS[1]}'/' >> /mnt/etc/fstab
rm /mnt/etc/fstab.orig #TODO OFC we need to check mountpoint, not only type of FS 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
echo "==> Result:" echo "==> Result:"
cat /mnt/etc/fstab cat /mnt/etc/fstab
##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
else
cat /mnt/etc/default/grub | sed 's/cryptdevice=UUID=[A-Fa-f0-9-]*:cryptroot //' > /mnt/etc/default/grub
fi
cat /mnt/etc/default/grub | sed 's/resume=UUID=[A-Fa-f0-9-]*/resume=UUID='${UUIDS[0]}'/' > /mnt/etc/default/grub 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
else
cat /mnt/etc/default/grub | sed 's/cryptdevice=UUID=[A-Fa-f0-9-]*:cryptroot //' > /mnt/etc/default/grub
fi
##mount system stuff cat /mnt/etc/default/grub | sed 's/resume=UUID=[A-Fa-f0-9-]*/resume=UUID='${UUIDS[0]}'/' > /mnt/etc/default/grub
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 updateBootloader()
echo "==> mkinitcpio and update grub..." {
##mount system stuff
echo "==> Mounting /dev, /sys, /proc..."
mount /dev /mnt/dev --bind
mount /sys /mnt/sys --bind
mount /proc /mnt/proc --bind
#cat /mnt/etc/mkinitcpio.conf | sed 's/encrypt //' > /mnt/etc/mkinitcpio.conf #remove cryptdevice, no encrypted fs today :c ##chroot, mkinitcpio and update grub
echo "==> mkinitcpio and update grub..."
#chroot env #cat /mnt/etc/mkinitcpio.conf | sed 's/encrypt //' > /mnt/etc/mkinitcpio.conf #remove cryptdevice, no encrypted fs today :c
#chroot env
cat << EOF | chroot /mnt cat << EOF | chroot /mnt
mkinitcpio -P mkinitcpio -P
grub-mkconfig -o /boot/grub/grub.cfg grub-mkconfig -o /boot/grub/grub.cfg
@ -182,9 +199,28 @@ grub-install $targetDrive
exit exit
EOF EOF
#deinit }
echo "==> Syncing..."
sync deinit()
echo "==> Unmounting..." {
umount -R /mnt #deinit
echo "==> Done!" echo "==> Syncing..."
sync
echo "==> Unmounting..."
umount -R /mnt
echo "==> Done!"
}
# funcions
testVars()
chooseBackup()
setTargetDisk()
warnDeploy()
partitionDisk()
makeFS()
getUUIDs()
mountDisk()
copyFiles()
updateFstab()
configBootloader()
updateBootloader()
deinit()