]> www.wagner.pp.ru Git - oss/restore.git/blob - restore
Documented backup and afermount scripts
[oss/restore.git] / restore
1 #!/bin/sh
2
3 if [ -z "$1" ] || [ "$1" = "--help" ]; then
4         cat >&2 << EOH
5 Usage
6
7         $0 [backup-dir] restore-mount-point
8
9 This is script to restore from rsnapshot-based backup.
10 It copies directory tree from backup dir to restore-mount-point
11 and updaes fstab there to have correct UUIDs of partitions mounted
12 underneath this mountpoint. 
13
14 You have to manually create partitions and filesystems on them and mount
15 it under target mount point.
16 EOH
17 fi
18
19 if [ -z "$2" ]; then
20         # only one parameter is specified it is assumed to be target. So in
21         # the current directory must be only one backup 
22         for candidate in alpha.0/etc "$(echo ./*/alpha.0/etc)"; do
23                 if [ -d "$candidate" ]; then
24                         backup="$(dirname "$candidate")"
25                         break
26                 fi
27         done
28         if [ -z "$backup" ]; then
29                 echo "Cannot find backup directory. Please specify it explicitely" >&2
30                 exit 1
31         fi
32         restore="$1"
33 else
34         backup="$1"
35         restore="$2"
36 fi
37
38
39 if [ ! -d "$backup/etc" ]||[ ! -d "$backup/home" ]||[ ! -d "$backup/usr" ]; then
40         echo "$backup doesn't look like full backup of unix filesystem"
41 fi
42
43 if [ ! -d "$restore" ]; then
44         echo "Restore mount point $restore doesn't exists" >&2
45         exit 1
46 fi
47 tempf=$(mktemp)
48 mount|while read -r dev _ point _ type rest; do
49         case $point in
50         $restore|$restore/*)
51                 uuid="$(blkid "$dev" --output export|grep '^UUID')"
52                 targetpoint=${point#$restore}
53                 [ -z "$targetpoint" ]&&targetpoint=/
54                 fsline=$(printf "%s %12s %s   defaults  0    0" "$uuid" "$targetpoint" "$type")
55                 echo "$fsline">>"$tempf"
56         ;;
57         *) 
58         :
59         ;;
60         esac
61 done
62 # Scan for swap partitions on the same drive
63 root="$(df "$restore" --output=source|tail -1)"
64 drive="${root%[0-9]}"
65 for part in "$drive"*; do
66         data="$(blkid "$part" --output=export)"
67         uuid=$(echo "$data"|grep "^UUID=")
68         if echo "$data"|grep -q TYPE=swap; then
69                 echo "$uuid none         swap     sw    1  0" >> "$tempf"
70         fi
71 done
72 while [ ! -e "$drive" ]; do
73         drive=${drive%?}
74 done
75 echo "Going to install boot loader on $drive"
76 #read fstab and copy out all lines which do not start with UUID
77 while read -r line; do
78         case "$line" in
79         UUID=*) 
80                 if  [ -e "$tempf" ]; then
81                 cat "$tempf"
82                 rm "$tempf"
83                 fi
84                 ;;
85         *)
86                 echo "$line"
87                 ;;
88         esac
89 done < "$backup/etc/fstab" > /tmp/fstab.new
90 #  restoring backup
91 echo "Restoring data"
92 rsync -a "$backup/" "$restore/" || exit 1
93 echo "Updatinf fstab"
94 cp /tmp/fstab.new "$restore/etc/fstab"
95 rm "$restore/etc/udev/rules.d"/*-persistent-net.rules
96 echo "Installing bootloader"
97 mount -t proc none "$restore/proc"
98 mount -o bind /dev "$restore/dev"
99 mount -t sysfs sys "$restore/sys"
100 chroot "$restore" update-grub
101 chroot "$restore" grub-install "$drive"
102 umount "$restore/sys"
103 umount "$restore/dev"
104 umount "$restore/proc"
105
106 echo "Restore completed. Please reboot"
107
108         
109