Home >Database >Mysql Tutorial >MYSQL代码 定期备份Mysql数据库_MySQL

MYSQL代码 定期备份Mysql数据库_MySQL

WBOY
WBOYOriginal
2016-06-01 13:19:13902browse

bitsCN.com 相信很多个人网站的站长,或者是独立Blog的博主,都有着备份数据库这样一个需求。当然WordPress这样插件非常丰富的Blog系统有好多能帮你完成这项任务的插件。如果您需要自己亲手来备份数据库的话,我贴一个Mysql自动备份脚本供大家参考,在国外网站上看到的,还是不错,实现了定期备份Mysql数据库,并且可以选在在每周的一天做指定目录下文件的全面备份,备份文件自动上传到你指定的FTP上,保证了备份的可靠性。如果只是把数据库备份在和数据库一起,我想意义也不是特别大了。

#!/bin/sh
# System + MySQL backup script
# Full backup day - Sun (rest of the day do incremental backup)
# Copyright (c) 2005-2006 nixCraft
# This script is licensed under GNU GPL version 2.0 or above
# Automatically generated by
# http://bash.cyberciti.biz/backup/wizard-ftp-script.php
# ---------------------------------------------------------------------
### System Setup ###
##备份本件本地目录##
DIRS="/home/xxxx"
BACKUP=/tmp/backup.$$
##日期格式 可以自己调节##
NOW=$(date +"%Y-%m-%d")
INCFILE="/home/xxxx"
DAY=$(date +"%u")
##星期天完全备份 可以自己调节##
FULLBACKUP="7"
### MySQL Setup ###
MUSER="chaos"
MPASS="password"
MHOST="host"
MYSQL="$(which mysql)"
MYSQLDUMP="$(which mysqldump)"
GZIP="$(which gzip)"
### FTP server Setup ###
##增量备份路径##
FTPD="/backup/xxxx/incremental"
FTPU="chaos"
FTPP="password"
FTPS="xxx.xxx.com"
### Other stuff ###
EMAILID="chaos@diablo.net"
### Start Backup for file system ###
[ ! -d $BACKUP ] && mkdir -p $BACKUP || :
### See if we want to make a full backup ###
if [ "$DAY" == "$FULLBACKUP" ]; then
##完全备份路径##
FTPD="/backup/xxxx/full"
FILE="fs-full-$NOW.tar.gz"
tar -zcvf $BACKUP/$FILE $DIRS
else
i=$(date +"%Hh%Mm%Ss")
FILE="fs-i-$NOW-$i.tar.gz"
tar -g $INCFILE -zcvf $BACKUP/$FILE $DIRS
fi
### Start MySQL Backup ###
# Get all databases name
##备份所有数据库 可自行调整 或备份指定数据库##
DBS="$($MYSQL -u $MUSER -h $MHOST -p$MPASS -Bse 'show databases')"
for db in $DBS
do
FILE=$BACKUP/mysql-$db.$NOW-$(date +"%T").gz
$MYSQLDUMP -u $MUSER -h $MHOST -p$MPASS $db | $GZIP -9 > $FILE
done
### Dump backup using FTP ###
#Start FTP backup using lftp
##ubuntu上可以使用ncftp 自行调节##
#ncftp -u"$FTPU" -p"$FTPP" $FTPSlftp -u $FTPU,$FTPP $FTPSmkdir $FTPD
mkdir $FTPD/$NOW
cd $FTPD/$NOW
lcd $BACKUP
mput *
quit
EOF
### Find out if ftp backup failed or not ###
if [ "$?" == "0" ]; then
rm -f $BACKUP/*
else
T=/tmp/backup.fail
echo "Date: $(date)">$T
echo "Hostname: $(hostname)" >>$T
echo "Backup failed" >>$T
mail -s "BACKUP FAILED" "$EMAILID" rm -f $T
fi

改一下里面的一些路径和账号密码等信息就可以了,希望对大家有所用处。bitsCN.com

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn