Skip to content

Commit 81e2c5e

Browse files
wip: docker in create snapshot script
1 parent 0d66a00 commit 81e2c5e

File tree

1 file changed

+32
-16
lines changed

1 file changed

+32
-16
lines changed

scripts/create_zfs_snapshot.sh

Lines changed: 32 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,20 @@ pre="_pre"
1111
ntries=1000
1212
now=$(date +%Y%m%d%H%M%S)
1313

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+
1421
# Storage configuration.
1522
# Name of the ZFS pool which contains PGDATA.
1623
zfs_pool=${ZFS_POOL:-"dblab_pool"}
1724
# Sudirectory in which PGDATA is located.
1825
pgdata_subdir=${PGDATA_SUBDIR:-""}
1926

2027
# Clone configuration.
21-
# Mount directory for DB Lab clones.
22-
mount_dir=${MOUNT_DIR:-"/var/lib/dblab/clones"}
2328
# Name of a clone which will be created and used for PGDATA manipulation.
2429
clone_name="clone${pre}_${now}"
2530
# Full name of the clone for ZFS commands.
@@ -29,26 +34,31 @@ clone_dir="${mount_dir}/${clone_name}"
2934
# Directory of PGDATA in the clone mount.
3035
clone_pgdata_dir="${clone_dir}${pgdata_subdir}"
3136

32-
# Postgres configuration.
37+
# Postgres in Docker configuration.
3338
# Port on which Postgres will be started using clone's PGDATA.
34-
clone_port=${CLONE_PORT:-6999}
3539
pg_bin_dir=${PG_BIN_DIR:-"/usr/lib/postgresql/12/bin"}
3640
pg_sock_dir=${PG_SOCK_DIR:-"/var/run/postgresql"}
37-
pg_username=${PGUSERNAME:-"postgres"}
3841
# Set password with PGPASSWORD env.
3942
pg_db=${PGDB:-"postgres"}
4043
sudo_cmd=${SUDO_CMD:-""} # Use `sudo -u postgres` for default environment
4144

45+
clone_label="dblab-sync-clone"
46+
4247
# Snapshot.
4348
# Name of resulting snapshot after PGDATA manipulation.
4449
snapshot_name="snapshot_${now}"
4550

51+
ids=$(docker images ${docker_image} -q)
52+
if [ -z "${ids}" ]; then
53+
${sudo_cmd} docker pull ${docker_image}
54+
fi
55+
4656
# TODO: decide: do we need to stop the shadow Postgres instance?
4757
# OR: we can tell the shadow Postgres: select pg_start_backup('database-lab-snapshot');
4858
# .. and in the very end: select pg_stop_backup();
4959

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}
5262

5363
cd /tmp # To avoid errors about lack of permissions.
5464

@@ -102,26 +112,34 @@ fi;
102112
103113
### pg_hba.conf
104114
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
106115
107116
### pg_ident.conf
108117
echo "" > ${clone_pgdata_dir}/pg_ident.conf
109118
SH
110119

111120
${sudo_cmd} ${pg_bin_dir}/pg_ctl \
112121
-D "${clone_pgdata_dir}" \
113-
-o "-p ${clone_port} -c 'shared_buffers=4096'" \
122+
-o "-c 'shared_buffers=4096'" \
114123
-W \
115124
start
116125

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+
117135
# 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...
119137
# During that period, we will have "FATAL: the database system is starting up".
120138
# Alternatively, we could use pg_ctl's "-w" option above (instead of manual checking).
121139

122140
failed=true
123141
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
125143
failed=false
126144
break
127145
fi
@@ -144,8 +162,6 @@ if [[ ! -z ${DATA_STATE_AT+x} ]]; then
144162
else
145163
data_state_at=$(${pg_bin_dir}/psql \
146164
--set ON_ERROR_STOP=on \
147-
-p ${clone_port} \
148-
-U ${pg_username} \
149165
-d ${pg_db} \
150166
-h ${pg_sock_dir} \
151167
-XAt \
@@ -157,7 +173,7 @@ ${sudo_cmd} ${pg_bin_dir}/pg_ctl -D ${clone_pgdata_dir} -W promote
157173

158174
failed=true
159175
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
161177
failed=false
162178
break
163179
fi
@@ -176,8 +192,8 @@ ${sudo_cmd} ${pg_bin_dir}/pg_ctl -D ${clone_pgdata_dir} -w stop
176192

177193
${sudo_cmd} rm -rf ${clone_pgdata_dir}/pg_log
178194

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}
181197

182198
# Snapshot "datastore/postgresql/db_state_1_pre@db_state_1" is ready and can be used for thin provisioning
183199

0 commit comments

Comments
 (0)