shell脚本(三)

  1. 自动备份数据库脚本


#!/bin/bash
#auto bakcup mysql
#by 2015-7-2

BAK_DIR="/data/backup/`date +%Y%m%d`"
MYSQL_DB=discuz
MYSQL_USER=root
MYSQL_PW=123456
MYSQL_CMD=/usr/bin/mysqldump

if [ $UID -ne 0 ]; then

echo "Must to be use root for run shell.."


        exit
fi

if [ ! -d "$BAK_DAR" ]; then

        mkdir -p $BAK_DIR

        echo -e "\033[32mthe $BAK_DIR is create successfully\033[0m"

else
        echo "this $BAK_DIR is exists......"

fi
#mysql backup commend
$MYSQL_CMD -u$MYSQL_USER -p$MYSQL_PW  $MYSQL_DB>$BAK_DIR/$MYSQL_DB.sql

if [ $? -eq 0 ] ; then


        echo -e "\033[32mthe mysql $MYSQL_DB is backup successfully\033[0m"
else
        echo -e "\033[32mthe mysql $MYSQL_DB is backup failed,please check\033[0m"

fi


你可能感兴趣的:(mysql自动备份脚本)