@@ -11,15 +11,20 @@ pre="_pre"
11
11
ntries=1000
12
12
now=$( date +%Y%m%d%H%M%S)
13
13
14
+ # Clones data mount directory.
15
+ mount_dir=${MOUNT_DIR:- " /var/lib/dblab/clones" }
16
+ # Unix sockets directory for secure connection to the clone.
17
+ unix_socket_dir=${UNIX_SOCKET_DIR:- " /var/lib/dblab/sockets" }
18
+ # Sync clone Docker image.
19
+ docker_image=${DOCKER_IMAGE:- " postgres:12-alpine" }
20
+
14
21
# Storage configuration.
15
22
# Name of the ZFS pool which contains PGDATA.
16
23
zfs_pool=${ZFS_POOL:- " dblab_pool" }
17
24
# Sudirectory in which PGDATA is located.
18
25
pgdata_subdir=${PGDATA_SUBDIR:- " " }
19
26
20
27
# Clone configuration.
21
- # Mount directory for DB Lab clones.
22
- mount_dir=${MOUNT_DIR:- " /var/lib/dblab/clones" }
23
28
# Name of a clone which will be created and used for PGDATA manipulation.
24
29
clone_name=" clone${pre} _${now} "
25
30
# Full name of the clone for ZFS commands.
@@ -29,26 +34,31 @@ clone_dir="${mount_dir}/${clone_name}"
29
34
# Directory of PGDATA in the clone mount.
30
35
clone_pgdata_dir=" ${clone_dir}${pgdata_subdir} "
31
36
32
- # Postgres configuration.
37
+ # Postgres in Docker configuration.
33
38
# Port on which Postgres will be started using clone's PGDATA.
34
- clone_port=${CLONE_PORT:- 6999}
35
39
pg_bin_dir=${PG_BIN_DIR:- " /usr/lib/postgresql/12/bin" }
36
40
pg_sock_dir=${PG_SOCK_DIR:- " /var/run/postgresql" }
37
- pg_username=${PGUSERNAME:- " postgres" }
38
41
# Set password with PGPASSWORD env.
39
42
pg_db=${PGDB:- " postgres" }
40
43
sudo_cmd=${SUDO_CMD:- " " } # Use `sudo -u postgres` for default environment
41
44
45
+ clone_label=" dblab-sync-clone"
46
+
42
47
# Snapshot.
43
48
# Name of resulting snapshot after PGDATA manipulation.
44
49
snapshot_name=" snapshot_${now} "
45
50
51
+ ids=$( docker images ${docker_image} -q)
52
+ if [ -z " ${ids} " ]; then
53
+ ${sudo_cmd} docker pull ${docker_image}
54
+ fi
55
+
46
56
# TODO: decide: do we need to stop the shadow Postgres instance?
47
57
# OR: we can tell the shadow Postgres: select pg_start_backup('database-lab-snapshot');
48
58
# .. and in the very end: select pg_stop_backup();
49
59
50
- sudo zfs snapshot ${zfs_pool} @${snapshot_name}${pre}
51
- sudo zfs clone ${ zfs_pool} @${snapshot_name}${pre} ${clone_full_name} -o mountpoint= ${clone_dir }
60
+ ${sudo_cmd} zfs snapshot ${zfs_pool} @${snapshot_name}${pre}
61
+ ${sudo_cmd} zfs clone -o mountpoint= ${clone_dir} ${ zfs_pool} @${snapshot_name}${pre} ${clone_full_name}
52
62
53
63
cd /tmp # To avoid errors about lack of permissions.
54
64
@@ -102,26 +112,34 @@ fi;
102
112
103
113
### pg_hba.conf
104
114
echo "local all all trust" > ${clone_pgdata_dir} /pg_hba.conf
105
- echo "host all all 0.0.0.0/0 md5" >> ${clone_pgdata_dir} /pg_hba.conf
106
115
107
116
### pg_ident.conf
108
117
echo "" > ${clone_pgdata_dir} /pg_ident.conf
109
118
SH
110
119
111
120
${sudo_cmd} ${pg_bin_dir} /pg_ctl \
112
121
-D " ${clone_pgdata_dir} " \
113
- -o " -p ${clone_port} - c 'shared_buffers=4096'" \
122
+ -o " -c 'shared_buffers=4096'" \
114
123
-W \
115
124
start
116
125
126
+ ${sudo_cmd} docker run \
127
+ --name ${clone_label} \
128
+ --detach \
129
+ --env PGDATA=/var/lib/postgresql/pgdata \
130
+ --volume " + c.Datadir + " :/var/lib/postgresql/pgdata " \
131
+ --volume " + c.UnixSocketCloneDir + " :/var/run/postgresql " \
132
+ --label ${clone_label} \
133
+ docker_image
134
+
117
135
# Now we are going to wait until we can connect to the server.
118
- # If it was a replica, it may take a while..
136
+ # If it was a replica, it may take a while...
119
137
# During that period, we will have "FATAL: the database system is starting up".
120
138
# Alternatively, we could use pg_ctl's "-w" option above (instead of manual checking).
121
139
122
140
failed=true
123
141
for i in {1..1000}; do
124
- if [[ $( ${pg_bin_dir} /psql -p $clone_port -U ${pg_username} - d ${pg_db} -h ${pg_sock_dir} -XAtc ' select pg_is_in_recovery()' ) == " t" ]]; then
142
+ if [[ $( ${pg_bin_dir} /psql -d ${pg_db} -h ${pg_sock_dir} -XAtc ' select pg_is_in_recovery()' ) == " t" ]]; then
125
143
failed=false
126
144
break
127
145
fi
@@ -144,8 +162,6 @@ if [[ ! -z ${DATA_STATE_AT+x} ]]; then
144
162
else
145
163
data_state_at=$( ${pg_bin_dir} /psql \
146
164
--set ON_ERROR_STOP=on \
147
- -p ${clone_port} \
148
- -U ${pg_username} \
149
165
-d ${pg_db} \
150
166
-h ${pg_sock_dir} \
151
167
-XAt \
@@ -157,7 +173,7 @@ ${sudo_cmd} ${pg_bin_dir}/pg_ctl -D ${clone_pgdata_dir} -W promote
157
173
158
174
failed=true
159
175
for i in {1..1000}; do
160
- if [[ $( ${pg_bin_dir} /psql -p ${clone_port} -U ${pg_username} - d ${pg_db} -h ${pg_sock_dir} -XAtc ' select pg_is_in_recovery()' ) == " f" ]]; then
176
+ if [[ $( ${pg_bin_dir} /psql -d ${pg_db} -h ${pg_sock_dir} -XAtc ' select pg_is_in_recovery()' ) == " f" ]]; then
161
177
failed=false
162
178
break
163
179
fi
@@ -176,8 +192,8 @@ ${sudo_cmd} ${pg_bin_dir}/pg_ctl -D ${clone_pgdata_dir} -w stop
176
192
177
193
${sudo_cmd} rm -rf ${clone_pgdata_dir} /pg_log
178
194
179
- sudo zfs snapshot ${clone_full_name} @${snapshot_name}
180
- sudo zfs set dblab:datastateat=" ${data_state_at} " ${clone_full_name} @${snapshot_name}
195
+ ${sudo_cmd} zfs snapshot ${clone_full_name} @${snapshot_name}
196
+ ${sudo_cmd} zfs set dblab:datastateat=" ${data_state_at} " ${clone_full_name} @${snapshot_name}
181
197
182
198
# Snapshot "datastore/postgresql/db_state_1_pre@db_state_1" is ready and can be used for thin provisioning
183
199
0 commit comments