-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbackup.sh
executable file
·72 lines (58 loc) · 2.05 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
#!/bin/bash
serverUrl="https://address:port"
loginUrl="${serverUrl}/login/"
username="<Your Username Here>"
password="<Your cPanel Password Here"
dbName="<databaseName here>"
tempFile=login_result.tmp
storageRoot="/tmp"
extension=".sql.gz"
#Log in and recover the session cookie and location
echo "Logging in to $loginUrl"
sessionLocation=`curl -s -i -F "user=$username" -F "pass=$password" $loginUrl | \
tee $tempFile | \
grep "Location" | cut -d "/" -f2`
sessionCookie=`grep cpsession $tempFile | sed 's/Set-Cookie:.\([^;]*\).*/\1/'`
echo "Recovered Location: $sessionLocation and cookie: $sessionCookie"
today=`date +%Y%m%d`
saveFilename="${dbName}_${today}${extension}"
dailyRoot="${storageRoot}/daily"
weeklyRoot="${storageRoot}/weekly"
monthyRoot="${storageRoot}/monthly"
yearlyRoot="${storageRoot}/yearly"
saveLocation="${dailyRoot}/${saveFilename}"
if [ ! -d "${dailyRoot}" ]; then
mkdir -p ${dailyRoot}
fi
if [ ! -d "${weeklyRoot}" ]; then
mkdir -p ${weeklyRoot}
fi
if [ ! -d "${monthlyRoot}" ]; then
mkdir -p ${monthlyRoot}
fi
if [ ! -d "${yearlyRoot}" ]; then
mkdir -p ${yearlyRoot}
fi
backupUrl="${serverUrl}/${sessionLocation}/getsqlbackup/${dbName}${extension}"
echo "Recovering database backup from: $backupUrl"
echo "Saving to: ${saveLocation}"
curl -o ${saveLocation} --cookie ${sessionCookie} $backupUrl > download_result.tmp
echo "Download complete"
dayOfWeek=`date +%u`
if [[ $dayOfWeek == "1" ]]; then
echo "First day of week, copying to ${weeklyRoot}"
cp "${saveLocation}" "${weeklyRoot}/${saveFilename}"
fi
dayOfMonth=`date +%d`
if [[ $dayOfMonth == "01" ]]; then
echo "First day of month, copying to ${monthlyRoot}"
cp "${saveLocation}" "${monthlyRoot}/${saveFilename}"
fi
dayOfYear=`date +%j`
if [[ $dayOfYear == "001" ]]; then
echo "First day of year, copying to ${yearlyRoot}"
cp "${saveLocation}" "${monthlyRoot}/${saveFilename}"
fi
#Now delete files that have been copied out
find ${dailyRoot} -type f -mtime +7 -name '*.gz' -execdir rm -- '{}' \;
find ${weeklyRoot} -type f -mtime +35 -name '*.gz' -execdir rm -- '{}' \;