首页 > 代码库 > 备份数据库的shell脚本

备份数据库的shell脚本

#!/bin/bash
#back_mysql by 2016-11-14
bak_dir=/data/backup_db/`date +%Y%m%d`
mysqldb=mysql
mysqluser=*****
mysqlpw=******
if [ $UID -ne 0 ];then
 

   echo "must to be use root for exec shell"
    exit
fi
#判断是否存在文件,\033[32m是显示颜色的
if [ ! -d $bak_dir ];then
    mkdir -p $bak_dir
    echo -e "\033[32mThe $bak_dir create successfully!\033[0m"
else
    echo "This $bak_dir is exists...."
fi
#备份语句
mysqldump -u$mysqluser -p$mysqlpw -d $mysqldb >$bak_dir/$mysqldb.sql.tar.gz
#是否备份成功
if [ $? -eq 0 ];then
    echo -e "\033[32mThe mysql backup $mysqldb successfully\033[0m"
else
    echo -e "\033[32mThe mysql backup $mysqldb failed .please check!\033[0m"
fi
#执行脚本和备份结果
技术分享

技术分享


本文出自 “通通的技术博客” 博客,转载请与作者联系!

备份数据库的shell脚本