-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmikrotikbck.sh
executable file
·163 lines (141 loc) · 5.82 KB
/
mikrotikbck.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
#!/bin/bash
# Script to automate backups for Mikrotiks
BCKSd="/home/mikrotiks/"
trap "rm $BCKSd*.rsc.tmp" EXIT # If we combine Ctrl+C we delete all the .rsc.tmp files
if (( $# < 1 ));then
echo "Need at least one param"
exit 1
fi
input=$1
DATA=`date +%Y-%m-%d`
CORREU="/var/log/mikrotik/mikrotiksbck_$DATA.log"
sshlog="/var/log/mikrotik/mikrotikErrSHH_$DATA.log"
sendmail="[email protected]" # Change this
regexdyn='^[a-zA-Z0-9]{12}\.sn\.mynetname\.net$' # mikrotik dyn dns
regexip='^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$' # check if ip is valid
wcc=$(cat $input | grep -v "#" | wc -l)
OK=0
ERR=0
# Re-order the $input file
sed -i '/^$/d' $input
grep "#" $input > /tmp/ordered-servers.tmp
grep -v "#" $input | sort >> /tmp/ordered-servers.tmp
cat /tmp/ordered-servers.tmp > $input
rm /tmp/ordered-servers.tmp
if [ -f $CORREU ];then
rm $CORREU
fi
echo "<html>" >> $CORREU
echo "<head>" >> $CORREU
echo "</head>" >> $CORREU
echo "<body>" >> $CORREU
echo "<h2>BACKUPS MIKROTIK USERS - $(date)</h2>" >> $CORREU
echo "<p>Users: $wcc</p>" >> $CORREU
echo "<br/>" >> $CORREU
echo "<hr/>" >> $CORREU
# Check for $input file is well done
while read line
do
if [[ $(echo $line | cut -c 1) != "#" ]];then
client=$(echo $line | awk -F "," '{print $1}')
ip=$(echo $line | awk -F "," '{print $2}')
passwd=$(echo $line | awk -F "," '{print $3}')
port=$(echo $line | awk -F "," '{print $4}')
if [ -z $client ];then
echo "<div style=\"color:red;\"> <a>File $input is NOT well done (Client: $client/IP: $ip/Pass: $pass/Port: $port)<br/></a></div>" >> $CORREU
cat $CORREU | mail -a "Content-type: text/html ; charset=UTF-8" -s "MikrotiksBCK: File $input is NOT well done (client-$client)" $sendmail
exit 1
fi
if [ -z $ip ];then
echo "<div style=\"color:red;\"> <a>File $input is NOT well done (Client: $client/IP: $ip/Pass: $pass/Port: $port)<br/></a></div>" >> $CORREU
cat $CORREU | mail -a "Content-type: text/html ; charset=UTF-8" -s "MikrotiksBCK: File $input is NOT well done (ip1-$ip)" $sendmail
exit 1
fi
if ! [[ $ip =~ $regexip || $ip =~ $regexdyn ]]; then
echo "<div style=\"color:red;\"> <a>File $input is NOT well done (Client: $client/IP: $ip/Pass: $pass/Port: $port)<br/></a></div>" >> $CORREU
cat $CORREU | mail -a "Content-type: text/html ; charset=UTF-8" -s "MikrotiksBCK: File $input is NOT well done (ip2-$ip)" $sendmail
exit 1
fi
if [ -z $passwd ];then
echo "<div style=\"color:red;\"> <a>File $input is NOT well done (Client: $client/IP: $ip/Pass: $pass/Port: $port)<br/></a></div>" >> $CORREU
cat $CORREU | mail -a "Content-type: text/html ; charset=UTF-8" -s "MikrotiksBCK: File $input is NOT well done (pass-$pass)" $sendmail
exit 1
fi
if ! [[ $port =~ ^[0-9]{1,5}$ || -z $port ]]; then
echo "<div style=\"color:red;\"> <a>File $input is NOT well done (Client: $client/IP: $ip/Pass: $pass/Port: $port)<br/></a></div>" >> $CORREU
cat $CORREU | mail -a "Content-type: text/html ; charset=UTF-8" -s "MikrotiksBCK: File $input is NOT well done (port-$port)" $sendmail
exit 1
fi
fi
done < "$input"
while read line
do
if [[ $(echo $line | cut -c 1) != "#" ]];then
client=$(echo $line | awk -F "," '{print $1}')
ip=$(echo $line | awk -F "," '{print $2}')
passwd=$(echo $line | awk -F "," '{print $3}')
port=$(echo $line | awk -F "," '{print $4}')
if ! [ -d $BCKd$client/ ];then
echo "Directory $BCKd$client doesn't exists" > /dev/null # DEBUG
mkdir $BCKd$client/
fi
# Check if host replies to icmp
cmd="ping -c 3 $ip"
loss=$($cmd | grep "loss" | cut -d "," -f3 | cut -d " " -f2)
if [ $loss == "0%" ];then
echo "User $client with IP $ip replies to ping" > /dev/null # DEBUG
if [ -z $port ];then
port2=""
else
port2=$(echo -p $port)
fi
sshpass -p $passwd ssh -no StrictHostKeyChecking=no -o ConnectTimeout=10 $port2 admin@$ip export show-sensitive > $BCKd$client/$client.rsc.tmp 2> $sshlog
if [ $? == '0' ];then
echo "<a>$client OK<br/></a>" >> $CORREU
mv $BCKd$client/$client.rsc.tmp $BCKd$client/$client"0".rsc
OK=$(($OK+1))
else
echo -e "<div style=\"color:red;\"> <a>$client ERROR => $(cat $sshlog)<br/></a></div>" >> $CORREU
ERR=$(($ERR+1))
fi
# Log rotate
if [ -f $BCKd$client/$client"0".rsc ];then # Check if first backup went well
echo "File $client"0".rsc exists" > /dev/null # DEBUG
if [ -f $BCKd$client/$client"7".rsc ];then # Check if we have 7 days backup
echo "File $client"7".rsc exists" > /dev/null # DEBUG
rm $BCKd$client/$client"7".rsc
else
echo "File $client"7".rsc doesn't exists" > /dev/null # DEBUG
fi
for i in {6..0}
do
if [ -f $BCKd$client/$client$i.rsc ];then
mv $BCKd$client/$client$i.rsc $BCKd$client/$client$(($i+1)).rsc
echo "We moved $client$i to $client$(($i+1))" > /dev/null # DEBUG
else
echo "File $BCKd$client/$client$i.rsc doesn't exists" > /dev/null # DEBUG
fi
done
else
echo "ERROR - File $BCKd$client/$client"0".rsc doesn't exists" > /dev/null # DEBUG
fi
else
echo "ERROR: $client with IP $ip doesn't reply to ping!!" > /dev/null # DEBUG
echo -e "<div style=\"color:red;\"> <a>$client ERROR => IP $ip doesn't reply to ping!!<br/></a></div>" >> $CORREU
ERR=$(($ERR+1))
fi
fi
done < "$input"
if [ -f *.rsc.tmp ];then
rm *.rsc.tmp
fi
if [ -f /var/log/mikrotik/mikrotikErrSHH_*.log ];then
rm /var/log/mikrotik/mikrotikErrSHH_*.log
fi
echo "<br/>" >> $CORREU
echo "<hr/>" >> $CORREU
echo "<h3>BACKUPS FINISHED - $(date)</h3>" >> $CORREU
echo "</body>" >> $CORREU
echo "</html>" >> $CORREU
SUBJECT="Backups Mikrotiks $DATA || Errors: $ERR Successfully: $OK"
cat $CORREU | mail -a "Content-type: text/html ; charset=UTF-8" -s "$SUBJECT" $sendmail