当前位置: 技术问答>linux和unix
SHELL脚本 求助
来源: 互联网 发布时间:2017-04-10
本文导语: 本帖最后由 b13272012771 于 2013-05-02 21:38:10 编辑 最近学shell,求助一下,shell脚本如下: ------------------------------脚本开始---------------------------- #设置数据库名,密码,备份路径,日志路径,数据库文件闻之,以及备份方...
------------------------------脚本开始----------------------------
#设置数据库名,密码,备份路径,日志路径,数据库文件闻之,以及备份方式
#默认的备份方式是tar ,还可以是mysqldump,mysqldoccopy
#默认情况下,用root登录mysql数据库,备份至/root/dbxxxxx.tgz
#!/bin/bash
#This is a ShellScript For Auto DB BackuP
#Powered BY xx
#Setting
DBName=mysql
DBUser=root
DBPasswd=888888
BackupPath=/home/xx/mysql
LogFile=/home/xx/mysql/mysql.log
DBPath=/var/lib/mysql/
#BackupMethod=mysqldump
#BackupMethod=mysqlhotcopy
#BackupMethod=tar
#setting end
NewFile="$BackupPath"db$(date +%y%m%d).tgz
DumpFile="$BackupPath"db$(date +%y%m%d)
OldFile="$BackupPath"db$(date +%y%m%d --date='5 days ago').tgz
echo "----------------------------------------" >>$LogFile
echo $(date +"%y-%m-%d %H:%M:%S")>>$LogFile
echo "----------------------------------------" >>$LogFile
#Delete Old File
if[-f $OldFile ]
then
rm -f $OldFile>>$LogFile 2>&1
echo "[$OldFile] Delete Old File Success!">>$LogFile
else
echo "[$OldFile] No Old File !">>$LogFile
fi
if[-f $NewFile]
then
echo "[$NewFile] The Back File Is exists,Cann't Backup">>$LogFile
else
case $BackupMethod in
mysqldump)
if[-z $DBPasswd]
then
mysqldump -u $DBUser --opt $DBName > $DumpFile
else
mysqldump -u $DBUser -p $DBPasswd -$DBName > $DumpFile
fi
tar czvf $NewFile $DumpFile >> $LogFile 2>&1
echo "[$NewFile] Back up Success !" >> $LogFile
rm -rf $DumpFile
;;
mysqlhotcopy)
rm -rf $DumpFIle
mkdir $DumpFile
if[-z $DBPasswd]
then
mysqlhotcopy -u $DBUser $DBName $DumpFIle >> $LogFile 2>&1
else
mysqlhotcopy -u $DBUser -p $DBPasswd $DBName $DumpDFile >> $LogFile 2>&1
fi
tar czvf $NewFile $DumpFile >> $LogFile 2>&1
echo "[$NewFile] Back up Sucess !" >> $LogFile
rm -rf $DUmpFile
;;
*)
/etc/nint.d/mysqld stop > /dev/null 2>&1
tar czvf $NewFile $DBPath$DBName >> $LogFile 2>&1
;;
esac
fi
echo "----------------------------------------" >> $LogFile
---------------------------------脚本结束----------------------
报错信息如下:
./mysql_backup.sh: 行 28: if[-f /home/xx/mysqldb130427.tgz ]: 没有那个文件或目录
./mysql_backup.sh: 行 29: 未预期的符号 `then' 附近有语法错误
./mysql_backup.sh: 行 29: `then'
求助~~
|
if[-f $NewFile]
像这种判断,都有添加空格,改为if [ -f $NewFile ]
下面还有几个if判断,都加上空格
像这种判断,都有添加空格,改为if [ -f $NewFile ]
下面还有几个if判断,都加上空格
|
BackupPath=/home/xx/mysql
LogFile=/home/xx/mysql/mysql.log
DBPath=/var/lib/mysql/
LogFile=/home/xx/mysql/mysql.log
DBPath=/var/lib/mysql/
|
检查一下目录存不存在。
|
if ....; then顺便帮你找了个参考:http://www.worlduc.com/blog2012.aspx?bid=3216739
....
fi