ウェブサイト検索

「Automysqlbackup」および「Autopostgresqlbackup」ツールを使用して MySQL/MariaDB および PostgreSQL をバックアップ/復元する方法


あなたがデータベース管理者 (DBA) であるか、データベースの保守、バックアップ、復元を担当している場合は、データを失うわけにはいかないことを知っているでしょう。理由は簡単です。データの損失は重要な情報の損失を意味するだけでなく、ビジネスに経済的な損害を与える可能性があるからです。

このため、次のことを常に確認する必要があります。

1. データベースは定期的にバックアップされます。
2. これらのバックアップは安全な場所に保存され、
3. 定期的に復旧訓練を実施します。

このような状況で何を行う必要があるかを練習せずに大きな問題に遭遇したくないので、この最後のアクティビティを見逃してはなりません。

このチュートリアルでは、MySQL/MariaDB および PostgreSQL データベースをそれぞれバックアップする 2 つの優れたユーティリティを紹介します: automysqlbackupautopostgresqlbackup

後者は前者に基づいているため、automysqlbackup に焦点を当てて説明し、autopsqlbackup との違いがある場合は強調します。

システム全体のクラッシュが発生した場合でもカバーできるように、バックアップ ディレクトリにマウントされたネットワーク共有にバックアップを保存することを強くお勧めします。

MySQL に関する次の役立つガイドをお読みください。

MySQL/MariaDB/PostgreSQL データベースのインストール

1. このガイドは、MySQL/MariaDB/PostgreSQL インスタンスが実行されている必要があることを前提としています。そうでない場合は、インストールしてください。次のパッケージ:

Fedora ベースのディストリビューション:

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

Debian とその派生製品:

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

2. 使用できるテスト用の MySQL/MariaDB/PostgreSQL データベースがあります (<これらのツールに慣れるまでは、実稼働環境ではautomysqlbackup または autopostgresqlbackup を使用しないでください)。

それ以外の場合は、続行する前に 2 つのサンプル データベースを作成し、データを入力します。この記事では、次のデータベースとテーブルを使用します。

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 の設定ファイルは、/etc/automysqlbackup 内に myserver.conf という名前で配置されています。最も関連性の高い設定ディレクティブを見てみましょう。

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

毎日ディレクトリを簡単に検査すると、automysqlbackup が正常に実行されていることがわかります。

pwd
ls -lR daily

もちろん、crontab エントリを追加して、ニーズに合った時間帯 (以下の例では毎日午前 1 時 30 分) に automysqlbackup を実行することができます。

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. Debian 8 とは対照的に、CentOS 7 では、autopostgresqlbackuppostgres として実行するのが最適です。システム ユーザーなので、これを行うには、そのアカウントに切り替えるか、その crontab ファイルに cron ジョブを追加する必要があります。

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

ちなみに、バックアップ ディレクトリを作成する必要があり、そのアクセス許可とグループ所有権を 0770postgres に再帰的に設定する必要があります (これも < では必要ありません)strong>Debian):

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

Debian 8 での考慮事項

前に述べたように、Debian へのこれらのツールのインストールだけでなく、それぞれの設定も簡単です。設定ファイルは次の場所にあります。

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

まとめ

この記事では、2 つの優れたデータベース バックアップである automysqlbackupautopostgresqlbackup のインストール方法と使用方法を説明しました (前者の使用方法を学ぶと、後者もマスターできるようになります)。 DBA またはシステム管理者/エンジニアとしてのタスクをはるかに容易にするツール。

電子メール通知を設定したり、バックアップ ファイルを添付ファイルとして電子メールで送信したりすることで、このトピックを拡張できることに注意してください。厳密に必須ではありませんが、場合によっては便利な場合があります。

最後の注意として、構成ファイルのアクセス許可は最小値 ( ほとんどの場合0600) に設定する必要があることに注意してください。この記事についてのご意見をお待ちしております。以下のフォームを使用して、お気軽にお知らせください。