我想将每个mysql表转储到单独的文件中。手册指出此语法为
mysqldump [options] db_name [tbl_name ...]
这表示您事先知道表名称。我可以设置现在知道每个表名称的脚本,但是说我在路上添加了一个新表,却忘记了更新转储脚本。然后我就缺少一个或多个表的转储。
有没有办法自动将每个现有表转储到单独的文件中?还是我必须做一些脚本赋;查询数据库,获取所有表名,然后按名称转储它们。
如果我走脚本赋路线,哪些脚本语言可以访问mysql数据库?
Answers:
mysqldump命令行程序会为您执行此操作-尽管文档对此尚不清楚。
需要注意的一件事是〜/ output / dir必须是拥有mysqld的用户可写的。在Mac OS X上:
sudo chown -R _mysqld:_mysqld ~/output/dir
mysqldump --user=dbuser --password --tab=~/output/dir dbname
执行完上述操作后,您将拥有一个包含每个表的架构的tablename.sql文件(创建表语句)和一个包含数据的tablename.txt文件。
如果只想使用模式进行转储,请添加--no-data标志:
mysqldump --user=dbuser --password --no-data --tab=~/output/dir dbname
mysqlimport
可用于导入结果数据txt文件。同样,任何包含句点的表名,(.)
例如:my.table
只会导致具有相同名称的数据文件(不带.txt扩展名)。dev.mysql.com/doc/refman/5.0/en/…此外,这似乎是--single-transaction
每个文件转储执行表的唯一方法。
FILE
服务器上的特权,并将文件写入服务器上的磁盘。
这是一个脚本,它将表数据作为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"
mysqldump --extended-insert=FALSE --order-by-primary --complete-insert
您可以通过以下方式完成此操作:
mysqldump
# 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
--routines
并--triggers
捕获过程和触发器。
这是相应的导入。
#!/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'"
#!/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
看来这里每个人都忘记了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
如果要从所有数据库中转储所有表,只需结合Elias Torres Arroyo和Trutane的答案:如果您不想在终端上提供密码,只需将您的密码存储在一个额外的配置文件中(chmod 0600)-请参见Mysqldump通过cron和密码安全
#!/bin/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)
# usage:
# sk-db.bash parameters
# where pararmeters are:
# d "dbs to leave"
# t " tables to leave"
# u "user who connects to database"
# h "db host"
# f "/backup/folder"
user='root'
host='localhost'
backup_folder=''
leave_dbs=(information_schema mysql)
leave_tables=()
while getopts ":d:t:u:h:f:" opt; do
case $opt in
d) leave_dbs=( $OPTARG )
;;
t) leave_tables=( $OPTARG )
;;
u) user=$OPTARG
;;
h) host=$OPTARG
;;
f) backup_folder=$OPTARG
;;
\?) echo "Invalid option -$OPTARG" >&2
;;
esac
done
echo '****************************************'
echo "Database Backup with these options"
echo "Host $host"
echo "User $user"
echo "Backup in $backup_folder"
echo '----------------------------------------'
echo "Databases to emit:"
printf "%s\n" "${leave_dbs[@]}"
echo '----------------------------------------'
echo "Tables to emit:"
printf "%s\n" "${leave_tables[@]}"
echo '----------------------------------------'
BACKUP_DIR=$backup_folder/$(date +%Y-%m-%dT%H_%M_%S);
CONFIG_FILE=/root/db-config.cnf
function contains() {
local n=$#
local value=${!n}
for ((i=1;i < $#;i++)) {
if [ "${!i}" == "${value}" ]; then
echo "y"
return 0
fi
}
echo "n"
return 1
}
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=$CONFIG_FILE -B -s -u $user -e 'show databases' )
do
if [ $(contains "${leave_dbs[@]}" "$db") == "y" ]; then
echo "leave database $db as requested"
else
# 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=$CONFIG_FILE -NBA -h $host -u $user -D $db -e 'show tables')
do
if [ $(contains "${leave_tables[@]}" "$db.$t") == "y" ]; then
echo "leave table $db.$t as requested"
else
echo "DUMPING TABLE: $db.$t"
# mysqldump --defaults-extra-file=$CONFIG_FILE -h $host -u $user $db $t > $DIR/$db.$t.sql
tbl_count=$(( tbl_count + 1 ))
fi
done
echo "Database $db is finished"
echo '----------------------------------------'
fi
done
echo '----------------------------------------'
echo "Backup completed"
echo '**********************************************'
而且,这有助于:
我不是bash大师,但是我只是用bash脚本来做。在不了解MySQL的情况下,只要了解数据目录和数据库名称,您就可以扫描所有.frm文件(该db /目录中的每个表一个),以查找表列表。
我敢肯定,有一些方法可以使它更流畅并接受参数或其他参数,但这对我来说很好。
table_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]和所需的输出文件约定,并取消注释最后的mysqldump行。
对于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%'
请参阅以下Pauli Marcus的文章:
将包含整个数据库的sql文件拆分为每个表的文件非常容易:对于出现DROP TABLE的情况,请对.sql进行grep。从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
DROP TABLE
怎么办?这不是一个安全的解决方案。