Skip to content

Commit

Permalink
Add support for importing an existing Postgres database
Browse files Browse the repository at this point in the history
  • Loading branch information
spantaleev committed Jan 1, 2019
1 parent f472c1b commit 6d89319
Show file tree
Hide file tree
Showing 5 changed files with 85 additions and 0 deletions.
24 changes: 24 additions & 0 deletions docs/importing-postgres.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# Importing an existing Postgres database from another installation (optional)

Run this if you'd like to import your database from a previous installation of Matrix Synapse.
(don't forget to import your `media_store` files as well - see [the restoring media store guide](restoring-media-store.md)).


## Prerequisites

For this to work, **the database name in Postgres must match** what this playbook uses.
This playbook uses a Postgres database name of `homeserver` by default (controlled by the `matrix_postgres_db_name` variable).
If your database name differs, be sure to change `matrix_postgres_db_name` to your desired name and to re-run the playbook before proceeding.

The playbook supports importing Postgres dump files in **text** (e.g. `pg_dump > dump.sql`) or **gzipped** formats (e.g. `pg_dump | gzip -c > dump.sql.gz`).

Before doing the actual import, **you need to upload your Postgres dump file to the server**.


## Importing

To import, run this command (make sure to replace `<server-path-to-postgres-dump.sql>` with a file path on your server):

ansible-playbook -i inventory/hosts setup.yml --extra-vars='server_path_postgres_dump=<server-path-to-postgres-dump.sql>' --tags=import-postgres

**Note**: `<server-path-to-postgres-dump.sql>` must be a file path to a Postgres dump file on the server (not on your local machine!).
2 changes: 2 additions & 0 deletions docs/installing.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ After installing, but before starting the services, you may want to do additiona

- [Importing an existing SQLite database (from another installation)](importing-sqlite.md) (optional)

- [Importing an existing Postgres database (from another installation)](importing-postgres.md) (optional)

- [Restoring `media_store` data files from an existing installation](restoring-media-store.md) (optional)


Expand Down
1 change: 1 addition & 0 deletions roles/matrix-server/defaults/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -418,6 +418,7 @@ matrix_ssl_log_dir_path: "{{ matrix_ssl_base_path }}/log"

# Variables to Control which parts of the role run.
run_setup: true
run_import_postgres: true
run_upgrade_postgres: true
run_start: true
run_register_user: true
Expand Down
53 changes: 53 additions & 0 deletions roles/matrix-server/tasks/import_postgres.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
---

# Pre-checks

- name: Fail if playbook called incorrectly
fail: msg="The `server_path_postgres_dump` variable needs to be provided to this playbook, via --extra-vars"
when: "server_path_postgres_dump is not defined or server_path_postgres_dump.startswith('<')"

- name: Check if the provided Postgres dump file exists
stat: path="{{ server_path_postgres_dump }}"
register: result_server_path_postgres_dump_stat

- name: Fail if provided Postgres dump file doesn't exists
fail: msg="File cannot be found on the local machine at {{ server_path_postgres_dump }}"
when: not result_server_path_postgres_dump_stat.stat.exists

- include: tasks/util/detect_existing_postgres_version.yml

- name: Abort, if no existing Postgres version detected
fail: msg="Could not find existing Postgres installation"
when: "not matrix_postgres_detected_existing"


# Defaults

- name: Set postgres_start_wait_time, if not provided
set_fact:
postgres_start_wait_time: 15
when: "postgres_start_wait_time|default('') == ''"


# Actual import work

- name: Ensure matrix-postgres is started
service: name=matrix-postgres state=started daemon_reload=yes

- name: Wait a bit, so that Postgres can start
wait_for:
timeout: "{{ postgres_start_wait_time }}"
delegate_to: 127.0.0.1
become: false

- name: Perform Postgres database import
command: |
/usr/bin/docker run --rm --name matrix-postgres-import \
--network={{ matrix_docker_network }} \
--env-file={{ matrix_environment_variables_data_path }}/env-postgres-pgsql-docker \
-v {{ server_path_postgres_dump }}:{{ server_path_postgres_dump }}:ro \
--entrypoint=/bin/sh
{{ matrix_postgres_docker_image_latest }}
-c 'cat {{ server_path_postgres_dump }} | \
{{ 'gunzip |' if server_path_postgres_dump.endswith('.gz') else '' }}
psql -v ON_ERROR_STOP=1 -h matrix-postgres'
5 changes: 5 additions & 0 deletions roles/matrix-server/tasks/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,11 @@
- include: tasks/setup/main.yml
when: run_setup

- include: tasks/import_postgres.yml
tags:
- import-postgres
when: run_import_postgres

- include: tasks/upgrade_postgres.yml
tags:
- upgrade-postgres
Expand Down

0 comments on commit 6d89319

Please sign in to comment.