linux 备份mysql并上传_Linux下Mysql自动备份并上传到远程FTP服务器

如果大家还有什么更好的需求和建议请留言。

#!/bin/bash

#Mysql autobackup shell

#write by loulancn#163.com

#-------------------数据库相关的用户名、密码、需要备份的数据库名、备份目录等

dbuser=root

dbpasswd=

dbserver=localhost

dbname=dcradius

dbopt=--opt

backupdir=/dcbackup/

#-------------------是否开启FTP远程备份,0为否,1为是。

copytoftp=0

ftpserver=

ftpuser=

ftppasswd=

#-------------------以下参数

fileprefix=dcradius

dumpfilename=$backupdir`date +%F`.sql

newfile=$fileprefix-`date +%F`.tar.gz

keepdays=10

#-------------------以下为备份时的日志

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 -n<

open $ftpserver

user $ftpuser $ftppasswd

lcd $backupdir

hash

prompt

put $newfile

close

bye

eof

fi

fi

echo "End=======================================">>$logfile

fi

阅读(475) | 评论(0) | 转发(0) |

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值