>데이터 베이스 >MySQL 튜토리얼 >Linux下MySQL备份脚本

Linux下MySQL备份脚本

WBOY
WBOY원래의
2016-06-07 17:08:591152검색

Linux下MySQL备份脚本: #!/bin/bash #Mysql autobackup shell#write by tuspark.cn #-------------------数据库相关的用户名、密

Linux下MySQL备份脚本:

#!/bin/bash

#Mysql autobackup shell#write by tuspark.cn

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

dbuser=root

dbpasswd=XXXXX

dbserver=localhost

dbname=XXXXX

dbopt=--opt

backupdir=/dcbackup/

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

copytoftp=1

ftpserver=XXXXX

ftpuser=XXXXX

ftppasswd=XXXXX

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

fileprefix=dcradiusdump

filename=$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>$logfile
fi

linux

성명:
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.