From bdf5e3143e7ccdf074a580ec4d3ddf976a60d8b9 Mon Sep 17 00:00:00 2001 From: Victor Wagner Date: Mon, 25 Apr 2022 18:42:16 +0300 Subject: [PATCH] Initial commit of restore script --- README.md | 87 +++++++++++++++++++++++++++++++++++++++++++ restore | 109 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 196 insertions(+) create mode 100644 README.md create mode 100755 restore diff --git a/README.md b/README.md new file mode 100644 index 0000000..2d43d8e --- /dev/null +++ b/README.md @@ -0,0 +1,87 @@ +Restoration script for full linux backup +======================================== + +Prepare for backups +------------------- + +Before you'll be able to restore your system from backup, you have to do +backups. + +1. Get an USB thumb drive or external HDD big enough to hold entire + content of our notebook's SSD. +2. Install gparted-live into beginning of this disk +3. Format rest of this disk (gparted would take no more than half of GB +at the beginning) as ext4 system +4. Install rsnapshot on your machine and set up it to make backup into + ``` + /media/${your name}/${label of your USB partition}/${hostname} + ``` + If you have big usb HDD and several notebooks, you can backup all of + them on one medium. + You should backup everything including /boot/efi, although you can + omit mozilla cache and other thinks which are likely to change during + backup. + +5. Do first backup. Do: + ``` + echo p |fdisk /dev/nvme0n1 > partitions.layout + + copy restore script into root of your backup partition. + +Do backup regularly +------------------- + +Don't forget to insert this USB drive into your notebook and run +rsnapshot with appropraite argument as root on regular basis. + +Use alpha/beta/gamma modes of rsnapshots, so you'll store daily backups +for last week, weekly ones for last month and several monthly ones. + +Restore old files occasionaly +----------------------------- + +Sometimes you'll find out that you have incedently removed or modified +some file. You can than plug your backup drive in and get yesterday's or +week ago copy. + +When disaster happens +--------------------- + +When your SSD drive dies, or have been irrepairable wiped out, +or hit by trojan or cryptolocker so it is easier to wipe out then do +anythin else: + +1. Repair the hardware +2. Insert your backup drive in USB port and boot from it. Mount your + second partition under, say /mnt +3. From parted-live GUI create neccessary partitions. You can consult + partitions.layout file which you have created while preparing backup. + Don't forget create vfat partion for /boot/efi, if you are using uefi + boot. +4. Mount newly created root partition under, say /target + and if you unse separate partions for /home, /var or anything else, + mount them on /target/home, /target/var etc. + Don't forget to mount /target/boot/ef +5. Cd to /mnt and run + ``` + restore /target + ``` + IF you keep backup for several + compiters on one disk or want to restore not the latest daily backup, + specify backup directory + + ``` + restore mynote/beta.1 /target + ``` +6. Reboot and enjoy. + +Other uses +---------- + +You can use this restoration procedure when you are upgrading your main +SSD - no need to search for USB SATA or USB NVME controller to copy data +from old disk. That is why we don't create partitions from script. User +may want to rearrange partition layout or just restore system on bigger +drive. + + diff --git a/restore b/restore new file mode 100755 index 0000000..1072b45 --- /dev/null +++ b/restore @@ -0,0 +1,109 @@ +#!/bin/sh + +if [ -z "$1" ] || [ "$1" = "--help" ]; then + cat >&2 << EOH +Usage + + $0 [backup-dir] restore-mount-point + +This is script to restore from rsnapshot-based backup. +It copies directory tree from backup dir to restore-mount-point +and updaes fstab there to have correct UUIDs of partitions mounted +underneath this mountpoint. + +You have to manually create partitions and filesystems on them and mount +it under target mount point. +EOH +fi + +if [ -z "$2" ]; then + # only one parameter is specified it is assumed to be target. So in + # the current directory must be only one backup + for candidate in alpha.0/etc "$(echo ./*/alpha.0/etc)"; do + if [ -d "$candidate" ]; then + backup="$(dirname "$candidate")" + break + fi + done + if [ -z "$backup" ]; then + echo "Cannot find backup directory. Please specify it explicitely" >&2 + exit 1 + fi + restore="$1" +else + backup="$1" + restore="$2" +fi + + +if [ ! -d "$backup/etc" ]||[ ! -d "$backup/home" ]||[ ! -d "$backup/usr" ]; then + echo "$backup doesn't look like full backup of unix filesystem" +fi + +if [ ! -d "$restore" ]; then + echo "Restore mount point $restore doesn't exists" >&2 + exit 1 +fi +tempf=$(mktemp) +mount|while read -r dev _ point _ type rest; do + case $point in + $restore|$restore/*) + uuid="$(blkid "$dev" --output export|grep '^UUID')" + targetpoint=${point#$restore} + [ -z "$targetpoint" ]&&targetpoint=/ + fsline=$(printf "%s %12s %s defaults 0 0" "$uuid" "$targetpoint" "$type") + echo "$fsline">>"$tempf" + ;; + *) + : + ;; + esac +done +# Scan for swap partitions on the same drive +root="$(df "$restore" --output=source|tail -1)" +drive="${root%[0-9]}" +for part in "$drive"*; do + data="$(blkid "$part" --output=export)" + uuid=$(echo "$data"|grep "^UUID=") + if echo "$data"|grep -q TYPE=swap; then + echo "$uuid none swap sw 1 0" >> "$tempf" + fi +done +while [ ! -e "$drive" ]; do + drive=${drive%?} +done +echo "Going to install boot loader on $drive" +#read fstab and copy out all lines which do not start with UUID +while read -r line; do + case "$line" in + UUID=*) + if [ -e "$tempf" ]; then + cat "$tempf" + rm "$tempf" + fi + ;; + *) + echo "$line" + ;; + esac +done < "$backup/etc/fstab" > /tmp/fstab.new +# restoring backup +echo "Restoring data" +rsync -a "$backup/" "$restore/" || exit 1 +echo "Updatinf fstab" +cp /tmp/fstab.new "$restore/etc/fstab" +rm "$restore/etc/udev/rules.d"/*-persistent-net.rules +echo "Installing bootloader" +mount -t proc none "$restore/proc" +mount -o bind /dev "$restore/dev" +mount -t sysfs sys "$restore/sys" +chroot "$restore" update-grub +chroot "$restore" grub-install "$drive" +umount "$restore/sys" +umount "$restore/dev" +umount "$restore/proc" + +echo "Restore completed. Please reboot" + + + -- 2.39.2