我想保留所有MySQL数据库的备份。我有100多个MySQL数据库。我想同时导出所有这些文件,然后再一次将它们全部导入我的MySQL服务器。我该怎么做呢?


当前回答

基于这些答案,我制作了脚本,将所有数据库备份到单独的文件中,然后将它们压缩到一个以日期为名称的存档中。

这个不会要求密码,可以在cron中使用。要将密码存储在。my.cnf中,请检查这个答案https://serverfault.com/a/143587/62749

还为那些不太熟悉bash脚本的人提供了注释。

#!/bin/bash

# This script will backup all mysql databases into 
# compressed file named after date, ie: /var/backup/mysql/2016-07-13.tar.bz2

# Setup variables used later

# Create date suffix with "F"ull date format
suffix=$(date +%F)
# Retrieve all database names except information schemas. Use sudo here to skip root password.
dbs=$(sudo mysql --defaults-extra-file=/root/.my.cnf --batch --skip-column-names -e "SHOW DATABASES;" | grep -E -v "(information|performance)_schema")
# Create temporary directory with "-d" option
tmp=$(mktemp -d)
# Set output dir here. /var/backups/ is used by system, 
# so intentionally used /var/backup/ for user backups.
outDir="/var/backup/mysql"
# Create output file name
out="$outDir/$suffix.tar.bz2"

# Actual script

# Check if output directory exists
if [ ! -d "$outDir" ];then
  # Create directory with parent ("-p" option) directories
  sudo mkdir -p "$outDir"
fi

# Loop through all databases
for db in $dbs; do
  # Dump database to temporary directory with file name same as database name + sql suffix
  sudo mysqldump --defaults-extra-file=/root/.my.cnf --databases "$db" > "$tmp/$db.sql"
done

# Go to tmp dir
cd $tmp

# Compress all dumps with bz2, discard any output to /dev/null
sudo tar -jcf "$out" * > "/dev/null"

# Cleanup
cd "/tmp/"
sudo rm -rf "$tmp"

其他回答

基于这些答案,我制作了脚本,将所有数据库备份到单独的文件中,然后将它们压缩到一个以日期为名称的存档中。

这个不会要求密码,可以在cron中使用。要将密码存储在。my.cnf中,请检查这个答案https://serverfault.com/a/143587/62749

还为那些不太熟悉bash脚本的人提供了注释。

#!/bin/bash

# This script will backup all mysql databases into 
# compressed file named after date, ie: /var/backup/mysql/2016-07-13.tar.bz2

# Setup variables used later

# Create date suffix with "F"ull date format
suffix=$(date +%F)
# Retrieve all database names except information schemas. Use sudo here to skip root password.
dbs=$(sudo mysql --defaults-extra-file=/root/.my.cnf --batch --skip-column-names -e "SHOW DATABASES;" | grep -E -v "(information|performance)_schema")
# Create temporary directory with "-d" option
tmp=$(mktemp -d)
# Set output dir here. /var/backups/ is used by system, 
# so intentionally used /var/backup/ for user backups.
outDir="/var/backup/mysql"
# Create output file name
out="$outDir/$suffix.tar.bz2"

# Actual script

# Check if output directory exists
if [ ! -d "$outDir" ];then
  # Create directory with parent ("-p" option) directories
  sudo mkdir -p "$outDir"
fi

# Loop through all databases
for db in $dbs; do
  # Dump database to temporary directory with file name same as database name + sql suffix
  sudo mysqldump --defaults-extra-file=/root/.my.cnf --databases "$db" > "$tmp/$db.sql"
done

# Go to tmp dir
cd $tmp

# Compress all dumps with bz2, discard any output to /dev/null
sudo tar -jcf "$out" * > "/dev/null"

# Cleanup
cd "/tmp/"
sudo rm -rf "$tmp"

mysqldump -uroot - root——所有数据库> allDB.sql

注意:-u"你的用户名" - p“密码”

我写这篇评论已经4年多了,现在决定把它作为一个答案。

jruzafa的脚本可以简化一点:

#!/bin/bash

USER="zend"
PASSWORD=""
ExcludeDatabases="Database|information_schema|performance_schema|mysql"
databases=`mysql -u $USER -p$PASSWORD -e "SHOW DATABASES;" | tr -d "| " | egrep -v $ExcludeDatabases`

for db in $databases; do
    echo "Dumping database: $db"
    mysqldump -u $USER -p$PASSWORD --databases $db > `date +%Y%m%d`.$db.sql
done

注意:

排除的数据库(通常是系统表)在变量ExcludeDatabases中提供 请注意,密码是在命令行中提供的。这被认为是不安全的。研究这个问题。

当从和导入到不同的MySQL版本时要小心,因为MySQL表可能有不同的列。 如果运气不好,授予特权可能无法工作。 我创建了这个脚本(mysql_export_grants。SQL)来转储补助金,以便导入到新数据库中,以防万一:

#!/bin/sh
stty -echo
printf 'Password: ' >&2
read PASSWORD
stty echo
printf "\n"
if [ -z "$PASSWORD" ]; then
        echo 'No password given!'
        exit 1
fi
MYSQL_CONN="-uroot -p$PASSWORD"
mysql ${MYSQL_CONN} --skip-column-names -A -e"SELECT CONCAT('SHOW GRANTS FOR ''',user,'''@''',host,''';') FROM mysql.user WHERE user<>''" | mysql ${MYSQL_CONN} --skip-column-names -A | sed 's/$/;/g'

导出Ubuntu中的所有数据库

1 - mysqldump -u root -p——databases database1 database2 > ~/Desktop/databases_1_2.sql

OR

2 - mysqldump -u root -p——all_databases > ~/Desktop/all_databases.sql