-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdbackup.sh
executable file
·174 lines (147 loc) · 3.85 KB
/
dbackup.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
#!/usr/bin/env bash
if ! command -v dar >/dev/null; then
echo "dar not found"
exit 1
fi
NO_COMPRESS=(
"7z" "ace" "ape" "apk" "arc" "arj" "avi"
"bz2" "cap" "dar" "deb" "dmg" "exe" "flac"
"flv" "gz" "ha" "ice" "iso" "jar" "jpg" "jpeg"
"lha" "lz" "lzh" "lzma" "lzo"
"m4a" "m4v" "mkv" "mov" "mp3" "mp4" "ogg"
"pak" "png" "rar" "rpm" "rz"
"s7z" "sea" "sfark" "sit" "sitx" "sqx" "sz"
"tbz" "tgz" "txz"
"wmv" "xz" "zip" "zoo" "zz"
)
NAME=""
SOURCE=""
TARGET=""
SKIP=0
function showusage {
echo "usage: $(basename $0) [arguments]
-s, --source The directory to compress.
-t, --target Optional target directory. Defaults to the parent directory
of the specified root directory, but cannot be the same.
-n, --name Optional name. Defaults to the name of the specified root
directory.
-m, --mtime Use mtime of source directory for timestamp instead of
current time.
--skip Skip test of created archive.
-h, --help Display this usage information.
"
}
if [ "$1" = "" ]; then
showusage
exit 1
fi
MTIME=0
while [[ "$1" != "" ]]; do
case $1 in
-s|--source)
if [ ! -d "$2" ]; then
echo "specified source is not a directory or directory not found"
exit 1
fi
SOURCE="$(cd "$2" && echo $PWD)"
shift
;;
-t|--target)
if [ ! -d "$2" ]; then
echo "specified target is not a directory or directory not found"
exit 1
fi
TARGET="$(cd "$2" && echo $PWD)"
shift
;;
-n|--name)
NAME="$2"
shift
;;
-m|--mtime)
MTIME=1
;;
--skip)
SKIP=1
;;
-h|-\?|--help)
showusage
exit 1
;;
*)
echo "unknown argument"
exit 1
;;
esac
shift
done
if [ "$SOURCE" = "" ]; then
echo "no source directory specified"
exit 1
elif [ "$SOURCE" = "/" ] && [ "$NAME" = "" ]; then
echo "argument -n is mandatory if source points to the root directory"
exit 1
fi
if [ "$TARGET" != "" ]; then
if [ ! -d "$TARGET" ]; then
echo "specified target is not a directory or directory not found"
exit 1
fi
else
TARGET=$(dirname "$SOURCE")
fi
if [ "$TARGET" -ef "$SOURCE" ]; then
echo "root and target directory cannot be the same"
exit 1
fi
if [ "$NAME" = "" ]; then
NAME="$(basename "$SOURCE")"
fi
PREV=$(ls -1 $TARGET/$NAME""_*_*.1.dar 2>/dev/null | egrep '_[^_]+_(base|diff)\.1\.dar$' | sort | tail -n 1)
if [ "$PREV" != "" ]; then
PREV=$(dirname $PREV)/$(basename $PREV .1.dar)
INCR="-A $PREV"
PFIX="_diff"
else
PREV=""
INCR=""
PFIX="_base"
fi
if [ $MTIME -eq 1 ]; then
TIME=$(date -u -r $SOURCE +"%Y-%m-%dT%H:%M:%SZ")
else
TIME=$(date -u +"%Y-%m-%dT%H:%M:%SZ")
fi
NAME="$NAME""_$TIME$PFIX"
FILE="$TARGET/$NAME"
echo "Creating archive: $FILE"
echo " from: $SOURCE"
if [ "$PREV" != "" ]; then
echo " previous: $PREV"
fi
# @see http://dar.sourceforge.net/doc/mini-howto/dar-differential-backup-mini-howto.en.html#making-a-full-backup-with-dar
# for usage details
Z=""
for i in "${NO_COMPRESS[@]}"; do
Z="$Z -Z \"*.$i\""
done
dar -m 256 -zbzip2 -y -s 1000G -R "$SOURCE" -c "$FILE" $Z $INCR #> /dev/null
err=$?
if [ $err -ne 0 ]; then
echo "Archive creation FAILED"
exit $err
fi
if [ $SKIP -eq 1 ]; then
echo "Archive created"
exit 0
else
dar -t "$FILE" > /dev/null
err=$?
if [ $err -ne 0 ]; then
echo "Archive created but test FAILED"
exit $err
else
echo "Archive created and successfully tested"
exit 0
fi
fi