-
Notifications
You must be signed in to change notification settings - Fork 0
/
db_backup.sh
33 lines (25 loc) · 1009 Bytes
/
db_backup.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
#!/bin/sh
# mysql database backup script.
#----------------------------------------------------
# (1) set up all the mysqldump variables
PATH=/data/backups/db/
DBSERVER=127.0.0.1
USER=root
PASS=XXXXXX
for DATABASE in db_name1 db_name2
do
FILE=${PATH}${DATABASE}.sql.`/bin/date +"%Y%m%d"`
# (2) in case you run this more than once a day, remove the previous version of the file
/bin/rm ${FILE} 2> /dev/null
/bin/rm ${FILE}.gz 2> /dev/null
# (3) do the mysql database backup (dump)
# use this command for a database server on a separate host:
#/usr/bin/mysqldump --opt --protocol=TCP --user=${USER} --password=${PASS} --host=${DBSERVER} ${DATABASE} > ${FILE}
# use this command for a database server on localhost. add other options if need be.
/usr/bin/mysqldump --opt --user=${USER} --password=${PASS} ${DATABASE} > ${FILE}
# (4) gzip the mysql database dump file
/bin/gzip $FILE
# (5) show the user the result
echo "${FILE}.gz was created:"
/bin/ls -l ${FILE}.gz
done