Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit2a2a5f2

Browse files
committed
Merge pull requestalphagov#134 from alphagov/add-postgres-sync-command
Add a postgres sync command
2 parentsc59bedf +fff1ea8 commit2a2a5f2

File tree

3 files changed

+29
-0
lines changed

3 files changed

+29
-0
lines changed

‎README.md‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,3 +73,15 @@ and ensure that you do not have the following option anywhere in your
7373
`~/.ssh/config`:
7474

7575
ForwardAgent yes
76+
77+
##Syncing postgres machines
78+
79+
An example
80+
81+
`fab <env> -H '<src_db>' postgresql.sync:<db_name>,<dst_db> -A`
82+
83+
the -A must be specified to forward the agent
84+
85+
This will sync the specified database`<db_name>` from the machine with the
86+
hostname of`<src_db>` to the machine with hostaname`<dst_db>`. It will destroy
87+
data on the destination db

‎fabfile.py‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
importnagios
3131
importnginx
3232
importntp
33+
importpostgresql
3334
importpuppet
3435
importrabbitmq
3536
importrkhunter

‎postgresql.py‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
fromfabric.apiimporttask,sudo,env,settings,run
2+
fromfabric.utilsimportabort
3+
4+
@task
5+
defsync(database,dest_machine):
6+
# the agent forwarding is required for the ssh command to the destination
7+
# postgres server to work - this saves copying a temporary file down and
8+
# back up again. We cant use fabrics sudo context manager because the
9+
# command after the pipe should not run as the postgres user.
10+
# The combination of "--schema public" and "--clean" in the pg_restore
11+
# command clears all existing tables in the public schema before restoring
12+
# the tables and data
13+
withsettings(forward_agent=True):
14+
run('sudo -iupostgres pg_dump -Fc {0} | ssh {1} '
15+
'"sudo -upostgres pg_restore --clean --single-transaction '
16+
'--schema public --dbname {0}"'.format(database,dest_machine))

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp