@@ -98,7 +98,7 @@ function check_connect_as_user {
98
98
99
99
function check_db_exists {
100
100
set +e
101
- count=$( psql -l " ${PGDATABASE} " | grep -c " ${PGDATABASE} " )
101
+ count=$( psql -t -l --csv " ${PGDATABASE} " | grep -c " ^ ${PGDATABASE} , " )
102
102
if test " ${count} " -lt 1 ; then
103
103
echo
104
104
echo " Error : No '${PGDATABASE} ' database."
@@ -108,7 +108,7 @@ function check_db_exists {
108
108
echo
109
109
exit 1
110
110
fi
111
- count=$( psql -l " ${PGDATABASE} " | grep " ${PGDATABASE} " | cut -d \| -f 3 | grep -c UTF8)
111
+ count=$( psql -t -l --csv " ${PGDATABASE} " | grep " ^ ${PGDATABASE} , " | grep -c UTF8)
112
112
if test " ${count} " -ne 1 ; then
113
113
echo
114
114
echo " Error : '${PGDATABASE} ' database exists, but is not UTF8."
@@ -164,19 +164,20 @@ function create_snapshot {
164
164
tmp_dir=$( mktemp " ${directory} " -t db-sync-snapshot-XXXXXXXXXX)
165
165
echo $" Working directory: ${tmp_dir} "
166
166
pg_dump --no-owner --schema=public --jobs=" ${numcores} " " ${PGDATABASE} " --format=directory --file=" ${tmp_dir} /db/"
167
- lstate_gz_file=$( basename " ${ledger_file} " ) .gz
168
- gzip --to-stdout " ${ledger_file} " > " ${tmp_dir} /$( basename " ${ledger_file} " ) .gz"
169
- tree " ${tmp_dir} "
167
+ if [ -n " ${ledger_file} " ]; then
168
+ lstate_gz_file=$( basename " ${ledger_file} " ) .gz
169
+ gzip --to-stdout " ${ledger_file} " > " ${tmp_dir} /${lstate_gz_file} "
170
+ fi
170
171
# Use plain tar here because the database dump files and the ledger state file are already gzipped.
171
- tar cvf - --directory " ${tmp_dir} " " db" " ${ lstate_gz_file}" | tee " ${tgz_file} .tmp" \
172
- | sha256sum | head -c 64 | sed -e " s/$/ ${tgz_file} \n/" > " ${tgz_file} .sha256sum"
172
+ tar cvf - --directory " ${tmp_dir} " " db" $( [ -f " / ${tmp_dir} / ${ lstate_gz_file}" ] && echo " ${lstate_gz_file} " ) | tee " ${tgz_file} .tmp" | \
173
+ sha256sum | head -c 64 | sed -e " s/$/ ${tgz_file} \n/" > " ${tgz_file} .sha256sum"
173
174
mv " ${tgz_file} .tmp" " ${tgz_file} "
174
175
rm " ${recursive} " " ${force} " " ${tmp_dir} "
175
176
if test " $( tar " ${test} " --file " ${tgz_file} " ) " ; then
176
177
echo " Tar reports the snapshot file as being corrupt."
177
178
echo " It is not safe to drop the database and restore using this file."
178
179
exit 1
179
- fi
180
+ fi
180
181
echo " Created ${tgz_file} + .sha256sum"
181
182
}
182
183
@@ -185,24 +186,25 @@ function restore_snapshot {
185
186
if test " ${file_count} " -gt 0 ; then
186
187
echo " Ledger state directory ($2 ) is not empty. Please empty it and then retry."
187
188
exit 1
188
- fi
189
+ fi
189
190
tmp_dir=$( mktemp " ${directory} " -t db-sync-snapshot-XXXXXXXXXX)
190
191
tar xvf " $1 " --directory " $tmp_dir "
191
192
if test -d " ${tmp_dir} /db/" ; then
192
193
# New pg_dump format
193
- lstate_gz_file=$( find " ${tmp_dir} /" -iname " *.lstate.gz" )
194
- lstate_file=$( basename " ${lstate_gz_file} " | sed ' s/.gz$//' )
195
- gunzip --to-stdout " ${lstate_gz_file} " > " $2 /${lstate_file} "
194
+ if [ -n " ${lstate_gz_file} " ] ; then
195
+ lstate_file=$( basename " ${lstate_gz_file} " | sed ' s/.gz$//' )
196
+ gunzip --to-stdout " ${lstate_gz_file} " > " $2 /${lstate_file} "
197
+ fi
196
198
197
- # Important: specify --schema=public below to skip over `create schema public`
198
- # statement generated by pg_dump
199
+ # Important: specify --schema=public below to skip over `create schema public`
200
+ # statement generated by pg_dump
199
201
pg_restore \
200
202
--schema=public \
201
203
--jobs=" ${numcores} " \
202
204
--format=directory \
203
205
--dbname=" ${PGDATABASE} " \
204
- --no-owner \
205
- -- exit-on-error \
206
+ --no-owner \
207
+ $( [ -z " ${SKIP_RESTORE_ERROR} " ] && echo " -- exit-on-error" ) \
206
208
" ${tmp_dir} /db/"
207
209
else
208
210
# Old snapshot format produced by this script
@@ -228,7 +230,7 @@ function usage_exit {
228
230
echo " $progname --dump-schema - Dump the schema of the database."
229
231
echo
230
232
echo " - Create a db-sync state snapshot"
231
- echo " $progname --create-snapshot <snapshot-file> <ledger-state-file>"
233
+ echo " $progname --create-snapshot <snapshot-file> [ <ledger-state-file>] "
232
234
echo
233
235
echo " - Restore a db-sync state snapshot."
234
236
echo " $progname --restore-snapshot <snapshot-file> <ledger-state-dir>"
@@ -300,22 +302,22 @@ case "${1:-""}" in
300
302
--create-snapshot)
301
303
check_pgpass_file
302
304
check_db_exists
303
- if test $# -ne 3 ; then
304
- echo " Expecting exactly 2 more arguments, the snapshot file name template and the ledger state file."
305
+ if test $# -lt 2 ; then
306
+ echo " Expecting the snapshot file name (without extension) and optionally the ledger state file as arguments ."
305
307
exit 1
306
- fi
308
+ fi
307
309
if test -z " $2 " ; then
308
- echo " Second argument should be the snapshot file name template ."
310
+ echo " Second argument should be the snapshot file name (without extension) ."
309
311
exit 1
310
- fi
312
+ fi
311
313
if test -z " $3 " ; then
312
314
echo " Third argument should be the ledger state file."
313
315
exit 1
314
- fi
315
- if test -d " $3 " ; then
316
- echo " Third argument is a directory and expecting a file."
316
+ fi
317
+ if test -n " $3 " && test - d " $3 " ; then
318
+ echo " Third argument provided is a directory but expecting a file."
317
319
exit 1
318
- fi
320
+ fi
319
321
create_snapshot " $2 " " $3 "
320
322
;;
321
323
--restore-snapshot)
0 commit comments