-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcmd.sh
executable file
·51 lines (43 loc) · 1.11 KB
/
cmd.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
#!/usr/bin/env bash
########################################
# Import/export SQL dumps via S3.
# Required env vars:
# * ACTION - either 'import' or 'export'
# * DB_HOST
# * DB_NAME
# * DB_USER
# * DB_PASS
# * BUCKET
# Optional when ACTION=export:
# * OBJECT_NAME - default {ISO date}.sql
########################################
import () {
echo "Importing $OBJECT_NAME from $BUCKET to $DB_HOST"
aws s3 cp $BUCKET/$OBJECT_NAME $OBJECT_NAME
mysql $DB_NAME --host=$DB_HOST --user=$DB_USER --password=$DB_PASS < $OBJECT_NAME
}
export () {
if [[ $OBJECT_NAME = "" ]]; then
OBJECT_NAME=$(date -Iseconds).sql
fi
echo "Dumping DB from $DB_HOST"
mysqldump $DB_NAME \
--host=$DB_HOST \
--user=$DB_USER \
--password=$DB_PASS \
--result-file=$OBJECT_NAME \
--routines \
--add-drop-table \
--verbose
echo "Backing up to $BUCKET as $OBJECT_NAME"
aws s3 cp $OBJECT_NAME $BUCKET/$OBJECT_NAME
}
if [[ $ACTION = "import" ]]; then
import
elif [[ $ACTION = "export" ]]; then
export
else
echo "ACTION must be either 'import' or 'export'"
exit 1
fi
[[ -f $OBJECT_NAME ]] && rm $OBJECT_NAME