dehio3’s diary

仕事、生活、趣味のメモ

redashの設定を別のサーバへリストアする

はじめに

redashの検証環境での動作検証が終わったので、本番に作成したredashサーバへ定義をリストアする

環境

ami : "ami-0c7d9b740e997aa69" #redash-5.0.2-b5486-build2-ap-northeast-1

対応

バックアップ(検証環境)

$ sudo su -
# cid=`docker ps | grep redash_postgres | awk '{print $1}'`
# docker exec ${cid} /bin/bash -c 'pg_dump -U postgres postgres | gzip > /usr/local/redash_backup.gz'
# docker cp ${cid}:/usr/local/redash_backup.gz redash_backup.gz
# ls -l redash_backup.gz
-rw-r--r-- 1 root root 93180 Apr  1 07:36 redash_backup.gz

リストア(本番環境)

事前に上記で取得したバックアップファイルを本番サーバの/tmp配下にコピーしておく

redash停止
$ sudo su -
# cd /opt/redash
# docker-compose down
Stopping redash_nginx_1            ... done
Stopping redash_server_1           ... done
Stopping redash_scheduler_1        ... done
Stopping redash_adhoc_worker_1     ... done
Stopping redash_scheduled_worker_1 ... done
Stopping redash_redis_1            ... done
Stopping redash_postgres_1         ... done
Removing redash_nginx_1            ... done
Removing redash_server_1           ... done
Removing redash_scheduler_1        ... done
Removing redash_adhoc_worker_1     ... done
Removing redash_scheduled_worker_1 ... done
Removing redash_redis_1            ... done
Removing redash_postgres_1         ... done
Removing network redash_default
postgresサービスの設定を確認
# cat  docker-compose.yml
  postgres:
    image: postgres:9.5.6-alpine
    env_file: /opt/redash/env
    volumes:
      - /opt/redash/postgres-data:/var/lib/postgresql/data
    restart: always
postgresのみを起動
# docker run -d -v /opt/redash/postgres-data:/var/lib/postgresql/data -p 5432:5432 postgres:9.5.6-alpine
### Docker のcontainer id を確認
# cid=`docker ps | grep postgres | awk '{print $1}'`
# echo ${cid}
4a166ae36010
コンテナの IPアドレスを取得
# dbip=`docker inspect ${cid}  | jq -r .[].NetworkSettings.IPAddress`

### jqが入っていない場合はインストール
# apt install jq
Reading package lists... Done
Building dependency tree      
Reading state information... Done
The following additional packages will be installed:
  libjq1 libonig4
The following NEW packages will be installed:
  jq libjq1 libonig4
0 upgraded, 3 newly installed, 0 to remove and 0 not upgraded.
Need to get 276 kB of archives.
After this operation, 930 kB of additional disk space will be used.
Do you want to continue? [Y/n] y
Get:1 http://ap-northeast-1.ec2.archive.ubuntu.com/ubuntu bionic/universe amd64 libonig4 amd64 6.7.0-1 [119 kB]
Get:2 http://ap-northeast-1.ec2.archive.ubuntu.com/ubuntu bionic/universe amd64 libjq1 amd64 1.5+dfsg-2 [111 kB]
Get:3 http://ap-northeast-1.ec2.archive.ubuntu.com/ubuntu bionic/universe amd64 jq amd64 1.5+dfsg-2 [45.6 kB]
Fetched 276 kB in 0s (11.0 MB/s)
Selecting previously unselected package libonig4:amd64.
(Reading database ... 110254 files and directories currently installed.)
Preparing to unpack .../libonig4_6.7.0-1_amd64.deb ...
Unpacking libonig4:amd64 (6.7.0-1) ...
Selecting previously unselected package libjq1:amd64.
Preparing to unpack .../libjq1_1.5+dfsg-2_amd64.deb ...
Unpacking libjq1:amd64 (1.5+dfsg-2) ...
Selecting previously unselected package jq.
Preparing to unpack .../jq_1.5+dfsg-2_amd64.deb ...
Unpacking jq (1.5+dfsg-2) ...
Setting up libonig4:amd64 (6.7.0-1) ...
Setting up libjq1:amd64 (1.5+dfsg-2) ...
Processing triggers for libc-bin (2.27-3ubuntu1) ...
Processing triggers for man-db (2.8.3-2ubuntu0.1) ...
Setting up jq (1.5+dfsg-2) ...

# echo ${dbip}
172.17.0.2
postgresに接続
### 事前にパスワードを確認
# grep POSTGRES_PASSWORD env
# psql -h ${dbip} -U postgres template1

### クライアントがない場合はインストール
# apt install postgresql-client-common
Reading package lists... Done
Building dependency tree      
Reading state information... Done
The following NEW packages will be installed:
  postgresql-client-common
0 upgraded, 1 newly installed, 0 to remove and 0 not upgraded.
Need to get 29.5 kB of archives.
After this operation, 177 kB of additional disk space will be used.
Get:1 http://ap-northeast-1.ec2.archive.ubuntu.com/ubuntu bionic/main amd64 postgresql-client-common all 190 [29.5 kB]
Fetched 29.5 kB in 0s (1350 kB/s)            
Selecting previously unselected package postgresql-client-common.
(Reading database ... 110271 files and directories currently installed.)
Preparing to unpack .../postgresql-client-common_190_all.deb ...
Unpacking postgresql-client-common (190) ...
Setting up postgresql-client-common (190) ...
Processing triggers for man-db (2.8.3-2ubuntu0.1) ...
 
# psql -h ${dbip} -U postgres template1
Error: You must install at least one postgresql-client-<version> package
 
# dpkg --get-selections | grep postgresql
postgresql-client-common                        install
 
# apt search postgresql-client
Sorting... Done
Full Text Search... Done
devscripts/bionic-updates,bionic-security 2.17.12ubuntu1.1 amd64
  scripts to make the life of a Debian Package maintainer easier
 
postgresql-client/bionic 10+190 all
  front-end programs for PostgreSQL (supported version)
 
postgresql-client-10/bionic-updates,bionic-security 10.6-0ubuntu0.18.04.1 amd64
  front-end programs for PostgreSQL 10
 
postgresql-client-common/bionic,now 190 all [installed]
  manager for multiple PostgreSQL client versions

# apt install postgresql-client-10
database確認
template1=# SELECT * FROM pg_database;
  datname  | datdba | encoding | datcollate |  datctype  | datistemplate | datallowconn | datconnlimit | datlastsysoid | datfrozenxid | datminmxid | dattablespace |               datacl               
-----------+--------+----------+------------+------------+---------------+--------------+--------------+---------------+--------------+------------+---------------+-------------------------------------
 template1 |     10 |        6 | en_US.utf8 | en_US.utf8 | t             | t            |           -1 |         12371 |          611 |          1 |          1663 | {=c/postgres,postgres=CTc/postgres}
 template0 |     10 |        6 | en_US.utf8 | en_US.utf8 | t             | f            |           -1 |         12371 |          611 |          1 |          1663 | {=c/postgres,postgres=CTc/postgres}
 postgres  |     10 |        6 | en_US.utf8 | en_US.utf8 | f             | t            |           -1 |         12371 |          611 |          1 |          1663 |
(3 rows)
database再作成
template1=# DROP DATABASE IF EXISTS postgres;
DROP DATABASE
template1=# CREATE DATABASE postgres;
CREATE DATABASE
template1=# \q
バックアップデータをコンテナ内にコピー
# ls /tmp/redash_backup.gz
tmp/redash_backup.gz
# docker cp /tmp/redash_backup.gz  ${cid}:/usr/local/redash_backup.gz
リストアコマンド実行
# docker exec -t ${cid} /bin/bash -c 'zcat /usr/local/redash_backup.gz | psql -U postgres -d postgres'
コンテナ停止
# docker stop ${cid}
# docker ps
redash起動
# cd /opt/redash/
# docker-compose up -d
Creating network "redash_default" with the default driver
Creating redash_postgres_1 ... done
Creating redash_redis_1    ... done
Creating redash_scheduler_1        ... done
Creating redash_scheduled_worker_1 ... done
Creating redash_server_1           ... done
Creating redash_adhoc_worker_1     ... done
Creating redash_nginx_1            ... done

参考

kuchitama.hateblo.jp