自动将所有mysql表转储到单独的文件中?

时间:2010-09-08 15:06:08

标签: mysqldump

我想将每个mysql表转储到单独的文件中。手册指出其语法为

mysqldump [options] db_name [tbl_name ...]

表示您事先知道了表名。我现在可以设置知道每个表名的脚本,但是说我在路上添加一个新表并忘记更新转储脚本。然后我错过了一个或多个表的转储。

有没有办法将每个现有表自动转储到单独的文件中?或者我将不得不做一些剧本;查询数据库,获取所有表名,并按名称转储它们。

如果我使用script-fu路由,哪些脚本语言可以访问mysql数据库?

10 个答案:

答案 0 :(得分:57)

mysqldump命令行程序为您完成此操作 - 虽然the docs对此非常不清楚。

需要注意的一点是〜/ output / dir必须由拥有mysqld的用户写入。在Mac OS X上:

sudo chown -R _mysqld:_mysqld ~/output/dir
mysqldump --user=dbuser --password --tab=~/output/dir dbname

运行上面的内容后,您将拥有一个tablename.sql文件,其中包含每个表的架构(create table statement)和包含数据的tablename.txt文件。

如果只想使用模式转储,请添加--no-data标志:

mysqldump --user=dbuser --password --no-data --tab=~/output/dir dbname

答案 1 :(得分:57)

这是一个脚本,它将表数据作为SQL命令转储到单独的压缩文件中。它不需要在MySQL服务器主机上,也不需要在脚本中硬编码密码,而只是针对特定的数据库,而不是服务器上的所有数据库:

#!/bin/bash

# dump-tables-mysql.sh
# Descr: Dump MySQL table data into separate SQL files for a specified database.
# Usage: Run without args for usage info.
# Author: @Trutane
# Ref: http://stackoverflow.com/q/3669121/138325
# Notes:
#  * Script will prompt for password for db access.
#  * Output files are compressed and saved in the current working dir, unless DIR is
#    specified on command-line.

[ $# -lt 3 ] && echo "Usage: $(basename $0) <DB_HOST> <DB_USER> <DB_NAME> [<DIR>]" && exit 1

DB_host=$1
DB_user=$2
DB=$3
DIR=$4

[ -n "$DIR" ] || DIR=.
test -d $DIR || mkdir -p $DIR

echo -n "DB password: "
read -s DB_pass
echo
echo "Dumping tables into separate SQL command files for database '$DB' into dir=$DIR"

tbl_count=0

for t in $(mysql -NBA -h $DB_host -u $DB_user -p$DB_pass -D $DB -e 'show tables') 
do 
    echo "DUMPING TABLE: $DB.$t"
    mysqldump -h $DB_host -u $DB_user -p$DB_pass $DB $t | gzip > $DIR/$DB.$t.sql.gz
    tbl_count=$(( tbl_count + 1 ))
done

echo "$tbl_count tables dumped from database '$DB' into dir=$DIR"

答案 2 :(得分:17)

您可以通过以下方式完成此任务:

  1. 获取mysql中的数据库列表
  2. 使用mysqldump
  3. 转储每个数据库
    # Optional variables for a backup script
    MYSQL_USER="root"
    MYSQL_PASS="something"
    BACKUP_DIR=/srv/backup/$(date +%Y-%m-%dT%H_%M_%S);
    test -d "$BACKUP_DIR" || mkdir -p "$BACKUP_DIR"
    # Get the database list, exclude information_schema
    for db in $(mysql -B -s -u $MYSQL_USER --password=$MYSQL_PASS -e 'show databases' | grep -v information_schema)
    do
      # dump each database in a separate file
      mysqldump -u $MYSQL_USER --password=$MYSQL_PASS "$db" | gzip > "$BACKUP_DIR/$db.sql.gz"
    done
    

答案 3 :(得分:5)

这是相应的导入。

#!/bin/bash

# import-files-mysql.sh
# Descr: Import separate SQL files for a specified database.
# Usage: Run without args for usage info.
# Author: Will Rubel
# Notes:
#  * Script will prompt for password for db access.

[ $# -lt 3 ] && echo "Usage: $(basename $0) <DB_HOST> <DB_USER> <DB_NAME> [<DIR>]" && exit 1

DB_host=$1
DB_user=$2
DB=$3
DIR=$4

DIR=$DIR/*


echo -n "DB password: "
read -s DB_pass
echo
echo "Importing separate SQL command files for database '$DB' into '$DB'"

file_count=0


for f in $DIR

do 
    echo "IMPORTING FILE: $f"

    gunzip -c $f | mysql -h $DB_host -u $DB_user -p$DB_pass $DB

    (( file_count++ ))
done

echo "$file_count files importing to database '$DB'"

答案 4 :(得分:2)

#!/bin/bash

for i in $(mysql -uUser -pPASSWORD DATABASE -e "show tables;"|grep -v Tables_in_);do mysqldump -uUSER -pPASSWORD DATABASE $i > /backup/dir/$i".sql";done

tar -cjf "backup_mysql_"$(date +'%Y%m%d')".tar.bz2" /backup/dir/*.sql

答案 5 :(得分:1)

看起来这里的每个人都忘记autocommit=0;SET unique_checks=0;SET foreign_key_checks=0;这是为了加快导入过程......

#!/bin/bash
MYSQL_USER="USER"
MYSQL_PASS="PASS"

if [ -z "$1" ]
  then
    echo "Dumping all DB ... in separate files"
    for I in $(mysql -u $MYSQL_USER --password=$MYSQL_PASS -e 'show databases' -s --skip-column-names); 
    do 
      echo "SET autocommit=0;SET unique_checks=0;SET foreign_key_checks=0;" > "$I.sql"
      mysqldump -u $MYSQL_USER --password=$MYSQL_PASS $I >> "$I.sql"; 
      echo "SET autocommit=1;SET unique_checks=1;SET foreign_key_checks=1;commit;" >> "$I.sql"
      gzip "$I.sql"
    done
    echo "END."
else
      echo "Dumping $1 ..."
      echo "SET autocommit=0;SET unique_checks=0;SET foreign_key_checks=0;" > "$1.sql"
      mysqldump -u $MYSQL_USER --password=$MYSQL_PASS $1 >> "$1.sql"; 
      echo "SET autocommit=1;SET unique_checks=1;SET foreign_key_checks=1;commit;" >> "$1.sql"
      gzip "$1.sql"
fi

答案 6 :(得分:0)

我不是bash master,但我只是用bash脚本来做。如果不知道数据目录和数据库名称,就可以扫描所有.frm文件(该db /目录中的每个表一个),以获取表列表。

我确信有办法让它更流畅并接受参数或诸如此类的东西,但这对我来说效果很好。

tables_in_a_db_to_sql.sh

#!/bin/bash

database="this_is_my_database"
datadir="/var/lib/mysql/"
datadir_escaped="\/var\/lib\/mysql\/"

all_tables=($(ls $datadir$database/*.frm | sed s/"$datadir_escaped$database\/"/""/g | sed s/.frm//g))

for t in "${all_tables[@]}"; do
        outfile=$database.$t.sql
        echo "-- backing up $t to $outfile"
        echo "mysqldump [options] $database $t > $outfile"
        # mysqldump [options] $database $t > $outfile
done

根据需要填写[options]和所需的outfile约定,并取消注释最后一个mysqldump行。

答案 7 :(得分:0)

如果要转储所有数据库中的所有表,只需结合Elias Torres Arroyo和Trutane的答案: 而且,如果您不想在终端上输入密码,只需将密码存储在一个额外的配置文件(chmod 0600)中,请参阅type system

#!/usr/bin/env bash

# this file
# a) gets all databases from mysql
# b) gets all tables from all databases in a)
# c) creates subfolders for every database in a)
# d) dumps every table from b) in a single file

    # this is a mixture of scripts from Trutane (http://stackoverflow.com/q/3669121/138325) 
    # and Elias Torres Arroyo (https://stackoverflow.com/a/14711298/8398149)

[ $# -lt 3 ] && echo "Usage: $(basename $0) <DB_HOST> <DB_USER> <DIR>" && exit 1

DB_host=$1
DB_user=$2
BACKUP_DIR=$3/$(date +%Y-%m-%dT%H_%M_%S);

test -d "$BACKUP_DIR" || mkdir -p "$BACKUP_DIR"
# Get the database list, exclude information_schema
database_count=0
tbl_count=0

for db in $(mysql --defaults-extra-file=/yourfile/config.cnf -B -s -u $DB_user -e 'show databases' | grep -v information_schema)
do
    # dump each database in a separate file
    (( database_count++ ))
    DIR=$BACKUP_DIR/$db
    [ -n "$DIR" ] || DIR=.

    test -d $DIR || mkdir -p $DIR

    echo
    echo "Dumping tables into separate SQL command files for database '$db' into dir=$DIR"

    for t in $(mysql --defaults-extra-file=/yourfile/config.cnf -NBA -h $DB_host -u $DB_user -D $db -e 'show tables')
    do
        echo "DUMPING TABLE: $db.$t"
        mysqldump --defaults-extra-file=/yourfile/config.cnf -h $DB_host -u $DB_user $db $t  > $DIR/$db.$t.sql
        tbl_count=$(( tbl_count + 1 ))
    done

    echo "Database $db is finished"

done


echo "Backup completed"

答案 8 :(得分:0)

对于Windows Server,您可以使用如下所示的批处理文件:

set year=%DATE:~10,4%
set day=%DATE:~7,2%
set mnt=%DATE:~4,2%
set hr=%TIME:~0,2%
set min=%TIME:~3,2%

IF %day% LSS 10 SET day=0%day:~1,1%
IF %mnt% LSS 10 SET mnt=0%mnt:~1,1%
IF %hr% LSS 10 SET hr=0%hr:~1,1%
IF %min% LSS 10 SET min=0%min:~1,1%

set backuptime=%year%-%mnt%-%day%-%hr%-%min%
set backupfldr=C:\inetpub\wwwroot\backupfiles\
set datafldr="C:\Program Files\MySQL\MySQL Server 5.5\data"
set zipper="C:\inetpub\wwwroot\backupfiles\zip\7za.exe"
set retaindays=21

:: Switch to the data directory to enumerate the folders
pushd %datafldr%

:: Get all table names and save them in a temp file
mysql --skip-column-names --user=root --password=mypassword mydatabasename -e "show tables" > tables.txt

:: Loop through all tables in temp file so that we can save one backup file per table
for /f "skip=3 delims=|" %%i in (tables.txt) do (
  set tablename = %%i
  mysqldump --user=root --password=mypassword mydatabasename %%i > "%backupfldr%mydatabasename.%backuptime%.%%i.sql"
)
del tables.txt

:: Zip all files ending in .sql in the folder
%zipper% a -tzip "%backupfldr%backup.mydatabasename.%backuptime%.zip" "%backupfldr%*.sql"

echo "Deleting all the files ending in .sql only"
del "%backupfldr%*.sql"

echo "Deleting zip files older than 21 days now"
Forfiles /p %backupfldr% /m *.zip /d -%retaindays% /c "cmd /c del /q @path"

然后使用Windows Task Scheduler对其进行计划。

此外,如果要在备份中排除某些表,请注意,可以在“显示表”语句上使用where子句,但列名取决于数据库名。

例如,如果您的数据库名称为“ blah”,那么“ 显示表”结果集中的列名称将为“ tables_in_blah”。这意味着您可以添加一个类似于以下内容的where子句:

show tables where tables_in_blah <> 'badtable'

show tables where tables_in_blah like '%goodtable%'

答案 9 :(得分:-1)

请参阅Pauli Marcus的以下文章:

Howto split a SQL database dump into table-wise files

  

将包含整个数据库的sql文件拆分为每个表文件   非常简单:为任何出现的DROP TABLE grep .sql。生成   DROP TABLE中包含的表名中的文件名   声明。将输出回显到文件。这是一个小脚本   需要一个.sql文件作为输入:

#!/bin/bash

file=$1 # the input file
directory="$file-splitted" # the output directory
output="$directory/header" # the first file containing the header
GREP="DROP TABLE" # what we are looking for

mkdir $directory # create the output directory

while read line
do
   # if the current line contains the wanted statement
   if [ $(echo "$line" | grep -c "$GREP") == "1" ]
   then
      # extract the file name
      myfile=$(echo $line | awk '{print $5}' | sed -e 's/`//g' -e 's/;//g')
      # set the new file name
      output="$directory/$myfile"
   fi
       echo "$line" >> $output # write to file
done < $file