-
Notifications
You must be signed in to change notification settings - Fork 77
/
Copy pathbackup-database.sh
executable file
·51 lines (38 loc) · 1.32 KB
/
backup-database.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
#!/bin/sh
# @author Ryan McIntyre
# @todo
# option for all database
# e.g.
# mysqldump -u root --all-databases | gzip -9 > /c/Users/Ryan/db_backups/ALL_DBS_$(date +%d-%m[%b]-%Y-%H_%M_%S).sql.gz
MYSQL_DUMP="/opt/lampp/bin/mysqldump"
echo "Enter Database name: "
read -r DBNAME
echo "Database: $DBNAME will be backed up"
echo "Enter backup tag description (<ENTER> for default): "
read -r POSTFIX_NAME
if [ "$POSTFIX_NAME" != "" ]; then
echo 'Backup tag description given: ' "$POSTFIX_NAME"
POSTFIX_NAME="_"$POSTFIX_NAME
else
# no arguments
echo 'No backup tag description given'
POSTFIX_NAME=""
fi
SAVE_PATH="/home/ryan/db_backups/${DBNAME}_$(date +%d-%m[%b]-%Y-%H_%M_%S)$POSTFIX_NAME.sql.gz"
echo -n "Backing up to: $SAVE_PATH"
# To run a process in the background, include an & (an ampersand) at the end of the command you use to run the job
$MYSQL_DUMP -u root $DBNAME | gzip -9 > $SAVE_PATH &
# $! = last pid
mysqlpid=$!
#while ps | grep " $mysqlpid " > /dev/null
while kill -0 $mysqlpid > /dev/null # only check process is alive, doesn't kill
do
echo -n ". "
sleep 2
done
echo -n "Complete"
echo -n "File saved to: " $SAVE_PATH
exit 0
# sources:
# http://stackoverflow.com/questions/1570262/shell-get-exit-code-of-background-process
# http://stackoverflow.com/questions/226703/how-do-i-prompt-for-input-in-a-linux-shell-script