首页 > 代码库 > 一个手动备份MySQL数据库的脚本

一个手动备份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=http://www.mamicode.com/2 >> $backupdir/$backupname "---------backup OK-------"
        echo "---------backup stop-------"
     else
        echo "-------back lose--------"
        exit
     fi
  fi

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

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