简体   繁体   English

Bash 中的 MySQL 备份脚本

[英]MySQL Backup Script in Bash

REF http://www.rsnapshot.org/howto/1.2/rsnapshot-HOWTO.en.html 4.3.9.参考http://www.rsnapshot.org/howto/1.2/rsnapshot-HOWTO.en.html 4.3.9。 backup_script备份脚本

I need to backup ALL the mysql databases by dynamically should new ones be created.如果创建新数据库,我需要动态备份所有 mysql 数据库。 Is there an ideal way to do this in bash with minimal code?有没有一种理想的方法可以用最少的代码在 bash 中做到这一点?

Would I need to log in to mysql and get all the databases?我需要登录到 mysql 并获取所有数据库吗?

Seems that you are wanting a bash script to run backup on dynamic databases that are created in MySQL.似乎您想要一个 bash 脚本在 MySQL 中创建的动态数据库上运行备份。 You can add the mysql root user account information in my.cnf in the root directory or within the bash script under the # [ Define Variables ].您可以在根目录下的my.cnf 或bash 脚本中的#[Define Variables] 下添加mysql root 用户帐户信息。

you will need to chmod the bash script with您将需要使用 chmod bash 脚本

$sudo chmod +x backupmysql.sh

This will allow you to run the script with the following command.这将允许您使用以下命令运行脚本。

$sudo ./backupmysql.sh

You can name the script whatever you like.您可以随意命名脚本。 In this example, I named it backupmysql.sh.在本例中,我将其命名为 backupmysql.sh。

Here is the bash script:这是 bash 脚本:

#!/bin/bash

# [ Define Variables ]
HOST=`hostname -s`
syslogtag=MySQL-Backup
DEST=/var/dba/backup/
DBS="$(mysql -u root -Bse 'show databases' | egrep -v '^Database$|hold$' | grep -v 'performance_schema\|information_schema')"
DATE=$(date +'%F')

#[ Individually dump all databases with date stamps ]
for db in ${DBS[@]};
do  

GZ_FILENAME=$HOST-$db-$DATE.sql.gz
mysqldump -u root --quote-names --opt --single-transaction --quick $db > $DEST$HOST-$db-$DATE.sql
ERR=$?

if [ $ERR != 0 ]; then
NOTIFY_MESSAGE="Error: $ERR, while backing up database: $db"
logger -i -t ${syslogtag} "MySQL Database Backup FAILED; Database: $db"
else
NOTIFY_MESSAGE="Successfully backed up database: $db "
logger -i -t ${syslogtag} "MySQL Database Backup Successful; Database: $db"
fi
echo $NOTIFY_MESSAGE
done

If you have large files for backup, you can replace the statement in the bash script for the mysqldump to compress the file using gzip.如果你有大文件要备份,你可以将mysqldump的bash脚本中的语句替换为gzip压缩文件。

mysqldump -u root --quote-names --opt --single-transaction --quick $db | gzip -cf > $DEST$HOST-$db-$DATE.sql.gz

you can use gunzip to uncompress the file.您可以使用 gunzip 解压缩文件。

The mysqldump command has an --all-databases option to back up every single database in one pass. mysqldump命令有一个--all-databases选项,可以一次性备份每个数据库。

The only down-side to this is you have to restore them all together, you don't have the luxury of picking and choosing.唯一的缺点是你必须一起恢复它们,你没有挑选和选择的奢侈。

Keep in mind that databases usually have an associated directory in your MySQL data directory, so you can always iterate through those to find out which databases exist.请记住,数据库通常在您的 MySQL 数据目录中有一个关联目录,因此您始终可以遍历这些目录以找出存在哪些数据库。

For mysql local system process I use:对于 mysql 本地系统进程,我使用:

#!/bin/bash

DEFAULTSFILE=/etc/my.cnf.d/.backups.cnf
BACKUPDIR=/root/db_backups
SAVEIFS=$IFS

# get list of dbs with field name (Database)

DBLIST=$(mysql -e "show databases" | cut -d ' '  -f 2 | grep -v Database)
IFS=$'\n'
DBLIST=($DBLIST)

# create backups directory if it doesn't exist

[ -d $BACKUPDIR ] || mkdir -p $BACKUPDIR

# make a single dump file for all databases in case entire server needs to be restored:

echo "Dumping all databases..."
mysqldump --defaults-file=$DEFAULTSFILE --all-databases > $BACKUP_DIR/$(date +%Y%m%d).all.sql
echo "Done dumping all databases."

# backup each database into it's own file to facilitate discrete restoration:

for (( i=0; i<${#DBLIST[@]}; i++ ))
do
  echo "starting dump of  ${DBLIST[$i]}..."
  mysqldump --defaults-file=$DEFAULTSFILE ${DBLIST[$i]} --skip-lock-tables > $BACKUPDIR/$(date +%Y%m%d).${DBLIST[$i]}.sql
  echo "done dumping ${DBLIST[$i]}."
done
IFS=$SAVEIFS

# gzip the sql files in-place to save space

gzip -f $BACKUPDIR/*.sql

# delete sql files older than 7 days

find $BACKUPDIR -type f -mtime +7 -name '*.gz' -execdir rm -- '{}' \;

For docker things are slightly different:对于 docker,事情略有不同:

#!/bin/bash
DEFAULTSFILE=/etc/my.cnf.d/.backups.cnf
BACKUPDIR=/root/db_backups
SAVEIFS=$IFS

# get list of dbs with field name (Database) and information_schema excluded. information_schema hangs during dumps

DBLIST=$(docker exec mysql mysql --defaults-file=/etc/my.cnf.d/.backups.cnf -e "show databases" | cut -d ' '  -f 2 | grep -v Database)
IFS=$'\n'
DBLIST=($DBLIST)

# create backups directory if it doesn't exist

[ -d $BACKUPDIR ] || mkdir -p $BACKUPDIR

# backup each database into it's own file to facilitate discrete restoration:

for (( i=0; i<${#DBLIST[@]}; i++ ))
do
  echo "starting dump of  ${DBLIST[$i]}..."
  docker exec mysql mysqldump --defaults-file=$DEFAULTSFILE ${DBLIST[$i]} --skip-lock-tables > $BACKUPDIR/$(date +%Y%m%d).${DBLIST[$i]}.sql
  echo "done dumping ${DBLIST[$i]}."
done
IFS=$SAVEIFS

# gzip the sql files in-place to save space

gzip -f $BACKUPDIR/*.sql

# delete sql files older than 7 days

find $BACKUPDIR -type f -mtime +7 -name '*.gz' -execdir rm -- '{}' \;

I haven't been able to get --all-databases working for docker.我一直无法让 --all-databases 为 docker 工作。 I don't like all-in-one backups anyway...反正我不喜欢多合一备份...

I threw these directly into root's crontab and they work fine...我将这些直接扔到 root 的 crontab 中,它们工作正常......

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM