MySQL mysqldump 백업 및 복원 가이드: 구문, 옵션 및 모범 사례

1. 소개

Backing up and restoring a MySQL database is an essential task for maintaining system security and stability. You need to be prepared to recover quickly in the event of data loss or server failure. The mysqldump command is a commonly used tool for backing up MySQL databases. It is characterized by its simple syntax and flexibility through a wide range of options.

MySQL 데이터베이스를 백업하고 복원하는 것은 시스템 보안과 안정성을 유지하기 위한 필수 작업입니다. 데이터 손실이나 서버 장애가 발생했을 때 신속하게 복구할 수 있도록 준비되어 있어야 합니다. mysqldump 명령은 MySQL 데이터베이스를 백업할 때 흔히 사용되는 도구이며, 간단한 구문과 다양한 옵션을 통한 유연성이 특징입니다.

In this article, we will explain everything from the basics of the mysqldump command to how to use advanced options, as well as the procedures for restoring from a backup in detail.

이 문서에서는 mysqldump 명령의 기본부터 고급 옵션 사용 방법, 그리고 백업으로부터 복원하는 절차까지 자세히 설명합니다.

2. 기본 mysqldump 구문

mysqldump is a command used to dump (export) MySQL data and structure. First, let’s review the basic syntax.

mysqldump는 MySQL 데이터와 구조를 덤프(내보내기)하는 데 사용되는 명령입니다. 먼저 기본 구문을 살펴보겠습니다.

mysqldump -u [username] -p [database_name] > [output_file_name.sql]

매개변수 설명

  • -u [username] : MySQL에 연결할 때 사용할 사용자 이름을 지정합니다.
  • -p : 비밀번호를 지정합니다(생략하면 대화식으로 입력하라는 프롬프트가 표시됩니다).
  • [database_name] : 백업하려는 데이터베이스 이름을 지정합니다.
  • > [output_file_name] : 출력 덤프 파일의 경로를 지정합니다.

By adding options to this basic syntax, you can create dump files tailored to various situations.

이 기본 구문에 옵션을 추가하면 다양한 상황에 맞는 덤프 파일을 만들 수 있습니다.

샘플 명령

mysqldump -u root -p my_database > my_database_backup.sql

This command backs up the database named my_database to a file called my_database_backup.sql.

이 명령은 my_database 데이터베이스를 my_database_backup.sql 파일로 백업합니다.

3. 일반 옵션 및 사용법

3.1 –all-databases

Using the --all-databases option allows you to back up all databases on the MySQL server at once. This option is useful when managing multiple databases in bulk.

--all-databases 옵션을 사용하면 MySQL 서버에 있는 모든 데이터베이스를 한 번에 백업할 수 있습니다. 여러 데이터베이스를 한꺼번에 관리할 때 유용합니다.

mysqldump -u root -p --all-databases > all_databases_backup.sql

3.2 –no-data

The --no-data option dumps only the database structure and does not output the actual data. It is useful when you want to replicate only the table definitions.

--no-data 옵션은 실제 데이터를 출력하지 않고 데이터베이스 구조만 덤프합니다. 테이블 정의만 복제하고 싶을 때 유용합니다.

mysqldump -u root -p --no-data my_database > my_database_schema.sql

3.3 –add-drop-table

When you use the --add-drop-table option, DROP TABLE statements are included in the backup file. This helps prevent duplicate tables during restoration and makes it easier to overwrite existing tables.

--add-drop-table 옵션을 사용하면 백업 파일에 DROP TABLE 문이 포함됩니다. 복원 시 중복 테이블을 방지하고 기존 테이블을 덮어쓰기 쉽게 해줍니다.

mysqldump -u root -p --add-drop-table my_database > my_database_backup.sql

3.4 –lock-tables

The --lock-tables option locks tables during the backup process to prevent data from being modified while dumping. This is effective when working with a live database.

--lock-tables 옵션은 백업 과정에서 테이블을 잠가 덤프 중 데이터가 변경되는 것을 방지합니다. 실시간 데이터베이스를 다룰 때 효과적입니다.

mysqldump -u root -p --lock-tables my_database > locked_backup.sql

4. 고급 옵션

4.1 –where 옵션을 사용한 조건부 덤프

You can use the --where option to dump data based on specific conditions. For example, if you want to back up only data created after a certain date, this option is effective.

--where 옵션을 사용하면 특정 조건에 따라 데이터를 덤프할 수 있습니다. 예를 들어, 특정 날짜 이후에 생성된 데이터만 백업하고 싶을 때 유용합니다.

mysqldump -u root -p my_database --where="created_at >= '2023-01-01'" > filtered_data_backup.sql

In this example, only data where the created_at field is on or after January 1, 2023 is backed up.

이 예에서는 created_at 필드가 2023년 1월 1일 이후인 데이터만 백업됩니다.

4.2 –xml 옵션

The --xml option exports data in XML format. Use this option when providing data to systems or applications that require XML format.

--xml 옵션은 데이터를 XML 형식으로 내보냅니다. XML 형식이 필요한 시스템이나 애플리케이션에 데이터를 제공할 때 사용합니다.

mysqldump -u root -p --xml my_database > database_backup.xml

5. mysqldump를 사용한 데이터 복원

To restore a database from a backup file created with mysqldump, use the mysql command.

mysqldump로 만든 백업 파일에서 데이터베이스를 복원하려면 mysql 명령을 사용합니다.

mysql -u [username] -p [database_name] < [backup_file.sql]

5.1 복원 예시

The following command restores my_database from the my_database_backup.sql file.

다음 명령은 my_databasemy_database_backup.sql 파일에서 복원합니다.

mysql -u root -p my_database < my_database_backup.sql

중요 참고 사항

  • Overwrite Risk : 복원 중에 기존 데이터가 덮어써질 수 있습니다. 주의해서 진행하십시오.
  • Check Encoding : 다중 바이트 문자를 다룰 때 문자 인코딩이 일치하지 않으면 정상 복원이 어려울 수 있습니다.

6. 백업 전략 팁

6.1 정기 백업 자동화

데이터베이스 내용은 지속적으로 변하기 때문에 정기적인 백업을 자동화하는 것이 중요합니다. cron 작업을 사용하여 매일 백업을 실행할 수 있습니다.

0 2 * * * mysqldump -u root -p my_database > /path/to/backup/my_database_$(date +\%F).sql

이 예시에서는 백업이 매일 새벽 2시에 실행되며, 파일 이름에 날짜가 포함됩니다.

6.2 증분 백업

매번 모든 데이터를 백업하는 대신, 증분 백업을 사용하면 변경된 데이터만 백업할 수 있습니다. --where 옵션을 사용하여 최근에 변경된 데이터만 덤프함으로써 효율성을 높일 수 있습니다.

7. 요약

mysqldump 도구는 MySQL 백업에 매우 유용합니다. 간단하면서도 강력합니다. 기본 사용법과 고급 옵션을 모두 이해하고 백업 전략을 수립함으로써 시스템 보안과 데이터 보호를 강화할 수 있습니다. 정기적인 백업을 자동화하고 증분 백업을 활용하는 것이 효율적인 데이터 관리의 핵심입니다.

이 문서를 참고하여 mysqldump 기반 백업 작업을 최적화하고 데이터베이스 관리 방식을 개선하십시오.