使用if条件语句编写MySQL备份脚本

#!/bin/bash
#备份数据库

dir=/usr/share/nginx/html/project/ssh/backup/`date +%Y%m%d`
mysqldb=test
musqluser=root
mysqlpwd=123456
mysqlicmd=/usr/bin/mysqldump

if [ $UID -ne 0 ];then
    sudo su
fi

if [ ! -d $dir ];then
    mkdir -p $dir
    echo "create $dir now"
else 
    echo "$dir exists"
fi

$mysqlicmd -u$musqluser -p$mysqlpwd -d $mysqldb >$dir/$mysqldb.sql

if [ $? -ef 0 ];then
    echo -e "\033[32mBackup Successfully!\033[0m"
else
    echo -e "\033[32mBackup failded!\033[0m"
fi

相关推荐