it-source

완전한 mysql mysqldump 파일에서 단일 테이블을 복원할 수 있습니까?

criticalcode 2022. 10. 20. 21:17
반응형

완전한 mysql mysqldump 파일에서 단일 테이블을 복원할 수 있습니까?

모든 테이블로 구성된 mysql 데이터베이스의 mysqldump 백업이 있으며 약 440megs입니다.mysqldump에서 테이블 중 하나의 내용만 복원하고 싶습니다.이게 가능합니까?이론적으로는 원하는 표를 재구축하는 섹션을 잘라낼 수 있지만 텍스트 문서를 효과적으로 편집하는 방법도 모릅니다.

원하는 테이블만 추출하려면 sed를 사용해 보세요.

이 '먹다'라고 합시다.mytable " " " 입니다.mysql.dump덤프가 되어 있는 입니다.

$ sed -n -e '/CREATE TABLE.*`mytable`/,/Table structure for table/p' mysql.dump > mytable.dump

됩니다.mytable.dump「」의 사이에 있는 것CREATE TABLE mytable 리리음음음음음음음음CREATE TABLE다음 표에 대응합니다.

할 수 .mytable.dump를 알 수 있습니다.mytable, 「」(「」))INSERT를 참조해 주세요.

uloBas의 수정 버전을 사용했습니다.EI의 SED 명령입니다.위의 DROP 명령어가 포함되어 있어 mysql이 테이블(UNLOCK)에 데이터를 덤프할 때까지 읽습니다.WP_users를 Wordpress 사이트 여러 곳에 Import하는 일을 했습니다.

sed -n -e '/DROP TABLE.*`mytable`/,/UNLOCK TABLES/p' mydump.sql > tabledump.sql

이게 더 쉽게 할 수 있어요?저는 이렇게 했습니다.

임시 데이터베이스 작성(예: 복원):

mysqladmin -u root -p 복원 생성

temp 데이터베이스의 풀 덤프를 복원합니다.

mysql - u root -p --one-parames restore < fulldump >sql

복구할 테이블을 덤프합니다.

mysqldump restore mytable > mytable 。sql

테이블을 다른 데이터베이스로 Import합니다.

mysql -u root -p database < mytable 。sql

간단한 해결책은 개별적으로 복원할 테이블만 덤프를 만드는 것입니다.mysqldump 명령을 사용하면 다음 구문을 사용할 수 있습니다.

mysqldump -u [user] -p[password] [database] [table] > [output_file_name].sql

그런 다음 정상적으로 Import하면 덤프된 테이블만 Import됩니다.

어떤 방법으로든 이 처리를 하는 프로세스는 덤프의 전체 텍스트를 검토하고 어떤 방식으로든 해석해야 합니다.나는 그저 에 대해주고 싶다.

INSERT INTO `the_table_i_want`

출력을 mysql로 파이프합니다.INSERT가 올바른 방법인지 확인하기 위해 이전 덤프의 첫 번째 표를 확인하십시오.

편집: 네, 이번에는 올바른 포맷을 했습니다.

  1. 지원하다

    $ mysqldump -A | gzip > mysqldump-A.gz
    
  2. 단일 테이블 복원

    $ mysql -e "truncate TABLE_NAME" DB_NAME
    $ zgrep ^"INSERT INTO \`TABLE_NAME" mysqldump-A.gz | mysql DB_NAME
    

@bryn 명령어를 사용해 보겠습니다만, 「delimiter」를 사용하고 있지 않은 경우는, 프리픽스 또는 서픽스를 가지는 테이블도 추출합니다.이 작업은 보통 다음과 같습니다.

sed -n -e '/DROP TABLE.*`mytable`/,/UNLOCK TABLES/p' dump.sql > mytable.sql

또한 테스트 목적으로 가져오기 전에 테이블 이름을 변경할 수도 있습니다.

sed -n -e 's/`mytable`/`mytable_restored`/g' mytable.sql > mytable_restored.sql

Import 하려면 mysql 명령을 사용합니다.

mysql -u root -p'password' mydatabase < mytable_restore.sql

이 문제를 해결할 수 있는 방법 중 하나는 임시 데이터베이스로 복원하고 임시 데이터베이스에서 해당 테이블만 덤프하는 것입니다.그런 다음 새 스크립트를 사용합니다.

sed -n -e '/-- Table structure for table `my_table_name`/,/UNLOCK TABLES/p' database_file.sql > table_file.sql

에 SQL이 되어 있는 것은 때문에 의 몇 보다 나은 .DROP TABLE this 덤프가 될 야.이건 모든 종류의 쓰레기들이 쓸 수 있을 거야.

이 툴은 tbdba-restore-mysqldump가 필요할 수 있습니다.pl

https://github.com/orczhou/dba-tool/blob/master/tbdba-restore-mysqldump.pl

예: 데이터베이스 덤프 파일에서 테이블 복원:

tbdba-restore-mysqldump.pl -t yourtable -s yourdb -f backup.sql

테이블은 덤프와 데이터베이스 모두에서 동일한 구조로 표시되어야 합니다.

`zgrep -a ^"INSERT INTO \`table_name" DbDump-backup.sql.tar.gz | mysql -u<user> -p<password> database_name`

또는

`zgrep -a ^"INSERT INTO \`table_name" DbDump-backup.sql | mysql -u<user> -p<password> database_name`

이것도 도움이 될 거예요.

# mysqldump -u root -p database0 > /tmp/database0.sql
# mysql -u root -p -e 'create database database0_bkp'
# mysql -u root -p database0_bkp < /tmp/database0.sql
# mysql -u root -p database0 -e 'insert into database0.table_you_want select * from database0_bkp.table_you_want'

대부분의 최신 텍스트 편집기는 시스템이 감당할 수 있는 경우 이 크기의 텍스트 파일을 처리할 수 있습니다.

어쨌든, 나는 그것을 한 번 매우 빨리 해야 했고 어떤 도구도 찾을 시간이 없었다.새로운 MySQL 인스턴스를 설정하고 전체 백업을 가져온 다음 원하는 테이블만 내뱉었습니다.

그리고 그 테이블을 메인 데이터베이스로 Import했습니다.

그것은 지루했지만 오히려 쉬웠다.행운을 빌어요.

vi 에디터를 사용할 수 있습니다.유형:

vi -o mysql.dump mytable.dump

mysql.dump 「」을 참조해 주세요.mytable.dump을 줄에 맞는 삽입물을 ./ '에 '을 하고 'insert into 'mytable'을 'insert into 'mytable'로 해 주세요.yy에 의해 다음 ctrl+wdown arrow keypp으로 새 :wq qui by qui vi vi editor by.:q.

를 덤프한 에는 복사 수 .Nyy N아, 아, 아, 아, 아, 아, 아, 아, 아, 아, 아, 아.

920 MB의 파일로 했습니다.

몇 가지 옵션을 시도해 봤는데, 엄청나게 느렸어요.360GB 덤프를 몇 분 만에 테이블로 분할합니다.

mysqldump 출력을 작은 파일로 분할하려면 어떻게 해야 합니까?

앞서 말한 'sed' 솔루션은 좋지만 100% 보안은 아니다.

  • 다음과 같은 데이터가 포함된 INSERT 명령이 있을 수 있습니다.테이블 작성...(임의)...마이 테이블...

  • 예를 들어 DML 명령어를 저장하는 경우에는 "CREATE TABLE 'mytable"이라는 문자열도 사용할 수 있습니다.

(and if the table is huge you don't want to check that manually)

I would verify the exact syntax of the dump version used, and have a more restrictive pattern search:

Avoid ".*" and use "^" to ensure we start at the begining of the line. And I'd prefer to grab the initial 'DROP'

All in all, this works better for me:

sed -n -e '/^DROP TABLE IF EXISTS \`mytable\`;/,/^UNLOCK TABLES;/p' mysql.dump > mytable.dump

Get a decent text editor like Notepad++ or Vim (if you're already proficient with it). Search for the table name and you should be able to highlight just the CREATE, ALTER, and INSERT commands for that table. It may be easier to navigate with your keyboard rather than a mouse. And I would make sure you're on a machine with plenty or RAM so that it will not have a problem loading the entire file at once. Once you've highlighted and copied the rows you need, it would be a good idea to back up just the copied part into it's own backup file and then import it into MySQL.

SQL 청크는 "테이블 구조"로 차단됩니다.my_table" 및 "테이블용 데이터 덤핑my_table."

You can use a Windows command line as follows to get the line numbers for the various sections. Adjust the searched string as needed.

find /n "for table `" sql.txt

The following will be returned:

---------- SQL.TXT

[4384]--테이블의 테이블 구조my_table

[4500]--테이블용 데이터 덤프my_table

[4514]--테이블의 테이블 구조some_other_table

... etc.

That gets you the line numbers you need... now, if I only knew how to use them... investigating.

You can import single table using terminal line as given below. Here import single user table into specific database.

mysql -u root -p -D my_database_name < var/www/html/myproject/tbl_user.sql

여기의 독창성 중 일부는 존경하지만, 말 그대로 사용할 이유가 없습니다.sedOP의 질문에 대답할 수 있습니다.

The comment "use --one-database" is the correct answer, built into MySQL/MariaDB. No need for third-party hacks.

mysql -u root -p databasename --one-database < localhost.sql는 원하는 데이터베이스만 Import합니다.

I also found in some cases, when using this to import a series of databases, it would create the next database in the list for me (but not put anything in it). Not sure why it did that, but it made the restore easier.

이 명령어를 사용하여 대화식으로 비밀번호를 입력하면 요청된 데이터베이스가 Import됩니다.

언급URL : https://stackoverflow.com/questions/1013852/can-i-restore-a-single-table-from-a-full-mysql-mysqldump-file

반응형