-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmysql_db_backup_to_gdrive
73 lines (53 loc) · 2.22 KB
/
mysql_db_backup_to_gdrive
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
#!/bin/bash
###################################################################
#Script Name : mysql_db_backup_to_gdrive.sh
#Description : This script the dump the desire database from mysql
# and upload it to google drive specific folder.
#Args :
#Date : 20/03/2018 17:30:20
#Author : H.R. Shadhin
#Email : [email protected]
###################################################################
# Bail out if there are any errors.
set -e
#server name for dump file prefix
servername="myserver"
#user name
dbuser="root"
#user password
dbuserpassword="foobar"
# The name of the database we're going to backup.
dbname="my_db"
# The Google Drive folder ID where database exports will be uploaded to
gdrivefolderid="1D_1KQg6wURzd"
# Number of days we want to retain local backups for
retentiondays=1
# Date format for dates appended to database export files
dateformat="%Y-%m-%d_%H:%M:%S"
# The local directory where we'll be storing database exports
dumpdir="/var/spool/db-dump"
#skips tables name
EXCLUDED_TABLES=(
sym_channel
sym_conflict
sym_context
)
ignored_table_string=''
for TABLE in "${EXCLUDED_TABLES[@]}"
do :
ignored_table_string+=" --ignore-table=${dbname}.${TABLE}"
done
# Options to pass to the mysqldump command
mysqlopts="--user=$dbuser --password=$dbuserpassword --no-create-info --skip-triggers"
# Make sure the directory exists
mkdir -p "$dumpdir"
# Delete local export files older than our retentiondays value
find "$dumpdir" -type f -name "*.sql.gz" -mtime +"$retentiondays" -print -exec rm "{}" \;
# Zip up any existing export files
#find "$dumpdir" -type f -name "*.sql" -print -exec gzip "{}" \;
# Perform a backup of the live database
file="$servername-$dbname-$(date +$dateformat).sql.gz"
path="$dumpdir/$file"
mysqldump $mysqlopts "$dbname" $ignored_table_string | gzip > "$path"
# Upload the newly created file to Google Drive
/usr/local/bin/gdrive upload --parent "$gdrivefolderid" "$path"