一个手动备份MySQL数据库的脚本

#!/bin/bash
username=root
hostname=localhost
password=root
mysql -u$username -h$hostname -p$password -e "show databases"
read -p "PLZ input a dbname:" dbname
read -p "PLZ input backupdir:" backupdir
read -p "PLZ input backupname:" backupname
  if [[ "$dbname" == "quit" || "$backupdir" == "quit" || "$backupname" == "quit" ]];then
       exit 4
  else
   echo "----------backup start----------"
    ! [ -d $backupdir ] && mkdir -p $backupdir
    if [[ "$dbname" == "all" ]];then
       /usr/local/mysql/bin/mysqldump -u$username -h$hostname -p$password --all-databases --lock-all-tables --flush-logs --master-data=2 >> $backupdir/$backupname 
    else
      /usr/local/mysql/bin/mysqldump -u$username -h$hostname -p$password --databases $dbname --lock-all-tables --flush-logs --master-data=2 >> $backupdir/$backupname 
    fi 
    if [ $? -eq 0 ];then
        echo "---------backup OK-------"
        echo "---------backup stop-------"
     else
        echo "-------back lose--------"
        exit
     fi
  fi

  使用脚本时需要先修改脚本内数据库信息,赋予执行权限。

posted @ 2017-02-10 13:22  郭延龙  阅读(347)  评论(0编辑  收藏  举报