备份MySQL数据库的Bash脚本

时间:2012-07-28 16:21:21

标签: linux bash shell

我写了一个脚本,我一直收到以下错误。尝试连接时,用户'zul'@'localhost(使用密码:是)拒绝访问

项目:43:[d:未找到

项目:43:[d:未找到

它不是读取输入d,也不是连接到MySQL。有任何想法吗?

这是我的剧本:

#!/bin/bash

tstamp=$(date +$s) 

#Dump or Update
if [ -z "$1" ]; then
  echo -n "What do you want to do? [D]ump or [U]pdate: "
  read parm
else
  parm=$1
fi

#Database Name
if [ -z "$2" ]; then
  echo -n "What database are you using?"
  read dbname
else
  dbname=$2
fi

#Database User
if [ -z "$3" ]; then
  echo -n "What database user are you using?"
  read dbuser
else
  dbuser=$3
fi

#Database Password
if [ -z "$4" ]; then
     echo -n "What password are you using?"
     read dbpass
else
     dbpass=$4
fi


#Checks if the directory exists
if test ! -d "/home/zul/sql/backup"; then
  mkdir /home/zul/sql
  mkdir /home/zul/sql/backup
fi

mysqldump $dbuser -p$dbpass $dbname > /home/zul/sql/backup/$tstamp.$dbname.sql

if [$parm = d ]; then
  mysqldump $dbuser -p$dbpass $dbname > /home/zul/sql/backup/$dbname.sql
elif [$parm = u ]; then
  mysqldump $dbuser -p$dbpass $dbname < /home/zul/sql/backup/$dbname.sql
else
  echo " d or u option only! (lower case)"
fi

2 个答案:

答案 0 :(得分:2)

我猜你的if-else测试中有一个空格:
应该是[ test ]而不是[test ]

if [ $parm = d ]; then
  mysqldump $dbuser -p$dbpass $dbname > /home/zul/sql/backup/$dbname.sql
elif [ $parm = u ]; then
  mysqldump $dbuser -p$dbpass $dbname < /home/zul/sql/backup/$dbname.sql
else

答案 1 :(得分:1)

您必须将'['与其参数分隔为空格:

if [ $parm = d ]; then
    mysqldump $dbuser -p$dbpass $dbname > /home/zul/sql/backup/$dbname.sql
elif [ $parm = u ]; then