-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdatabase.bash
executable file
·81 lines (67 loc) · 1.77 KB
/
database.bash
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
#!/bin/bash
# database.bash
# Automates working with a database
# v2.1
# Last Updated Dec 1 2015
# Documentation: http://www.nickyeoman.com/blog/db/31-mysql-backup-restore
##
# Set/Get Variables
##
tstamp=$(date +%s) # Date, The "+%s" option to 'date' is GNU-specific.
#Dump or Update?
if [ -z "$1" ]; then
echo -n "What do you want to do?([D]ump or [U]pdate)"
read parm
else
parm=$1
fi
# Database name
if [ -z "$2" ]; then
echo -n "What database are you using?"
read dbname
else
dbname=$2
fi
#Database user
if [ -z "$3" ]; then
echo -n "What database user are you using?"
read dbuser
else
dbuser=$3
fi
#database password
if [ -z "$4" ]; then
echo -n "What database password are you using?"
read dbpass
else
dbpass=$4
fi
#database host (in case not local)
if [ -z "$5" ]; then
echo -n "What is the database host? (usually localhost)"
read dbhost
else
dbhost=$5
fi
##
# Got everything we need, let's begin
##
#Regardless of what we are doing, create a backup of the existing database.
# This is a life saver when you run an update instead of a dump or want to restore to a previous dump
# if your using git, just add sql/backup/ to .gitignore then your dumps will remain local
#check to see if dir exists
if test ! -d "../sql/backup"; then
mkdir ../sql/backup #if not make the directory
fi
#dump the database incase we want to revert
mysqldump -h $dbhost -u $dbuser -p$dbpass $dbname > ../sql/backup/$tstamp.$dbname.sql
#Great, what are we actually doing?
#dump or update the db
if [ "$parm" = d ]; then
mysqldump -h $dbhost -u $dbuser -p$dbpass $dbname > ../sql/$dbname.sql
elif [ "$parm" = u ]; then #update the db
mysql -h $dbhost -u $dbuser -p$dbpass $dbname < ../sql/$dbname.sql
else
echo "d or u options only! (lower case)"
fi
#All Done