-
Notifications
You must be signed in to change notification settings - Fork 1
/
backup.sh
114 lines (98 loc) · 2.58 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
#!/bin/bash
#
# backup.sh
# MT Grid Backup
# by Sean Butze ([email protected])
# 8/24/14
#
# Main backup script for MT Grid Backup.
# usage: sh backup.sh [-d | -D | -f | -F | -c ]
#
# -d : Backup databases
# -D : Upload database backups
# -f : Backup files
# -F : Upload file backups
# -c : Cleanup local backup archives
#
# Load configuration options
ROOT=`dirname $0`
source "$ROOT/backup.conf"
# Create temp storage directories if we need to
if [[ ! -d "$ROOT/db" ]]; then
mkdir $ROOT/db
fi
if [[ ! -d "$ROOT/files" ]]; then
mkdir $ROOT/files
fi
# DATABASE BACKUP
# Create mysql dump files of all databases
function db_backup {
databases=`mysql --user=$DB_USER --password=$DB_PASSWORD -h $DB_HOST -e "SHOW DATABASES;" | tr -d "| " | grep -v Database`
for db in $databases; do
if [[ "$db" != "information_schema" ]] && [[ "$db" != _* ]] ; then
filename=$db.`date +$DATEFORMAT`
echo "Dumping database: $db"
mysqldump --force --single-transaction --user=$DB_USER --password=$DB_PASSWORD -h $DB_HOST --databases $db > $ROOT/db/$filename.sql
gzip $ROOT/db/$filename.sql
rm -f $ROOT/db/$filename.sql
fi
done
echo "Database backup done"
}
# FILE BACKUP
# Create compressed archives of all domains
function file_backup {
for dir in $HOMEDIR/*/
do
dir=${dir%*/}
dirname=${dir##*/}
tar -zcf $ROOT/files/$dirname.`date +$DATEFORMAT`.tar.gz $dir/
done
echo "File backup done."
}
# S3 UPLOAD
# Uploads local backup files to Amazon S3
function s3_upload {
if [ $# -lt 1 ]; then
echo "ERROR: Must specify file type ('db' or 'files')."
exit
fi
export AWS_ACCESS_KEY_ID=$AWS_ACCESS_KEY_ID
export AWS_SECRET_KEY=$AWS_SECRET_KEY
export AWS_S3_BUCKET=$AWS_S3_BUCKET
for filepath in $ROOT/$1/*.gz
do
php $ROOT/src/s3upload.php $1 $filepath
done
echo "S3 upload done."
}
# CLEANUP
# Removes local backup files
function cleanup {
rm $ROOT/db/*.gz > /dev/null 2>&1
rm $ROOT/db/*.sql > /dev/null 2>&1
rm $ROOT/files/*.tar.gz > /dev/null 2>&1
echo "Cleanup done."
}
# Parse command line arguments
while [ $# -gt 0 ]
do
case $1 in
-d|--db) db_backup
;;
-f|--file) file_backup
;;
-D|--db-upload) s3_upload db
;;
-F|--file-upload) s3_upload files
;;
-c|--cleanup) cleanup
;;
(-*) echo "$0: error - unrecognized option $1" 1>&2; exit 1
;;
(*) break
;;
esac
shift
done
shift $((OPTIND - 1))