Home > Database > Mysql Tutorial > body text

自动备份MYSQL的linux脚本_MySQL

WBOY
Release: 2016-06-01 13:44:32
Original
888 people have browsed it

bitsCN.com

 

这是我写的一个自动备份MYSQL的 脚本, 能备份所有MYSQL 中的所有数据库。

 

 

 

 

#!/bin/bash

 

umask 177

 

MUSER="userid"

MPASS="password"

MHOST="localhost"

MYSQL="$(which mysql)"

MYSQLDUMP="$(which mysqldump)"

PARAMS="--hex-blob --skip-extended-insert --password=$MPASS"

DATE="$(date +%F)"

BACKUPDIR="/home/${MUSER}/BACKUP/$DATE/"

RMDATE="$(date -d "1 week ago" +%F)"

RMDIR="/home/${MUSER}/BACKUP/$RMDATE"

BACKUPLOG="/home/${MUSER}/BACKUP/backup.log"

 

if [ ! -f $BACKUPLOG ] ; then

touch $BACKUPLOG

chmod 600 $BACKUPLOG

fi

 

if [ -d $RMDIR ]; then

rm -rf $RMDIR

echo "Backup DELETED from" $RMDATE >> $BACKUPLOG

fi

 

if [ ! -d $BACKUPDIR ]; then

mkdir -m 700 -p $BACKUPDIR

fi

 

echo "Backup BEGINNING" $DATE >> $BACKUPLOG

 

DBS="$($MYSQL -h $MHOST -u $MUSER -p$MPASS -Bse 'show databases')"

for db in $DBS

do

$MYSQLDUMP $PARAMS $db > $BACKUPDIR$db.sql && bzip2 --best $BACKUPDIR$db.sql

done

 

echo "Backup COMPLETED" $DATE >> $BACKUPLOG

 

另外可以结合CRONJOB定时备份

作者 eimhee

bitsCN.com
Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!