-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbackup-gcs.sh
executable file
·105 lines (88 loc) · 4.11 KB
/
backup-gcs.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
#!/bin/bash
# Get date for tagging backups.
suffix=$(date +"%Y%m%d")
# Mark the date for deleting the database backup taken given number of days before today.
rotate=$(date +"%Y%m%d" -d "-7days")
# Set path and logging details
scriptpath="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
logfile="backup-$(hostname)-$suffix.log"
exitcodes="exit-codes.log"
# Import settings from config file
if [[ -f $scriptpath/settings.conf ]]; then
source $scriptpath/settings.conf
# If it doesn't exist, create the directory for storing database dumps as defined in settings.
if [[ ! -d "$mysql_output" ]]; then
mkdir -p $mysql_output
fi
# Skip databases set to be excluded in the settings.conf file.
excluded_dbs=( $mysql_exclude )
function excludeDBs()
{
local is_db_excluded="false"
for xdb in "${excluded_dbs[@]}"
do
if [ "$1" == "${xdb}" ]; then
is_db_excluded="true"
break
fi
done
echo "$is_db_excluded"
}
# Dump all databeses and create arcives.
databases=`mysql --user=$mysql_user --password=$mysql_password -e "SHOW DATABASES;" | tr -d "| " | grep -v Database`
echo $? >> $scriptpath/$exitcodes
for db in $databases; do
if [[ $(excludeDBs "$db") == "false" ]]; then
echo -e "========================================\nDumping database: $db\n========================================" >> $scriptpath/$logfile
mysqldump --force --opt --add-drop-table --log-error=$scriptpath/$logfile --user=$mysql_user --password=$mysql_password --databases $db > $mysql_output/$db.$suffix.sql
echo $? >> $scriptpath/$exitcodes
tar cfv $mysql_output/$db.$suffix.sql.tar -C $mysql_output $db.$suffix.sql
echo $? >> $scriptpath/$exitcodes
7z a -t7z -m0=lzma -mx=9 -mfb=64 -md=32m -ms=on $mysql_output/$db.$suffix.tar.7z $mysql_output/$db.$suffix.sql.tar >>$scriptpath/$logfile 2>&1
echo $? >> $scriptpath/$exitcodes
rm $mysql_output/$db.$rotate.tar.7z
fi
done
rm $mysql_output/*.{sql,tar}
# Sync all local assets with remote.
echo -e "========================================\nSynchronizing databases\n========================================" >> $scriptpath/$logfile
if [[ -z "$gs_sync_exclude" ]]; then
gsutil -m rsync $gs_sync_params $mysql_output gs://$gs_bucket_dbs &>> $scriptpath/$logfile
else
gsutil -m rsync $gs_sync_params -x "$gs_sync_exclude" $mysql_output gs://$gs_bucket_dbs &>> $scriptpath/$logfile
fi
echo $? >> $scriptpath/$exitcodes
echo -e "========================================\nSynchronizing virtual hosts\n========================================" >> $scriptpath/$logfile
if [[ -z "$gs_sync_exclude" ]]; then
gsutil -m rsync $gs_sync_params $root_vhosts gs://$gs_bucket_vhosts &>> $scriptpath/$logfile
else
gsutil -m rsync $gs_sync_params -x "$gs_sync_exclude" $root_vhosts gs://$gs_bucket_vhosts &>> $scriptpath/$logfile
fi
echo $? >> $scriptpath/$exitcodes
# Check if any errors happened during creating and synchronizing backups.
errorcount="$(grep -Ev '(^0|^$)' $scriptpath/$exitcodes|wc -l)"
# Send the report.
report=$(openssl enc -base64 -A -in $scriptpath/$logfile)
if [[ $errorcount -eq 0 ]]; then
errorstatus="without any errors"
elif [[ $errorcount -eq 1 ]]; then
errorstatus="with $errorcount error"
else
errorstatus="with $errorcount errors"
fi
echo "{From: '$mail_from', To: '$mail_to', Subject: 'Backup completed $errorstatus @ $(date) for $(hostname)', HtmlBody: 'Please find job report attached to this email.', TextBody: 'Please find job report attached to this email.', Attachments: [{Name: '$logfile', Content: '$report', ContentType: 'text/plain'}]}" >> $scriptpath/mail.json
curl "https://api.postmarkapp.com/email" \
-X POST \
-H "Accept: application/json" \
-H "Content-Type: application/json" \
-H "X-Postmark-Server-Token: $postmark_token" \
-d @$scriptpath/mail.json
# Set trap for cleanup
function cleanup {
rm $scriptpath/$logfile $scriptpath/$exitcodes $scriptpath/mail.json
}
trap cleanup EXIT
else
echo "Missing settings.conf file, please refer to README." > error-$(hostname)-$suffix.log
exit 0
fi