웹사이트 검색

'Automysqlbackup' 및 'Autopostgresqlbackup' 도구를 사용하여 MySQL/MariaDB 및 PostgreSQL을 백업/복원하는 방법


데이터베이스 관리자(DBA)이거나 데이터베이스 유지, 백업, 복원을 담당하는 경우 데이터가 손실되어서는 안 된다는 것을 알고 계실 것입니다. 이유는 간단합니다. 데이터 손실은 중요한 정보의 손실을 의미할 뿐만 아니라 비즈니스에 금전적인 피해를 줄 수도 있습니다.

따라서 항상 다음 사항을 확인해야 합니다.

1. 데이터베이스는 정기적으로 백업됩니다.
2. 해당 백업은 안전한 장소에 저장되며
3. 정기적으로 복원 훈련을 수행합니다.

이러한 상황에서 수행해야 할 작업을 연습하지 않고 큰 문제에 직면하고 싶지 않으므로 이 마지막 활동을 간과해서는 안 됩니다.

이 튜토리얼에서는 MySQL/MariaDBPostgreSQL 데이터베이스를 각각 백업하는 두 가지 유용한 유틸리티인 automysqlbackup을 소개합니다. Strong> 및 autopostgresqlbackup.

후자가 전자를 기반으로 하므로 automysqlbackup에 중점을 두고 설명하고 autopgsqlbackup과의 차이점을 강조하겠습니다.

시스템 전체에 충돌이 발생하는 경우에도 계속해서 보호받을 수 있도록 백업 디렉터리에 마운트된 네트워크 공유에 백업을 저장하는 것이 좋습니다.

MySQL에 대한 다음 유용한 가이드를 읽어보세요.

MySQL/MariaDB/PostgreSQL 데이터베이스 설치

1. 이 가이드에서는 MySQL/MariaDB/PostgreSQL 인스턴스가 실행 중이어야 한다고 가정합니다. 그렇지 않은 경우 설치하세요. 다음 패키지:

Fedora 기반 배포판:

yum update && yum install mariadb mariadb-server mariadb-libs postgresql postgresql-server postgresql-libs

데비안 및 파생 상품:

aptitude update && aptitude install mariadb-client mariadb-server mariadb-common postgresql-client postgresql postgresql-common

2. 사용할 수 있는 테스트용 MySQL/MariaDB/PostgreSQL 데이터베이스가 있습니다. < Strong style="color:red">이러한 도구에 익숙해질 때까지는 프로덕션 환경에서 automysqlbackup 또는 autopostgresqlbackup을 사용하지 마십시오.

그렇지 않으면 계속하기 전에 두 개의 샘플 데이터베이스를 만들고 데이터로 채우십시오. 이 기사에서는 다음 데이터베이스와 테이블을 사용합니다.

CREATE DATABASE mariadb_db;
CREATE TABLE tecmint_tbl (UserID INT AUTO_INCREMENT PRIMARY KEY, 
UserName VARCHAR(50), 
IsActive BOOL);

CREATE DATABASE postgresql_db;
CREATE TABLE tecmint_tbl (
UserID SERIAL PRIMARY KEY,
UserName VARCHAR(50),
IsActive BOOLEAN);

CentOS 7 및 Debian 8에 automysqlbackup 및 autopgsqlbackup 설치

3. Debian 8에서는 두 도구 모두 저장소에서 사용할 수 있으므로 설치는 다음을 실행하는 것만큼 간단합니다.

aptitude install automysqlbackup autopostgresqlbackup

반면 CentOS 7에서는 설치 스크립트를 다운로드하여 실행해야 합니다. 아래 섹션에서는 CentOS 7에서 이러한 도구를 설치, 구성 및 테스트하는 데만 중점을 둘 것입니다. Debian 8에서는 거의 즉시 사용할 수 있기 때문입니다. 이 문서의 뒷부분에서 필요한 설명을 작성합니다.

CentOS 7에서 automysqlbackup 설치 및 구성

4. /opt 내에 작업 디렉토리를 생성하여 설치 스크립트를 다운로드하고 실행해 보겠습니다.

mkdir /opt/automysqlbackup
cd /opt/automysqlbackup
wget http://ufpr.dl.sourceforge.net/project/automysqlbackup/AutoMySQLBackup/AutoMySQLBackup%20VER%203.0/automysqlbackup-v3.0_rc6.tar.gz
tar zxf automysqlbackup-v3.0_rc6.tar.gz
./install.sh

5. automysqlbackup의 구성 파일은 myserver.conf 이름 아래 /etc/automysqlbackup 내에 있습니다. 가장 관련성이 높은 구성 지시문을 살펴보겠습니다.

Username to access the MySQL server
CONFIG_mysql_dump_username='root'
Password
CONFIG_mysql_dump_password='YourPasswordHere'
Host name (or IP address) of MySQL server
CONFIG_mysql_dump_host='localhost'
Backup directory
CONFIG_backup_dir='/var/backup/db/automysqlbackup'
List of databases for Daily/Weekly Backup e.g. ( 'DB1' 'DB2' 'DB3' ... )
set to (), i.e. empty, if you want to backup all databases
CONFIG_db_names=(AddYourDatabase Names Here)
List of databases for Monthly Backups.
set to (), i.e. empty, if you want to backup all databases
CONFIG_db_month_names=(AddYourDatabase Names Here)
Which day do you want monthly backups? (01 to 31)
If the chosen day is greater than the last day of the month, it will be done
on the last day of the month.
Set to 0 to disable monthly backups.
CONFIG_do_monthly="01"
Which day do you want weekly backups? (1 to 7 where 1 is Monday)
Set to 0 to disable weekly backups.
CONFIG_do_weekly="5"
Set rotation of daily backups. VALUE*24hours
If you want to keep only today's backups, you could choose 1, i.e. everything older than 24hours will be removed.
CONFIG_rotation_daily=6
Set rotation for weekly backups. VALUE*24hours. A value of 35 means 5 weeks.
CONFIG_rotation_weekly=35
Set rotation for monthly backups. VALUE*24hours. A value of 150 means 5 months.
CONFIG_rotation_monthly=150
Include CREATE DATABASE statement in backup?
CONFIG_mysql_dump_create_database='no'
Separate backup directory and file for each DB? (yes or no)
CONFIG_mysql_dump_use_separate_dirs='yes'
Choose Compression type. (gzip or bzip2)
CONFIG_mysql_dump_compression='gzip'
What would you like to be mailed to you?
- log   : send only log file
- files : send log file and sql files as attachments (see docs)
- stdout : will simply output the log to the screen if run manually.
- quiet : Only send logs if an error occurs to the MAILADDR.
CONFIG_mailcontent='quiet'
Email Address to send mail to? ([email )
CONFIG_mail_address='root'
Do you wish to encrypt your backups using openssl?
#CONFIG_encrypt='no'
Choose a password to encrypt the backups.
#CONFIG_encrypt_password='password0123'
Command to run before backups (uncomment to use)
#CONFIG_prebackup="/etc/mysql-backup-pre"
Command run after backups (uncomment to use)
#CONFIG_postbackup="/etc/mysql-backup-post"

필요에 따라 automysqlbackup을 구성한 후에는 /etc/automysqlbackup/README에 있는 README 파일을 확인하는 것이 좋습니다.

MySQL 데이터베이스 백업

6. 준비가 되면 구성 파일을 인수로 전달하여 프로그램을 실행하세요.

automysqlbackup /etc/automysqlbackup/myserver.conf

daily 디렉토리를 빠르게 검사하면 automysqlbackup이 성공적으로 실행되었음을 알 수 있습니다.

pwd
ls -lR daily

물론 crontab 항목을 추가하여 하루 중 귀하의 필요에 가장 적합한 시간에 automysqlbackup을 실행할 수 있습니다(아래 예에서는 매일 오전 1:30).

30 01 * * * /usr/local/bin/automysqlbackup /etc/automysqlbackup/myserver.conf

MySQL 백업 복원

7. 이제 의도적으로 mariadb_db 데이터베이스를 삭제해 보겠습니다.

다시 생성해서 백업을 복원해 보겠습니다. MariaDB 프롬프트에 다음을 입력합니다.

CREATE DATABASE mariadb_db;
exit

그런 다음 다음을 찾으세요.

cd /var/backup/db/automysqlbackup/daily/mariadb_db
ls

그리고 백업을 복원합니다.

mysql -u root -p mariadb_db < daily_mariadb_db_2015-09-01_23h19m_Tuesday.sql
mysql -u root -p
MariaDB [(none)]> USE mariadb_db; 
MariaDB [(none)]> SELECT * FROM tecmint_tb1;

CentOS 7에서 autopostgresqlbackup 설치 및 구성

8. autopostgresqlCentOS 7에서 완벽하게 작동하려면 먼저 몇 가지 종속성을 설치해야 합니다.

yum install mutt sendmail

그런 다음 이전과 같이 프로세스를 반복해 보겠습니다.

mkdir /opt/autopostgresqlbackup
cd /opt/autopostgresqlbackup
wget http://ufpr.dl.sourceforge.net/project/autopgsqlbackup/AutoPostgreSQLBackup/AutoPostgreSQLBackup-1.0/autopostgresqlbackup.sh.1.0
mv autopostgresqlbackup.sh.1.0 /opt/autopostgresqlbackup/autopostgresqlbackup.sh

스크립트를 실행 가능하게 만들고 서비스를 시작/활성화해 보겠습니다.

chmod 755 autopostgresqlbackup.sh
systemctl start postgresql
systemctl enable postgresql

마지막으로 백업 디렉터리 설정 값을 다음과 같이 편집합니다.

BACKUPDIR="/var/backup/db/autopostgresqlbackup"

automysqlbackup의 구성 파일을 마친 후에는 이 도구를 구성하는 것이 매우 쉽습니다(작업의 일부는 사용자에게 달려 있습니다).

9. CentOS 7에서는 Debian 8과 달리 autopostgresqlbackuppostgres로 실행되는 것이 가장 좋습니다. 시스템 사용자이므로 그렇게 하려면 해당 계정으로 전환하거나 crontab 파일에 cron 작업을 추가해야 합니다.

crontab -u postgres -e
30 01 * * * /opt/autopostgresqlbackup/autopostgresqlbackup.sh

그런데 백업 디렉토리를 생성해야 하며 해당 권한과 그룹 소유권을 0770postgres로 반복적으로 설정해야 합니다(다시 말하지만 <에서는 필요하지 않습니다). 강한>데비안):

mkdir /var/backup/db/autopostgresqlbackup
chmod -R 0770 /var/backup/db/autopostgresqlbackup
chgrp -R postgres /var/backup/db/autopostgresqlbackup

결과:

cd /var/backup/db/autopostgresqlbackup
pwd
ls -lR daily

10. 이제 필요할 때 파일을 복원할 수 있습니다(빈 데이터베이스를 다시 생성한 후 사용자 postgres로 이 작업을 수행하는 것을 기억하십시오).

gunzip -c postgresql_db_2015-09-02.Wednesday.sql.gz | psql postgresql_db

데비안 8의 고려사항

앞서 언급했듯이 Debian에서 이러한 도구를 설치하는 것이 더 간단할 뿐만 아니라 해당 구성도 더 간단합니다. 다음 위치에서 구성 파일을 찾을 수 있습니다.

  1. Automysqlbackup: /etc/default/automysqlbackup
  2. Autopostgresqlbackup: /etc/default/autopostgresqlbackup

요약

이 기사에서는 두 가지 훌륭한 데이터베이스 백업인 automysqlbackupautopostgresqlbackup을 설치하고 사용하는 방법을 설명했습니다(첫 번째 사용 방법을 배우면 두 번째도 마스터하는 데 도움이 됩니다). DBA 또는 시스템 관리자/엔지니어의 작업을 훨씬 쉽게 만들어 주는 도구입니다.

이메일 알림을 설정하거나 이메일을 통해 백업 파일을 첨부 파일로 전송하여 이 주제를 확장할 수 있습니다. 꼭 필요한 것은 아니지만 때로는 유용할 수 있습니다.

마지막으로 구성 파일의 권한은 최소(대부분의 경우 0600)로 설정되어야 한다는 점을 기억하세요. 이 기사에 대한 귀하의 의견을 기다리겠습니다. 아래 양식을 사용하여 언제든지 메모를 남겨주세요.