@@ -98,7 +98,7 @@ function check_connect_as_user {
9898
9999function check_db_exists {
100100 set +e
101- count=$( psql -l " ${PGDATABASE} " | grep -c " ${PGDATABASE} " )
101+ count=$( psql -t -l --csv " ${PGDATABASE} " | grep -c " ^ ${PGDATABASE} , " )
102102 if test " ${count} " -lt 1 ; then
103103 echo
104104 echo " Error : No '${PGDATABASE} ' database."
@@ -108,7 +108,7 @@ function check_db_exists {
108108 echo
109109 exit 1
110110 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)
112112 if test " ${count} " -ne 1 ; then
113113 echo
114114 echo " Error : '${PGDATABASE} ' database exists, but is not UTF8."
@@ -164,19 +164,20 @@ function create_snapshot {
164164 tmp_dir=$( mktemp " ${directory} " -t db-sync-snapshot-XXXXXXXXXX)
165165 echo $" Working directory: ${tmp_dir} "
166166 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
170171 # 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"
173174 mv " ${tgz_file} .tmp" " ${tgz_file} "
174175 rm " ${recursive} " " ${force} " " ${tmp_dir} "
175176 if test " $( tar " ${test} " --file " ${tgz_file} " ) " ; then
176177 echo " Tar reports the snapshot file as being corrupt."
177178 echo " It is not safe to drop the database and restore using this file."
178179 exit 1
179- fi
180+ fi
180181 echo " Created ${tgz_file} + .sha256sum"
181182}
182183
@@ -185,24 +186,25 @@ function restore_snapshot {
185186 if test " ${file_count} " -gt 0 ; then
186187 echo " Ledger state directory ($2 ) is not empty. Please empty it and then retry."
187188 exit 1
188- fi
189+ fi
189190 tmp_dir=$( mktemp " ${directory} " -t db-sync-snapshot-XXXXXXXXXX)
190191 tar xvf " $1 " --directory " $tmp_dir "
191192 if test -d " ${tmp_dir} /db/" ; then
192193 # 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
196198
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
199201 pg_restore \
200202 --schema=public \
201203 --jobs=" ${numcores} " \
202204 --format=directory \
203205 --dbname=" ${PGDATABASE} " \
204- --no-owner \
205- -- exit-on-error \
206+ --no-owner \
207+ $( [ -z " ${SKIP_RESTORE_ERROR} " ] && echo " -- exit-on-error" ) \
206208 " ${tmp_dir} /db/"
207209 else
208210 # Old snapshot format produced by this script
@@ -228,7 +230,7 @@ function usage_exit {
228230 echo " $progname --dump-schema - Dump the schema of the database."
229231 echo
230232 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>] "
232234 echo
233235 echo " - Restore a db-sync state snapshot."
234236 echo " $progname --restore-snapshot <snapshot-file> <ledger-state-dir>"
@@ -300,22 +302,22 @@ case "${1:-""}" in
300302 --create-snapshot)
301303 check_pgpass_file
302304 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 ."
305307 exit 1
306- fi
308+ fi
307309 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) ."
309311 exit 1
310- fi
312+ fi
311313 if test -z " $3 " ; then
312314 echo " Third argument should be the ledger state file."
313315 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."
317319 exit 1
318- fi
320+ fi
319321 create_snapshot " $2 " " $3 "
320322 ;;
321323 --restore-snapshot)
0 commit comments