1 #!/bin/sh
2
3 # home_backup v 0.0000000003 (2008/02/20)
4 # Copyright (C) 2008 Adsaria
5
6 # This program is free software; you can redistribute it and/or
7 # modify it.
8 # This program is distributed in the hope that it will be useful,
9 # but WITHOUT ANY WARRANTY.
10
11 #### Following variables are environment dependent, modify them to your environment.
12
13 SRC_DIR=/home/
14 DST_DIR=/secondary/home/
15
16 SRC_DEV=/dev/sda3
17 DST_DEV=/dev/sdb3
18
19 ####
20
21
22 CMD=`basename $0`
23
24 ROOT_DEV=`df / | tail -1 | awk '{print $1}'`
25
26 DUMP_DATES=${SRC_DIR}dumpdates
27 MAX_DUMP_LEVEL=65535
28
29 # Set "-v", if you need
30 DUMP_OPT=""
31 REST_OPT=""
32
33 MessageAndLog () {
34 echo "$1"
35 logger "$1"
36 }
37
38 UNMOUNT=false
39
40 MOUNT_DEV=`df $DST_DIR | tail -1 | awk '{print $1}'`
41 if [ $MOUNT_DEV = $ROOT_DEV ]
42 then
43 MSG="$CMD: mounting $DST_DEV on $DST_DIR"
44 UNMOUNT=true
45 mount $DST_DEV $DST_DIR
46 if [ $? != 0 ]
47 then
48 MSG="$MSG : Failed to mount, exiting."
49 MessageAndLog "$MSG"
50 exit -1
51 fi
52 MSG="$MSG : Success."
53 MessageAndLog "$MSG"
54 elif [ $MOUNT_DEV != $DST_DEV ]
55 then
56 MSG="$CMD: Another device($MOUNT_DEV) is mounting on $DST_DIR. Exiting."
57 MessageAndLog "$MSG"
58 exit -1
59 fi
60
61 if [ -f $DUMP_DATES ]; then
62 LAST_LEVEL=`sort -k 7n -k 4M -k 5n -k 6 $DUMP_DATES | tail -1 | awk '{print $2}'`
63 if [ $LAST_LEVEL -eq $MAX_DUMP_LEVEL ]; then
64 DUMP_LEVEL=$MAX_DUMP_LEVEL
65 DUMP_OPT="$DUMP_OPT -$DUMP_LEVEL"
66 MOD_LEVEL=`expr $MAX_DUMP_LEVEL - 1`
67 sed -i -e "/^[^ ]*[ ]*$MOD_LEVEL[ ]*/d" \
68 -e "\$s/\([^ ]*\)[ ]*$LAST_LEVEL[ ]*\(.*\)/\1 $MOD_LEVEL \2/" $DUMP_DATES
69 else
70 DUMP_LEVEL=`expr $LAST_LEVEL + 1`
71 DUMP_OPT="$DUMP_OPT -$DUMP_LEVEL"
72 fi
73 REST_OPT="$REST_OPT -xou"
74 else
75 DUMP_LEVEL=0;
76 DUMP_OPT="$DUMP_OPT -$DUMP_LEVEL"
77 REST_OPT="$REST_OPT -r"
78 fi
79
80 MSG="$CMD: Start backup at `date`"
81 MessageAndLog "$MSG"
82
83 DUMP_RET_FILE=/tmp/$CMD.$$.dump_ret ; rm -f $DUMP_RET_FILE
84 REST_RET_FILE=/tmp/$CMD.$$.rest_ret ; rm -f $REST_RET_FILE
85
86 # { ( cd $SRC_DIR ; dump $DUMP_OPT -u -D $DUMP_DATES -b 1024 -f - . ) ; DUMP_RET=$? ; } | { ( cd $DST_DIR ; restore $REST_OPT -b 1024 -f - ) ; REST_RET=$? ; }
87
88 (cd $SRC_DIR ; dump $DUMP_OPT -u -D $DUMP_DATES -b 1024 -f - . ; echo $? > $DUMP_RET_FILE ; ) | (cd $DST_DIR ; restore $REST_OPT -b 1024 -f - ; echo $? > $REST_RET_FILE ; )
89
90 DUMP_RET=`cat $DUMP_RET_FILE` ; rm -f $DUMP_RET_FILE
91 REST_RET=`cat $REST_RET_FILE` ; rm -f $REST_RET_FILE
92
93 case $DUMP_RET in
94 0 ) MSG="Success" ;;
95 1 ) MSG="Startup error" ;;
96 3 ) MSG="Abnormal termination" ;;
97 * ) MSG="Unknown error"
98 esac
99 if [ $MSG = "Success" ]
100 then
101 MSG="$CMD: DUMP success"
102 else
103 MSG="$CMD: DUMP error: $MSG"
104 fi
105 MessageAndLog "$MSG"
106
107 case $REST_RET in
108 0 ) MSG="Success" ;;
109 1 ) MSG="Media error" ;;
110 2 ) MSG="Comparison error" ;;
111 * ) MSG="Unknown error"
112 esac
113 if [ $MSG = "Success" ]
114 then
115 MSG="$CMD: RESTORE success"
116 else
117 MSG="$CMD: RESTORE error: $MSG"
118 fi
119 MessageAndLog "$MSG"
120
121 rsync -avx --delete --delete-excluded "${DST_DIR}restoresymtable" $SRC_DIR $DST_DIR
122 case $? in
123 0 ) MSG="Success" ;;
124 1 ) MSG="Syntax or usage error" ;;
125 2 ) MSG="Protocol incompatibility" ;;
126 3 ) MSG="Errors selecting input/output files, dirs" ;;
127 4 ) MSG="Requested action not supported" ;;
128 5 ) MSG="Error starting client-server protocol" ;;
129 6 ) MSG="Daemon unable to append to log-file" ;;
130 10 ) MSG="Error in socket I/O" ;;
131 11 ) MSG="Error in file I/O" ;;
132 12 ) MSG="Error in rsync protocol data stream" ;;
133 13 ) MSG="Errors with program diagnostics" ;;
134 14 ) MSG="Error in IPC code" ;;
135 20 ) MSG="Received SIGUSR1 or SIGINT" ;;
136 21 ) MSG="Some error returned by waitpid()" ;;
137 22 ) MSG="Error allocating core memory buffers" ;;
138 23 ) MSG="Partial transfer due to error" ;;
139 24 ) MSG="Partial transfer due to vanished source files" ;;
140 25 ) MSG="The --max-delete limit stopped deletions" ;;
141 30 ) MSG="Timeout in data send/receive" ;;
142 * ) MSG="Unknown error"
143 esac
144 if [ $MSG = "Success" ]
145 then
146 MSG="$CMD: RSYNC success"
147 else
148 MSG="$CMD: RSYNC error: $MSG"
149 fi
150 MessageAndLog "$MSG"
151
152 MSG="$CMD: Finish backup at `date`"
153 MessageAndLog "$MSG"
154
155 if [ $UNMOUNT = "true" ]
156 then
157 MSG="$CMD: unmounting $DST_DEV from $DST_DIR"
158 umount $DST_DIR
159 if [ $? != 0 ]
160 then
161 MSG="$MSG : Failed to unmount, exiting."
162 MessageAndLog "$MSG"
163 exit -1
164 fi
165 MSG="$MSG : Success."
166 MessageAndLog "$MSG"
167 fi
168
169 exit 0