sh 用于备份所有mysql数据库的脚本

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了sh 用于备份所有mysql数据库的脚本相关的知识,希望对你有一定的参考价值。

#!/bin/sh
# System + MySQL backup script
# Copyright (c) 2008 Marchost
# This script is licensed under GNU GPL version 2.0 or above
# ---------------------------------------------------------------------

# Taken from : https://www.howtoforge.com/shell-script-to-back-up-all-mysql-databases-each-table-in-an-individual-file-and-upload-to-remote-ftp

#########################
######TO BE MODIFIED#####

### System Setup ###
BACKUP="Mysql_backup"

### MySQL Setup ###
MUSER="root"
MPASS=""
MHOST="localhost"

### FTP server Setup ###
FTPD="YOUR_FTP_BACKUP_DIR"
FTPU="YOUR_FTP_USER"
FTPP="YOUR_FTP_USER_PASSWORD"
FTPS="YOUR_FTP_SERVER_ADDRESS"

######DO NOT MAKE MODIFICATION BELOW#####
#########################################

### Binaries ###
TAR="$(which tar)"
GZIP="$(which gzip)"
FTP="$(which ftp)"
MYSQL="$(which mysql)"
MYSQLDUMP="$(which mysqldump)"

### Today + hour in 24h format ###
NOW=$(date +"%d%H")

### Create hourly dir ###

mkdir $BACKUP/$NOW

### Get all databases name ###
DBS="$($MYSQL -u $MUSER -h $MHOST -p$MPASS -Bse 'show databases')"
for db in $DBS
	do

### Create dir for each databases, backup tables in individual files ###
  mkdir $BACKUP/$NOW/$db

    for i in `echo "show tables" | $MYSQL -u $MUSER -h $MHOST -p$MPASS $db|grep -v Tables_in_`;
	  do
		      FILE=$BACKUP/$NOW/$db/$i.sql.gz
			      echo $i; $MYSQLDUMP --add-drop-table --allow-keywords -q -c -u $MUSER -h $MHOST -p$MPASS $db $i | $GZIP -9 > $FILE
				    done
					done

### Compress all tables in one nice file to upload ###

ARCHIVE=$BACKUP/$NOW.tar.gz
ARCHIVED=$BACKUP/$NOW

$TAR -cvf $ARCHIVE $ARCHIVED

以上是关于sh 用于备份所有mysql数据库的脚本的主要内容,如果未能解决你的问题,请参考以下文章

sh 用于备份所有mysql数据库的脚本

sh 用于备份所有mysql数据库的脚本

sh Bash脚本备份所有MySQL数据库

sh 用于将所有docker镜像备份到文件的bash脚本

sh 用于备份组织的所有GitHub存储库的Shell脚本

mysql单表备份和恢复