ansitest/VIRTBOX/RESTORE-fsarchive-root.sh

120 lines
4.0 KiB
Bash
Raw Permalink Normal View History

2021-04-09 05:46:27 -05:00
#!/bin/bash
# no dvd #2 requirement - use sshfs
2021-04-09 06:35:58 -05:00
# NOTE this script should be included on the resulting ISO from mkrestoredvdiso.sh / or OSX version
# To be run from systemrescuecd environment; NOTE restore disk sda MUST be partitioned 1st!
2024-04-19 10:04:58 -06:00
# REQUIRES 1 arg: filename of .fsa to restore
2021-04-09 05:46:27 -05:00
# copy this script to /tmp and chmod +x, run from there
2021-04-09 06:07:11 -05:00
# NOTE ISO from mkrestoredvdiso should be mounted on 2nd dvd drive
# NOTE having a copy of supergrubdisc is handy if the VM fails to boot
# REF: https://distrowatch.com/table.php?distribution=supergrub
2022-04-22 12:52:47 -05:00
# If you prefer not to use an ISO to restore from, systemrescuecd has sshfs:
#
# sshfs -C -o Ciphers=chacha20-poly1305@openssh.com loginid@ipaddress:/path/to/backupfile \
# /mnt/path \
# -o follow_symlinks || \
# sshfs -C -o Ciphers=aes128-gcm@openssh.com loginid@ipaddress:/path/to/backupfile \
# /mnt/path \
# -o follow_symlinks
2021-04-09 05:46:27 -05:00
# failexit.mrg
function failexit () {
echo '! Something failed! Code: '"$1 $2" # code # (and optional description)
exit $1
}
2021-04-09 06:07:11 -05:00
# xxx TODO editme - assumes swap is on sda1
rootdev=/dev/sda2 # for VM - assuming sda1 is swap - use vda for virtio
2021-04-09 05:46:27 -05:00
rdevonly=${rootdev%[[:digit:]]}
umount $rootdev # failsafe
2022-04-22 12:52:47 -05:00
# xxx comment out references to cdrom2 if not using it
#cdr=/mnt/cdrom2
#mkdir -pv $cdr
2021-04-09 05:46:27 -05:00
rootdir=/mnt/tmp2
umount $rootdir # failsafe
mkdir -pv $rootdir
myres=BOOJUM-RESTORE.sh # injection script
# NOTE sr0 is systemrescue
#mount /dev/sr1 $cdr -oro
#chkmount=$(df |grep -c $cdr)
2024-04-19 10:04:58 -06:00
#[ $chkmount -gt 0 ] || failexit 99 "Failed to mount $cdr"; # failed to mount
2021-04-09 06:07:11 -05:00
chkmount=$(df |grep -c $rootdir)
[ $chkmount -eq 0 ] || failexit 98 "$rootdir is still mounted - cannot restore!"
2021-04-09 05:46:27 -05:00
2024-04-19 10:04:58 -06:00
#cd $cdr || failexit 199 "Cannot CD to $cdr";
2021-04-09 05:46:27 -05:00
pwd
2021-04-09 06:07:11 -05:00
echo "$(date) - RESTORING root filesystem to $rootdev"
2021-04-09 05:46:27 -05:00
# PROBLEM with long filenames in UDF - gets cut off, use $1
#time fsarchiver restfs *.fsa id=0,dest=$rootdev
time fsarchiver restfs "$1" id=0,dest=$rootdev || failexit 400 "Restore failed!";
date
# boojumtastic!
tune2fs -m1 $rootdev
mount $rootdev $rootdir -onoatime,rw
# Comment out any existing swap partitions in restored fstab
# REF: https://unix.stackexchange.com/questions/295537/how-do-i-comment-lines-in-fstab-using-sed
#sed -e '/[/]/common s/^/#/' /etc/fstab
/bin/cp -v $rootdir/etc/fstab $rootdir/etc/fstab--bkp && \
sed -i '/swap/s/^/#/' $rootdir/etc/fstab; grep swap $rootdir/etc/fstab
# Detect swap partition(s) in restore environ - print /dev/sdXX w/o ":"
2021-04-09 06:07:11 -05:00
for p in $(blkid |grep TYPE=\"swap\" |awk -F: '{ print $1 }'); do
2021-04-09 05:46:27 -05:00
pmod=${p##*/} # strip off beginning /dev/, leave sdXX
2021-04-09 06:07:11 -05:00
swapuuid=$(ls -l /dev/disk/by-uuid |grep $pmod |awk '{ print $9 }')
2021-04-09 05:46:27 -05:00
# check have we already done this?
2021-04-09 06:07:11 -05:00
[ $(grep -c $swapuuid $rootdir/etc/fstab) -gt 0 ] || \
2021-04-09 05:46:27 -05:00
echo "UUID=$swapuuid swap swap defaults,pri=2 0 0" |tee -a $rootdir/etc/fstab
# echo "UUID=$swapuuid swap swap defaults,pri=2 0 0" >> $rootdir/etc/fstab
done
# /dev/sdb5
#ls -l /dev/disk/by-uuid |grep sdb5
2021-04-09 06:07:11 -05:00
# 1 2 3 4 5 6 7 8 9 10 11
2021-04-09 05:46:27 -05:00
#lrwxrwxrwx 1 root root 10 Aug 27 11:09 dfc46f8f-bcfa-4e73-b62f-b24dd0bf60cf -> ../../sdb5
# Make sure we can boot!
grub-install --root-directory=$rootdir $rdevonly
mount -o bind /dev $rootdir/dev; mount -o bind /proc $rootdir/proc; mount -o bind /sys $rootdir/sys
# FIXED
myres2=$rootdir/$myres
touch $myres2 || failexit 298 "Check if R/O filesystem?"
echo "#!/bin/bash" > $myres2 || failexit 299 "Cannot update $myres2 injection script - Check R/O filesystem?"
echo "update-grub" >> $myres2
echo "grub-install $rdevonly" >> $myres2 # from chroot
echo "exit;" >> $myres2
#^D
# inject script here!
chroot $rootdir /bin/bash /$myres
#umount -a $rootdir/*
#umount -a $rootdir/{dev,proc,sys}
2021-04-09 06:07:11 -05:00
#umount $rootdir/* 2>/dev/null
2021-04-09 06:07:11 -05:00
#umount $rootdir 2>/dev/null
2021-04-09 05:46:27 -05:00
2021-04-09 06:07:11 -05:00
df -hT
2021-04-09 05:46:27 -05:00
2021-04-09 06:07:11 -05:00
echo "DON'T FORGET TO COPY /home and adjust fstab for home / squid BEFORE booting new drive!"
2021-04-09 05:46:27 -05:00
echo "+ also adjust etc/network/interfaces , getdrives-byid , etc/rc.local , etc/hostname , etc/hosts ,"
echo "+ etc/init/tty11 port (home/cloudssh/.bash_login"
exit;