-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdbdump.sh
executable file
·36 lines (28 loc) · 952 Bytes
/
dbdump.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
#!/bin/bash
# Simple database dumping script intended for use in development environments.
# Usage: "dbdump.sh <database>" eg. dbbump.sh wordpress_blog
# The default output location is your home directory.
# The default output file name is YYYYMMDD-HHMM_database_name.sql.gz
# To easily use the resulting dump: zcat YYYYMMDD-HHMM_database_name.sql.gz | mysql -uUSER -pPASS database_name
if [ "$#" -ne 1 ]; then
echo "Usage: $0 DB_NAME" >&2
exit 1
fi
DB_USER=''
DB_PASS=''
DB=$1
DUMP_DIR=$HOME'/'
DUMP_NAME=$(date +%Y%m%d)-$(date +%H%M)_$DB'.sql.gz'
DUMP_PATH=$DUMP_DIR$DUMP_NAME
if [ -z $DB_USER ] || [ -z $DB_PASS ]; then
echo 'ERROR: username or password not set'
exit 1
fi
$(echo 'exit' | mysql -u$DB_USER -p$DB_PASS 2>/dev/null)
if [ $? != 0 ]; then
echo 'ERROR: mysql credentials do not seem to be valid...'
exit 1
fi
echo "dumping ${DB} to ${DUMP_PATH}"
$(mysqldump -u$DB_USER -p$DB_PASS $DB | gzip > $DUMP_PATH)
echo "done"