The MYSQL code backs up the Mysql database periodically

  • 2020-05-07 20:34:59
  • OfStack

Believe the webmaster of a lot of individual website, or the blogger that is independent Blog, have backup database so 1 demand. Of course, the very rich Blog system has many plugins that can help you do this. If you need to backup the database by yourself, I posted a Mysql automatic backup script for your reference, in foreign web site, or good, implements the backup Mysql database regularly, and can choose the file directory specified in the 1 day of a week to do the full backup, backup files are automatically uploaded to your designated FTP, ensure the reliability of the backup. I don't think it would make too much sense if I just backed up the database from database 1.

#!/bin/sh 
# System + MySQL backup script 
# Full backup day - Sun (rest of the day do incremental backup) 
# Copyright (c) 2005-2006 nixCraft <http://www.cyberciti.biz/fb/> 
# 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 ### 
## Back up this local directory ## 
DIRS="/home/xxxx" 
BACKUP=/tmp/backup.$$ 
## The date format   You can adjust yourself ## 
NOW=$(date +"%Y-%m-%d") 
INCFILE="/home/xxxx" 
DAY=$(date +"%u") 
## Sunday full backup   You can adjust yourself ## 
FULLBACKUP="7" 
### MySQL Setup ### 
MUSER="chaos" 
MPASS="password" 
MHOST="host" 
MYSQL="$(which mysql)" 
MYSQLDUMP="$(which mysqldump)" 
GZIP="$(which gzip)" 
### FTP server Setup ### 
## Incremental backup path ## 
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 
## Full backup path ## 
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 
## Backup all databases   self-adjusting   Or backup the specified database ## 
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 Top available ncftp  Adjust themselves ## 
#ncftp -u"$FTPU" -p"$FTPP" $FTPS<<EOF 
lftp -u $FTPU,$FTPP $FTPS<<EOF 
mkdir $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" <$T 
rm -f $T 
fi 

Change 1 below 1 some path and account password and other information can be, hope to be useful to you.

Related articles: