linux下自动备份MySQL数据并上传到FTP上的shell脚本
程序员文章站
2023-11-02 13:28:46
复制代码 代码如下: #!/bin/bash #mysql autobackup shell # # #----------------set the mysql logi...
复制代码 代码如下:
#!/bin/bash
#mysql autobackup shell
#
#
#----------------set the mysql login parameters
dbuser=root
dbpasswd=
dbserver=localhost
dbname=fwserver2008
dbopt=--opt
backupdir=/dbbackup/
#-----------------set the ftp paramters , 0 no send to a ftp server,1 send to a ftp server
copytoftp=1
ftpserver=172.16.25.2
ftpuser=linux
ftppasswd=123456
#----------------set the backpfile paramters
fileprefix=fwserver
dumpfilename=$backupdir$fileprefix`date +%f_%h%m%s`.sql
newfile=$fileprefix-`date +%f_%h%m%s`.tar.gz
keepdays=10
#------------------write the operater command to log file
logfile=/var/log/_mysqlbackup.log
logtmp=/var/log/_mybackup.tmp
#===============================================
if [ ! -d $backupdir ]
then
echo "$backupdir is not exist, then make ..." >> $logfile
mkdir -p $backupdir
fi
echo "start====================================>">>$logfile
echo "beginning backup `date +%f %t`" >>$logfile
echo "delete $keepdays days ago files ..." >>$logfile
find $backupdir -name $fileprefix* -mtime +$keepdays -fls $logtmp -exec rm {} ;
echo "deleted backup file is :">>$logfile
cat $logtmp >>$logfile
echo "delete old file success!" >>$logfile
if [ -f $backupdir$newfile ]
then
echo "$newfile backup exist, backup stop ..." >>$logfile
else
if [ -z $dbpasswd ]
then
mysqldump -u$dbuser -h$dbserver $dbopt $dbname >$dumpfilename
else
mysqldump -u$dbuser -p$dbpasswd -h$dbserver $dbopt $dbname >$dumpfilename
fi
tar czvf $backupdir$newfile $dumpfilename >>$logfile 2>&1
echo "$backupdir$newfile backup success!" >>$logfile
rm -fr $dumpfilename
if [ $copytoftp = 1 ]; then
if [ -z $ftpserver ];then
echo "ftp server not set,copy to ftp failed ..." >>$logfile
exit 1
elif [ -z $ftpuser ];then
echo "ftp user not set, copy to ftp failed ..." >>$logfile
exit 2
elif [ -z $ftppasswd ]; then
echo "ftp password not set, copy to ftp failed ..." >>$logfile
exit 3
else
echo "start copy to ftp server ...." >> $logfile
ftp -i -n <<end_ftp
open $ftpserver
user $ftpuser $ftppasswd
lcd $backupdir
hash
prompt
put $newfile
close
bye
end_ftp
fi
fi
echo "end=======================================">>$logfile
fi
# the end of the auto backup script
上一篇: 茼蒿和松仁会摩擦出怎样的火花
下一篇: 官网建设和制作时 从这几个方面着手更有效
推荐阅读
-
CentOS中使用Shell脚本实现每天自动备份网站文件和数据库并上传到FTP中
-
linux下自动备份MySQL数据并上传到FTP上的shell脚本
-
Linux下使用crontab调用shell中的scp自动备份mysql数据
-
linux下Vps自动备份web和mysql数据库的脚本
-
CentOS中使用Shell脚本实现每天自动备份网站文件和数据库并上传到FTP中
-
linux下自动备份MySQL数据并上传到FTP上的shell脚本
-
Linux下自动备份MySQL数据库并上传到远程FTP服务器且删除指定日_MySQL
-
Linux下自动备份MySQL数据库并上传到远程FTP服务器且删除指定日_MySQL
-
Linux下使用crontab调用shell中的scp自动备份mysql数据