-
Notifications
You must be signed in to change notification settings - Fork 2
/
backup.sh
66 lines (52 loc) · 1.59 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
#!/bin/bash
###############################################################################
# The database backup script
#
# - Automatic backup all databases in format $database-$Y-$m-$d.sql.gz
# - Send backups to Dropbox
#
# Uses: https://github.com/andreafabrizi/Dropbox-Uploader
# Link: https://deluxeblogtips.com/vps-backup-wordpress/
###############################################################################
# The main function
main() {
local folder=./backup
local today=$(date +%F)
local user=root
local dump="mysqldump --skip-extended-insert --force"
local file
local database
echo "# Backup databases";
# Get list of all databases
local databases=$(echo "SHOW databases" | mysql -u $user)
for database in $databases; do
# Skip if this is the default built-in MySQL database
if [ "$database" = "Database" ] || [ "$database" = "information_schema" ] || [ "$database" = "mysql" ] || [ "$database" = "performance_schema" ] || [ "$database" = "sys" ]
then
continue
fi
# Backup
echo " - Backing up the database $database";
file="$folder/$database-$today.sql.gz"
$dump -u $user $database | gzip -9 > $file
# Upload to Dropbox
echo " - Uploading the database $database to Dropbox";
upload $file $database.sql.gz
done
echo ""
echo "# Cleaning up the backup folder"
cleanup $folder
}
# Upload a file to Dropbox
#
# @param $1 File to upload
# @param $2 File to store on Dropbox
upload() {
./dropbox_uploader.sh upload $1 $2
}
# Cleanup the backup folder, remove backups older than 30 days
cleanup() {
find "$1"/* -mtime +30 -exec rm {} \;
}
# Call the main function
main