返回顶部
首页 > 资讯 > 数据库 >linux下自动备份MySQL数据并上传到FTP上的shell脚本
  • 258
分享到

linux下自动备份MySQL数据并上传到FTP上的shell脚本

脚本自动备份数据 2022-06-04 21:06:14 258人浏览 安东尼
摘要

#!/bin/bash #Mysql autobackup shell # # #----------------set the mysql login parameters dbuser=root dbp


#!/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
您可能感兴趣的文档:

--结束END--

本文标题: linux下自动备份MySQL数据并上传到FTP上的shell脚本

本文链接: https://lsjlt.com/news/18155.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

猜你喜欢
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作