-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbackuprsync.sh
executable file
·203 lines (168 loc) · 5.71 KB
/
backuprsync.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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
#!/bin/bash
dir=`dirname $0`
cd $dir
rsyncparam="--one-file-system --delete -HAX --partial --stats --numeric-ids -r -a -z"
# include config
if [ -f $dir/config ]; then
source <(grep = $dir/config)
fi
for i in "$@"
do
case $i in
-u=*|--user=*)
user="${i#*=}"
;;
-s=*|--server=*)
server="${i#*=}"
;;
-p=*|--port=*)
port="${i#*=}"
;;
-k=*|--key=*)
key="${i#*=}"
;;
--backupfs=*)
backupfs="${i#*=}"
;;
--exclude=*)
exclude="--delete-excluded --exclude-from=${i#*=}"
;;
--include=*)
include="--include-from=${i#*=}"
;;
-t=*|--type=*)
type="${i#*=}"
;;
-e=*|--ext=*)
ext="${i#*=}"
;;
--password=*)
password="${i#*=}"
;;
--savepath=*)
savepath="${i#*=}"
;;
--prefix=*)
prefix="${i#*=}"
;;
"--sudo=yes")
export sudo="sudo -E"
;;
-ok=*|--okerr=*)
okerr="${i#*=}"
;;
-h|--help)
help=1
;;
*)
echo $i unknown option
;;
esac
done
printhelp() {
cat << EOF
params: protocol: need: description:
-t|--type ssh|rsync yes type protocol
-u|--user ssh|rsync yes username (if remote)
-s|--server ssh|rsync yes servername set local if backup localhost filesystem
-prefix ssh|rsync no prefix servername - need to human readable save path
-p|--port ssh|rsync no if remote; ssh or rsyncd port
--password |rsync no rsync auth password
-k|-key ssh| yes ssh key auth
--backupfs ssh|rsync yes filesystem over coma e.q. /,/boot, if rsync type - backupfs is modulename cfg file
--exclude ssh|rsync no path file to excludefile
--include ssh|rsync no path file to includefile
-e|--ext ssh|rsync no external params to rsync
--savepath ssh|rsync yes path to local backup dir
-ok|--okerr |rsync no normal rsync exit code, not 0
-h|--help ssh|rsync no print this help
--sudo ssh| no Set yes if need use local sudo rsync
EOF
exit
}
[[ $help -eq 1 ]] && printhelp
#check params
for i in "$backupfs" "$savepath" "$backupfs" "$server" "$type"
do
cnt=$((cnt+1))
if [[ "$i" == "" ]]; then
echo Mandatory param $cnt is empty. Need params: "backupfs savepath backupfs server type"
exit 1
fi
done
if [[ "$type" == "ssh" && -z "$key" ]]; then
echo If use type=ssh - mandatory "key" param!
exit 1
fi
. `dirname $0`/function
if [[ -z $ducount_on_rotate ]]; then
echo "Отсутствует параметр ducount_on_rotate."
echo "задаем по умолчанию ducount_on_rotate=0"
ducount_on_rotate=0
fi
for backup in `echo $backupfs | sed "s/,/\ /g"`; do
fs=`fsname $backup`
logzbx=$savepath/zabbix-alert.log
logbackup=$savepath/$fservername/latest-$fs/errorsbackup.log
logglobal=$savepath/errorsbackup.log
printf "%s" "start backup $fs on $fservername:"
printf "%s" "type:$type. Start rsync..."
mkdir -p $savepath/$fservername/latest-$fs
[[ "$type" == "rsync" && "$port" != "" ]] && backupsrv="rsync://$user@$server:$port/"
[[ "$type" == "rsync" && "$port" == "" ]] && backupsrv="rsync://$user@$server/"
[[ "$type" == "ssh" ]] && backupsrv="$user@$server:"
[[ "$type" == "local" || "$server" == "local" ]] && backupsrv=""
case $type in
"ssh"|"local")
port="-p $port"
if [[ -z $sudo ]]; then
rsync $backupsrv$backup $savepath/$fservername/latest-$fs \
-e "ssh $port -i $key" \
$rsyncparam $exclude $include \
$ext >> /tmp/$$.log 2>&1
exitrsync=$?
else
$sudo \
rsync $backupsrv$backup $savepath/$fservername/latest-$fs \
--rsync-path="sudo rsync" \
-e "ssh $port -i $key" \
--one-file-system --delete \
-A -H --archive --numeric-ids \
$exclude $include \
$ext >> /tmp/$$.log 2>&1
exitrsync=$?
fi
mv -f /tmp/$$.log $logbackup
;;
"rsync")
export RSYNC_PASSWORD="$password"
$sudo rsync $backupsrv$backup $savepath/$fservername/latest-$fs \
$rsyncparam $exclude $include \
$ext >> /tmp/$$.log 2>&1
exitrsync=$?
mv -f /tmp/$$.log $logbackup
;;
esac
echo $okerr | grep -q $exitrsync && exitrsync=0 # check exit code and fix if ok
[ $exitrsync -ne 0 ] && \
echo "`date` Error: Exit rsync code: $exitrsync: see log $logbackup" | tee -a $logglobal >> $logzbx
if [[ $ducount_on_rotate != 0 ]]; then
printf "%s" "du latest-$fs... "
rm -f $savepath/$fservername/latest-$fs/du.txt
ducount "$savepath/$fservername/latest-$fs" "$savepath/$fservername/latest-$fs/du.txt"
printf "%s" "du all $fs... "
rm -f $savepath/$fservername/latest-$fs/du-all.txt
ducount "$savepath/$fservername/$fs-* $savepath/$fservername/latest-$fs" "$savepath/$fservername/latest-$fs/du-all.txt"
fi
printf "%s" "cp... "
if [ $exitrsync -eq 0 ]; then
dst_path=$fs-$date
else
dst_path=$fs-$date-err
fi
mkdir -p $savepath/$fservername/$dst_path
cp --link --archive $savepath/$fservername/latest-$fs/* $savepath/$fservername/$dst_path/ >> $logbackup 2>&1
exitcp=$?
[ $exitcp -ne 0 ] && echo "`date` Error: Exit cp code: $exitcp: see log $logbackup" | tee -a $logglobal >> $logzbx
printf "%s\n" "done. "
done