18年運用したサイトのデータが消えました | Monotalk の反省から、当 Blog の PostgresSQL の バックアップを取得して、Google Cloud Storage に保存するようにしました。
実施したことを記載します。
前提
以下の環境で動作確認は実施しています。
-
OS
cat /etc/redhat-release CentOS Linux release 7.5.1804 (Core)
-
PostgresSQLのVersion
psql -V psql (PostgreSQL) 9.6.10
バックアップの対象と、バックアップ先
PostgresSQL の dump ファイルをバックアップの対象として、Google Cloud Storage に保存します。
バックアップは日次で実施し 10日分をバックアップとして保持、10日以前のデータは削除します。
Google Cloud Storage を バックアップ 先に使用する理由
バックアップ 先には Google Cloud Storage を使用します。
使用する理由を以下に記載します。
- 5GB まで無料で使える。
- 世代管理ができる。
- Google Drive でも同様のことはできそうだが、システムで保存するデータと手動で保存しているデータの Storage を分けたかった。
Google Drive に バックアップ する方法のあるようで検索すると 保存方法がヒットします。
個人的には以下の記事が気になりました。
- Googleお役立ちテクニック - Googleドライブで「版」を管理する | マイナビニュース
- PostgresSQLのbackup, restore方法まとめ
- Google Drive & PostgreSQL Integrations | Zapier
- How to Backup PostgreSQL to Google Drive - PostgreSQL Backup
- Backup Postgres DB to Google Drive
- Backing up Confluence PostgreSQL to Google Drive
バックアップスクリプトをスケジュール実行する
バックアップスクリプトを作成し、crontab で スケジュール実行します。
スクリプトでは、gsutil
を使いますので、まず gsutil
のインストールを実施します。
gsutil をインストールする
以下を参考に、gsutil
をインストールします。
gsutil をインストールする | Cloud Storage ドキュメント | Google Cloud
- gsutil の取得
curl コマンドで、google cloud sdk を取得します。
コマンドを実行するとダウンロードが開始します。curl https://sdk.cloud.google.com | bash
まず、インストール先のディレクトリを聞かれます。デフォルトでは、実行ユーザーのhome
ディレクトリ配下がインストール先になります。
このまま、エンターを押しました。
続いて、Downloading Google Cloud SDK install script: https://dl.google.com/dl/cloudsdk/channels/rapid/install_google_cloud_sdk.bash ######################################################################## 100.0% Running install script from: /tmp/tmp.TSbMQDRiEN/install_google_cloud_sdk.bash which curl curl -# -f https://dl.google.com/dl/cloudsdk/channels/rapid/google-cloud-sdk.tar.gz ######################################################################## 100.0% Installation directory (this will create a google-cloud-sdk subdirectory) (/home/xxxxxxxxxxxx):
PATH
に google cloud sdk のコマンドを追加するか聞かれます。
y
を選択して、エンターを押しました。
Modify profile to update your $PATH and enable shell command completion? Do you want to continue (Y/n)? y
.bashrc
のバックアップが取得され、.bashrc
には google cloud sdk のコマンドのパスが追加されました。
Enter a path to an rc file to update, or leave blank to use [/home/xxxxxxxxxxxx/.bashrc]: Backing up [/home/xxxxxxxxxxxx/.bashrc] to [/home/xxxxxxxxxxxx/.bashrc.backup]. [/home/xxxxxxxxxxxx/.bashrc] has been updated.
.bashrc
の設定反映のためシェルスクリプトを再読み込みします。
gcloud の初期化を実行します。exec -l $SHELL
OAuth 認証のためログインを促されます。gcloud init
y
を入力してエンターを押します。
表示されるURLにブラウザでアクセスします。Network diagnostic detects and fixes local network connection issues. Checking network connection...done. Reachability Check passed. Network diagnostic (1/1 checks) passed. You must log in to continue. Would you like to log in (Y/n)? y
ログイン後に表示されるコードをコピーして、Enter verification code:
の後にペーストします。
認証が通ると、対象のプロジェクト番号を聞かれます。対象の番号を選択して、エンターを押します。Go to the following link in your browser: https://accounts.google.com/o/oauth2/auth?redirect_uri=........ Enter verification code:
You are logged in as: [xxxxxxxxxxxxxx@gmail.com]. Pick cloud project to use: [1] xxxxxxxxxxxxxxxxxxxx [2] xxxxxxxxxxxx [3] Create a new project Please enter numeric choice or text value (must exactly match list
gsutil version
と入力して Version 番号が表示されれば上手くインストールができています。
gsutil version gsutil version: 4.33
スクリプトを実装、スケジューリング
Back up postgres db to google cloud storage を参考にスクリプトを作ります。
-
ps-snapshot.sh
#!/bin/bash # Requirements: # - gcloud/gsutil is installed on the box # - gcloud is logged in as a user with write access to Google Cloud Storage # - The file has execution rights so that it can be run in cron # - The Google Cloud Storage bucket already exits # Exit on any error set -e BUCKET='gs://your bucket' JOB_TIMESTAMP=`date +%Y%m%d-%H%M` DATABASE='your database name' DIR='your_backup_dir' GS_UTIL='your gsutil path' cd $DIR NUMBER=`"$GS_UTIL" ls "$BUCKET*.gz" | wc -l` if [ $NUMBER -gt 10 ]; then FILE_NAME=`"$GS_UTIL" ls "$BUCKET*.gz" | head -n 1` "$GS_UTIL" rm -f "$FILE_NAME" else : fi /usr/bin/pg_dump -w $DATABASE > $JOB_TIMESTAMP-pad.sql /bin/tar -cvzf $JOB_TIMESTAMP.tar.gz $JOB_TIMESTAMP-pad.sql "$GS_UTIL" cp $JOB_TIMESTAMP.tar.gz "$BUCKET" rm -f $JOB_TIMESTAMP-pad.sql $JOB_TIMESTAMP.tar.gz
-
説明
-
バックアップは 10 まで取得する。
if [ $NUMBER -gt 10 ]; then
あたりで、バックアップの取得数が10を超える場合削除しています。 -
pg_dump は
-w
オプションをつけて実行する。
DB ユーザと実行ユーザが異なりpg_dmp
を実行する際 パスワード入力を求められたので.pgpass
を作成し、-w
オプションを付与し、パスワード入力を求められないようにしました。 -
Google Cloud Storage には、世代管理を行う機能があります。オブジェクトのバージョニング | Cloud Storage | Google Cloud この機能を活用すると、世代管理を自前で行う必要はないのかもしれません。
-
GS_UTIL
には、gsutil
のインストールパスを記載してください。私がインストールを実施した際はインストールユーザーの home ディレクトリ直下を選択したので、そのパスを記載しました。
-
-
crotab の設定
以下のように crontab を設定しました。
毎日 午前4時にバックアップを取得します。
00 04 * * * /bin/sh /home/xxxxxxxxx/scripts/ps-snapshot.sh >> /var/log/jobs/ps-snapshot.log 2>&1
参考
以下、作成時に、参考にした記事になります。
以上です。
コメント