1 | #!/bin/bash |
---|
2 | #Restore script for Syn-3 (C)DatuX |
---|
3 | source /etc/backup.conf || exit 1 |
---|
4 | source $SCRIPTDIR/backuplib || exit 1 |
---|
5 | |
---|
6 | FILE=$1 |
---|
7 | if ! [ "$FILE" ]; then |
---|
8 | echo "Usage: $0 <filetorestore>" |
---|
9 | echo "The file should exist on the configured backup medium in /etc/backup.conf" |
---|
10 | echo "WARNING: This WILL format your $BACKUPDEV!" |
---|
11 | exit 1 |
---|
12 | fi |
---|
13 | |
---|
14 | cd / |
---|
15 | |
---|
16 | #exit if already running |
---|
17 | CheckRunning |
---|
18 | |
---|
19 | |
---|
20 | #stdout will go to logs from this point |
---|
21 | exec &> $LOG |
---|
22 | |
---|
23 | ulimit -n 100000 |
---|
24 | trap terminated 2 15 1 |
---|
25 | |
---|
26 | terminated() |
---|
27 | { |
---|
28 | echo "STAT:Restore session aborted!" |
---|
29 | if [ "$1" ]; then |
---|
30 | echo "$1"; |
---|
31 | fi |
---|
32 | syn3-state backup ALERT "Restore aborted! Your system is unusable right now, try restoring again." |
---|
33 | exit 1 |
---|
34 | } |
---|
35 | |
---|
36 | #devfs en udev are different, solve it this way |
---|
37 | vgscan --mknodes &> /dev/null |
---|
38 | |
---|
39 | echo "INFO:Restore started at `date`" |
---|
40 | echo "INFO:Restoring $FILE" |
---|
41 | syn3-state backup ALERT "Disaster restore started. Your system is unusable until the restore has completed." |
---|
42 | |
---|
43 | ############################# |
---|
44 | echo "STAT:Preparing $BACKUPDEV" |
---|
45 | |
---|
46 | echo "INFO:Killing all services that use $BACKUPMNT" |
---|
47 | #shut them all down except ssh |
---|
48 | svc -d /service/* /service/*/log |
---|
49 | svc -u /service/sshd |
---|
50 | #force kill them all |
---|
51 | kill -9 `lsof -F p $BACKUPMNT|cut -c2-` &>/dev/null |
---|
52 | sleep 5 |
---|
53 | |
---|
54 | |
---|
55 | #TODO: maybe we skip the whole formatting and just use a rm -rf for other modes? |
---|
56 | #rsync is cabable of deleting stuff itself and can continue restoring in case of an interruption |
---|
57 | if [ $mode != "rsync" ]; then |
---|
58 | echo "INFO:Unmounting $BACKUPMNT" |
---|
59 | umount $BACKUPMNT >/dev/null |
---|
60 | if cat /proc/mounts |cut -f2 -d' '|grep -x $BACKUPMNT >/dev/null; then |
---|
61 | echo "Cannot unmount $BACKUPMNT" |
---|
62 | terminated |
---|
63 | fi |
---|
64 | echo "INFO:Formatting..." |
---|
65 | mkfs.xfs -f $BACKUPDEV >/dev/null || terminated |
---|
66 | |
---|
67 | echo "INFO:Mounting..." |
---|
68 | mount $BACKUPMNT || terminated |
---|
69 | fi |
---|
70 | |
---|
71 | ################################## |
---|
72 | echo "STAT:Restoring data" |
---|
73 | rm -r /var/lib/xfsdump/ &>/dev/null |
---|
74 | |
---|
75 | echo "INFO:Transferring data..." |
---|
76 | |
---|
77 | #this block should output the errors of the operation so that |
---|
78 | #the while loop can read them |
---|
79 | ( |
---|
80 | if [ $mode == "smb" ]; then |
---|
81 | smbclient --timeout 3600 -s /dev/null --stderr -c "get $FILE -" -U "$smbuser" -W "$smbdomain" "//$smbserver/$smbshare" "$smbpasswd" | gunzip | xfsrestore -o -p 10 -F - $BACKUPMNT || terminated |
---|
82 | [ "${PIPESTATUS[0]}" != "0" ] && terminated "Error while reading from fileserver." |
---|
83 | exit 0 |
---|
84 | elif [ $mode == "tape" ]; then |
---|
85 | xfsrestore -L "$FILE" -o -p 10 -F -f $TAPEDEV $BACKUPMNT || terminated |
---|
86 | elif [ $mode == "usbtape" ]; then |
---|
87 | xfsrestore -L "$FILE" -o -b 65536 -p 10 -F -f $TAPEDEV $BACKUPMNT || terminated |
---|
88 | elif [ $mode == "rsync" ]; then |
---|
89 | export PASSWD="$rsyncpasswd" |
---|
90 | RSYNCSRC=`echo "$FILE" | sed 's@syn3backupmeta/syn3_.*@@'` |
---|
91 | /usr/backup/sshpass rsync $RSYNC_SETTINGS --rsh "$SSHCMD" -ax --numeric-ids --progress --delete --inplace --rsync-path="rsync --fake-super" "$rsyncserver:$RSYNCSRC" $BACKUPMNT/ || terminated |
---|
92 | else |
---|
93 | echo "Backupmode $mode is unknown!"; |
---|
94 | terminated |
---|
95 | fi |
---|
96 | ) 2>&1 | |
---|
97 | #analyse the output of the above block |
---|
98 | while read LINE; do |
---|
99 | ProcXfsOutput "$LINE" || terminated |
---|
100 | done |
---|
101 | |
---|
102 | #something went wrong? |
---|
103 | if [ "${PIPESTATUS[0]}${PIPESTATUS[1]}" != "00" ]; then |
---|
104 | exit 1 |
---|
105 | fi |
---|
106 | |
---|
107 | ################################## |
---|
108 | ###### create special files to let the restore-magic happend during reboot: |
---|
109 | #Look at: http://open.syn3.nl/syn3/trac/default/wiki/SynBackup for technical details. |
---|
110 | |
---|
111 | #we have a systembackup to restore? |
---|
112 | if [ -e $BACKUPMNT/.syn3systembackup ]; then |
---|
113 | #touch this file so the initrd will format / and /boot and copy the data there. |
---|
114 | touch /boot/.syn3restoresystem |
---|
115 | #for reduncany: make sure the system knows the data is consistent and the node is primary |
---|
116 | touch $BACKUPMNT/.syn3systembackup/boot/drbd.primary |
---|
117 | #make sure the post restore runs after restoring the system files |
---|
118 | chmod +x $BACKUPMNT/.syn3systembackup/etc/postinst.d/post.restore |
---|
119 | else |
---|
120 | #make sure the restore postinstaller runs |
---|
121 | chmod +x /etc/postinst.d/post.restore |
---|
122 | fi |
---|
123 | |
---|
124 | #inicate we want the post.restore to run the restore-scripts |
---|
125 | mkdir -p "$SPOOLDIR" 2>/dev/null |
---|
126 | touch "$SPOOLDIR/.syn3restorespool" 2>/dev/null |
---|
127 | |
---|
128 | sync #in case they wanna use reset ;) |
---|
129 | |
---|
130 | echo "INFO:Data reading finshed at `date`" |
---|
131 | echo "STAT:Data reading complete, please reboot to finish restore." |
---|
132 | |
---|
133 | syn3-state backup ALERT "Please reboot the system and wait for the disaster recovery to finish." |
---|