-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmysqlbackup.sh
executable file
·56 lines (42 loc) · 1.03 KB
/
mysqlbackup.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
52
53
54
55
56
#!/bin/bash
#
# Setup backup location
DEST="/home/hello/Desktop/test/"
#
# Should use key management and dynamic key like Vault
USERNAME='testuser'
PASSWORD='Nono1234$$'
DBNAME='localhost'
#
# check if destination exist, if not create it and set permission
#
if [ ! -d "$DEST" ]; then
echo "Creating backup directory"
mkdir -p "$DEST"
chmod 744 "$DEST"
fi
#
# Creating backup name with absolute path
#
CURRENTDATE=$(date +"DATE%Y%m%d-TIME%H%M%S")
BCKUPNAME="$DEST$CURRENTDATE.sql"
#
# check Database connection
#
if [ ! -n 'mysql --host=$DBNAME --user=$USERNAME --password=$PASSWORD' ]
then
#
# Should change to email and alert Administrator, but okay for now.
# Since
echo "Abort: Cannot connect to database"
else
#
# Backup all databases in MySQL, archive, compress and clean up
#
mysqldump --all-databases --host=$DBNAME --user=$USERNAME --password=$PASSWORD > $BCKUPNAME
tar -czvf "$DEST$CURRENTDATE.tar.gz" -P $BCKUPNAME
rm -f $BCKUPNAME
fi
# transfer file to Archive
# Archieve, encrypt,
# mysql -h localhost -u root -p