-
Notifications
You must be signed in to change notification settings - Fork 0
/
backup.sh
executable file
·262 lines (208 loc) · 6.27 KB
/
backup.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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
#!/bin/bash
## create /admin/.info/backup.config
# dirs="folder1 folder2"
# dbs="db1 db2"
# swift=1;
#end
if [ ! -e /admin/.info/backup.config ]; then
echo "Backup not enabled";
exit;
fi
. /admin/.info/backup.config
if [ -f /root/.swift/hostname ]; then
hostname=`cat /root/.swift/hostname`;
echo "Overriding hostname at $hostname";
else
hostname=`hostname`;
fi
if [ -e /etc/master.passwd ]; then
find=/usr/local/bin/gfind;
date=/usr/local/bin/gdate;
if [ ! -e /usr/local/bin/gfind ]; then
echo "Install misc/findutils";
exit;
fi
if [ ! -e /usr/local/bin/gdate ]; then
echo "Install coreutils";
exit;
fi
else
find=find;
date=date;
fi
#1 weekly / monthly
#2 folder
function delete()
{
if [ "$1" = "" -o "$2" = "" ]; then
echo 'Blank value passed, exiting';
return;
fi
folder="/backup/$1/$2";
container="$hostname-backup-$1";
if [ ! -e /backup/$1/$2 ]; then
echo "/backup/$1/$2 does not exist";
return;
fi
}
function delete_weekly_monthly()
{
for del in Weekly Monthly; do
grep="grep -v "
for i in $(ls /backup/$del/ | sort -n | tail -n 4); do
grep="$grep -e $i"
done
oldweeks="$(ls /backup/$del/ | $grep)"
if [ ! "$oldweeks" = "" ]; then
for i in $oldweeks; do
echo "Removing Old $del data $i"
if [ "$swift" = "1" ]; then
delete $del $i
fi
rm -rf /backup/$del/$i
done
fi
done
}
function getrealfile()
{
#date is not included
passedfile=$1;
echo "found $passedfile"
file=`echo $passedfile | cut -d/ -f2`;
#echo "Found file $file";
savedfile="$file";
}
function backup()
{
#Daily/Weekly/Monthly
backup="$1";
if [ ! -d /backup/$backup ]; then
echo "Missing /backup/$backup";
return;
fi
cd /backup/$backup
# put a date so we known when this ran
fulldate=`date`;
echo $fulldate > date.txt
container="$hostname-backup-$backup";
/admin/swift/mkdir_p $container --force
echo "Working on container $container";
/admin/swift/rsync $container date.txt --put date.txt
if [ "$1" = "Daily" ]; then
cd /backup/Daily
todo=`ls | grep -v date.txt | tail -n 1`;
elif [ "$1" = "Weekly" ]; then
cd /backup/Weekly
todo=`ls | grep -v date.txt | tail -n 1`;
elif [ "$1" = "Monthly" ]; then
cd /backup/Monthly
todo=`ls | grep -v date.txt | tail -n 1`;
fi
for realfile in `${find} * -name "*.gz" | grep $todo`; do
#echo "found $realfile"
getrealfile $realfile
echo
# added in $year-$month-$day
/admin/swift/rsync $container $realfile --put $savedfile-$year-$month-$day
/admin/swift/rsync $container $realfile --check $savedfile-$year-$month-$day
# delete after 45 days
/admin/swift/deleteafter $container $savedfile-$year-$month-$day 45
echo
done
# old stuff delete
olddates='2012 2013 2013 2014 2015';
for olddate in $olddates; do
for realold in `/admin/swift/isls $container | grep "\-${olddate}-"`; do
/admin/swift/isrm $container $realold
done
done
}
export HOME=/root
year=$(${date} +%Y)
month=$(${date} +%m)
day=$(${date} +%d)
mkdir -p /backup/Monthly
mkdir -p /backup/Weekly
mkdir -p /backup/Daily/$year-$month-$day
for dir in $dirs; do
dirname="$(echo "$dir" | sed s#"/"#"_"#g)"
if [ ! -e "/backup/Daily/$year-$month-$day/$dirname.tar.gz" ] || [ -e "/backup/Daily/$year-$month-$day/$dirname.tar.gz.building" ]; then
echo "Backing Up: $dir"
touch /backup/Daily/$year-$month-$day/$dirname.tar.gz.building
tar czf /backup/Daily/$year-$month-$day/$dirname.tar.gz $dir && rm -f /backup/Daily/$year-$month-$day/$dirname.tar.gz.building
else
echo "Skipping: $dir"
fi
done
# skip if blank
# need /root/.my.cnf set
if [ ! "$dbs" = "" ]; then
for db in $dbs; do
mysqldump --single-transaction --quick --skip-extended-insert --add-drop-table --add-drop-database --quote-names -u root $db | gzip -c - > /backup/Daily/$year-$month-$day/$db.sql.gz
tar -zcf /backup/Daily/$year-$month-$day/$db.tar.gz /var/lib/mysql/$db
done
mysqldump --single-transaction --quick --skip-extended-insert --all-databases --add-drop-table --allow-keywords -u root | gzip -c > /backup/Daily/$year-$month-$day/all_db.sql.gz
fi
# daily delete
if [ -e /admin/.info/backup_evenless ]; then
for i in $(ls /backup/Daily/ | grep -v -e "$year-$month-$day" \
-e $(${date} -d "1 day ago" +%Y-%m-%d)
); do
echo "Removing $i"
rm -rf /backup/Daily/$i
if [ "$swift" = "1" ]; then
delete Daily $i
fi
done
elif [ -e /admin/.info/backup_less ]; then
for i in $(ls /backup/Daily/ | grep -v -e "$year-$month-$day" \
-e $(${date} -d "1 day ago" +%Y-%m-%d) \
-e $(${date} -d "2 day ago" +%Y-%m-%d)
); do
echo "Removing $i"
rm -rf /backup/Daily/$i
if [ "$swift" = "1" ]; then
delete Daily $i
fi
done
else
for i in $(ls /backup/Daily/ | grep -v -e "$year-$month-$day" \
-e $(${date} -d "1 day ago" +%Y-%m-%d) \
-e $(${date} -d "2 day ago" +%Y-%m-%d) \
-e $(${date} -d "3 day ago" +%Y-%m-%d) \
-e $(${date} -d "4 day ago" +%Y-%m-%d) \
-e $(${date} -d "5 day ago" +%Y-%m-%d) \
-e $(${date} -d "6 day ago" +%Y-%m-%d)
); do
echo "Removing $i"
rm -rf /backup/Daily/$i
if [ "$swift" = "1" ]; then
delete Daily $i
fi
done
fi
if [ $(${date} +%w) = 0 ]; then
if [ ! -e "/backup/Weekly/$year-$month-$day" ]; then
echo "Copying Weekly Backup"
cp -a /backup/Daily/$year-$month-$day /backup/Weekly/
fi
fi
if [ "$(ls /backup/Monthly/$year-$month-* 2>/dev/null)" = "" ]; then
echo "Copying Monthly Backup $year-$month-$day"
cp -a /backup/Daily/$year-$month-$day /backup/Monthly/
fi
# weekly / monthly delete
delete_weekly_monthly
# swift
if [ "$swift" = "1" ]; then
if [ -e /backup/Daily ]; then
backup Daily
fi
if [ -e /backup/Weekly ]; then
backup Weekly
fi
if [ -e /backup/Monthly ]; then
backup Monthly
fi
fi