-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmybackup
executable file
·96 lines (78 loc) · 1.65 KB
/
mybackup
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
#!/bin/bash
set -euo pipefail
IFS=$'\n\t '
if [[ $# -eq 0 ]]; then
cat << EOL
$0 missed arguments
-h hostname
-p interactive password
-P password
-u username
-v verbose
-d database
EOL
exit 1
fi
USER=root
PASSWORD=
HOST=localhost
DATABASE=
VERBOSE=
POSITIONAL=()
while [[ $# -gt 0 ]]
do
case "$1" in
-u|--user)
USER="$2"
shift
;;
-h|--host)
HOST="$2"
shift
;;
-p)
echo "Please, input password"
read -s PASSWORD
;;
-d|--database)
DATABASE="$2"
shift
;;
-v)
VERBOSE=YES
;;
-P|--password)
PASSWORD="$2"
shift
;;
*) # unknown option
POSITIONAL+=("$1") # save it in an array for later
;;
esac
shift
done
backup_db() {
#hostname user password database
local HOST=$1
local USER=$2
local PASSWORD=$3
local DATABASE=$4
local VERBOSE=$5
[ -z $DATABASE ] && echo "missing database" && return 1
[ -z $USER ] && echo "missing user" && return 1
[ -z $HOST ] && echo "missing host" && return 1
[ -n "$VERBOSE" ] && echo 'start backuping $DATABASE'
[ ! -d $DATABASE ] && mkdir $DATABASE
tables=`echo 'SHOW TABLES;' | mysql -u $USER --password=$PASSWORD $DATABASE`
tables=( $tables )
for table in ${tables[@]:1}; do
[ -n "$VERBOSE" ] && echo "Exporting $table..."
mysqldump -u $USER --password=$PASSWORD $DATABASE $table > $DATABASE/$table.sql
done
NAME=$DATABASE-`date +%Y-%m-%d`.tar.gz
[ -n "$VERBOSE" ] && echo 'Start compression...'
tar cfz $NAME $DATABASE
rm -rf ./$DATABASE
[ -n "$VERBOSE" ] && echo 'Done!'
}
backup_db "$HOST" "$USER" "$PASSWORD" "$DATABASE" "$VERBOSE"