วันพฤหัสบดีที่ 16 พฤษภาคม พ.ศ. 2556

Shell script backup all Mysql Databases in Linux

Source: http://4rapiddev.com/mysql/shell-script-backup-all-mysql-databases-in-linux/

This shell script will backup all current Mysql databases, compress(gzip) them and put them in a new folder named by current date (one directory per database).


01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#!/bin/bash
  
PATH=/usr/sbin:/sbin:/bin:/usr/bin
  
MyUSER="your_db_user"
MyPASS="your_db_password"
MyHOST="your_db_host"
SUBFOLDER="$(date +"%Y-%m-%d")"
DEST="/somewhere/on/your/server"
MDB="$DEST/backup/$SUBFOLDER"
if [ ! -d $MDB ]
then
    mkdir -p $MDB >/dev/null 2>&1 && echo "Directory $MDB created." ||  echo "Error: Failed to create $MDB directory."
else
    echo "Error: $MDB directory exits!"
fi
NOW="$(date +"%Y-%m-%d_%H-%M-%S")"
FILE=""
DBS="$(mysql -u $MyUSER -h $MyHOST -p$MyPASS -Bse 'show databases')"
  
for db in $DBS
do
    FILE="$MDB/$db.$NOW.sql.gz"
    mysqldump -u $MyUSER -h $MyHOST -p$MyPASS --complete-insert $db | gzip -9 > $FILE
    echo "Backup $FILE.....DONE"
done

ไม่มีความคิดเห็น:

แสดงความคิดเห็น