-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbackup.sh
82 lines (68 loc) · 1.57 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
#!/bin/bash
#
set -e
export PATH="$PATH:/usr/local/bin"
usage()
{
cat << EOF
usage: $0 parameters
This script dumps MySQL, tars it, then sends it to an Amazon S3 bucket.
PARAMETERS:
-help Show this message
-u MySQL user name
-p MySQL password
-h MySQL host <hostname><:port>
-d MySQL database
-b Amazon S3 bucket name
EOF
}
MYSQL_USER=
MYSQL_PASSWORD=
MYSQL_HOST=
MYSQL_DB=
S3_BUCKET_NAME=
while getopts “u:p:h:d:b:” OPTION
do
case $OPTION in
u)
MYSQL_USER=$OPTARG
;;
p)
MYSQL_PASSWORD=$OPTARG
;;
h)
MYSQL_HOST=$OPTARG
;;
d)
MYSQL_DB=$OPTARG
;;
b)
S3_BUCKET_NAME=$OPTARG
;;
?)
usage
exit
;;
esac
done
if [[ -z $MYSQL_USER ]] || [[ -z $MYSQL_PASSWORD ]] || [[ -z $MYSQL_DB ]] || [[ -z $MYSQL_HOST ]] || [[ -z $S3_BUCKET_NAME ]]
then
usage
exit 1
fi
# Get the directory the script is being run from
DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
echo $DIR
# Store the current date in the format: YYYY-mm-DD-HHMMSS
DATE=$(date -u "+%F-%H%M%S")
FILE_NAME="backup-$DATE"
ARCHIVE_NAME="$FILE_NAME.tar.gz"
# dump & tar
mysqldump -u "$MYSQL_USER" -p"$MYSQL_PASSWORD" -h "$MYSQL_HOST" "$MYSQL_DB" --single-transaction > $DIR/backup/$FILE_NAME
tar -C $DIR/backup/ -zcvf $DIR/backup/$ARCHIVE_NAME $FILE_NAME/
# Remove the backup file
rm -r $DIR/backup/$FILE_NAME
# Send the dump to S3
DATE_YYYY=$(date -u "+%Y")
DATE_YYYYMM=$(date -u "+%Y-%m")
aws s3 mv $DIR/backup/$ARCHIVE_NAME s3://$S3_BUCKET_NAME/$DATE_YYYY/$DATE_YYYYMM/$ARCHIVE_NAME