Skip to content

Commit 434f78c

Browse files
authored
Merge pull request IntersectMBO#1210 from input-output-hk/erikd/misc-20220726
Consolidate 2 PRs
2 parents ff8b787 + 5e53c72 commit 434f78c

File tree

2 files changed

+33
-11
lines changed

2 files changed

+33
-11
lines changed

cardano-db-tool/src/Cardano/DbTool/PrepareSnapshot.hs

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import Ouroboros.Network.Block hiding (blockHash)
1818

1919
import Paths_cardano_db_tool (version)
2020

21+
import System.Info (arch, os)
2122
import System.IO (hFlush, stdout)
2223

2324
newtype PrepareSnapshotArgs = PrepareSnapshotArgs
@@ -93,12 +94,16 @@ runPrepareSnapshotAux firstTry args = do
9394
majorStr = case mMajor of
9495
Nothing -> ""
9596
Just majorV -> "schema-" ++ show majorV
97+
cmdStr = "Create a snapshot with:\n"
98+
++ case os of
99+
"freebsd" -> " cardano-db-sync-pgsql-setup"
100+
_otherwise -> " scripts/postgresql-setup.sh"
101+
++ " --create-snapshot db-sync-snapshot-"
96102
putStrLn $ concat
97-
[ "Create a snapshot with:\n"
98-
, " scripts/postgresql-setup.sh --create-snapshot db-sync-snapshot-"
103+
[ cmdStr
99104
, majorStr
100105
, "-block-"
101106
, show bblockNo
102-
, "-x86_64 "
107+
, "-" ++ arch ++ " "
103108
, fp
104109
]

scripts/postgresql-setup.sh

Lines changed: 25 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,19 @@ IFS=$'\n\t'
88

99
progname="$0"
1010

11+
case "$(uname)" in
12+
Linux)
13+
# Linux tools have long names for these options.
14+
recursive="--recursive"
15+
directory="--directory"
16+
force="--force"
17+
;;
18+
*)
19+
# These shoud work on any POSIX system
20+
recursive="-r"
21+
directory="-d"
22+
force="-f"
23+
esac
1124

1225
function die {
1326
echo "$1"
@@ -134,14 +147,14 @@ function create_snapshot {
134147
tgz_file=$1.tgz
135148
dbfile=$1.sql
136149
ledger_file=$2
137-
tmp_dir=$(mktemp --directory -t db-sync-snapshot-XXXXXXXXXX)
150+
tmp_dir=$(mktemp "${directory}" -t db-sync-snapshot-XXXXXXXXXX)
138151
echo $"Working directory: ${tmp_dir}"
139152
pg_dump --no-owner --schema=public "${PGDATABASE}" > "${tmp_dir}/$1.sql"
140153
cp "$ledger_file" "$tmp_dir/$(basename "${ledger_file}")"
141-
tar zcvf - --directory "${tmp_dir}" "${dbfile}" "$(basename "${ledger_file}")" \
142-
| tee "${tgz_file}.tmp" | sha256sum | sed -e "s/-/${tgz_file}/" > "${tgz_file}.sha256sum"
154+
tar zcvf - --directory "${tmp_dir}" "${dbfile}" "$(basename "${ledger_file}")" | tee "${tgz_file}.tmp" \
155+
| sha256sum | head -c 64 | sed -e "s/$/ ${tgz_file}\n/" > "${tgz_file}.sha256sum"
143156
mv "${tgz_file}.tmp" "${tgz_file}"
144-
rm -rf "${tmp_dir}"
157+
rm "${recursive}" "${force}" "${tmp_dir}"
145158
if test "$(gzip --test "${tgz_file}")" ; then
146159
echo "Gzip reports the snapshot file as being corrupt."
147160
echo "It is not safe to drop the database and restore using this file."
@@ -156,13 +169,13 @@ function restore_snapshot {
156169
echo "Ledger state directory ($2) is not empty. Please empty it and then retry."
157170
exit 1
158171
fi
159-
tmp_dir=$(mktemp --directory -t db-sync-snapshot-XXXXXXXXXX)
172+
tmp_dir=$(mktemp "${directory}" -t db-sync-snapshot-XXXXXXXXXX)
160173
tar -zxvf "$1" --directory "$tmp_dir"
161174
db_file=$(find "$tmp_dir/" -iname "*.sql")
162175
lstate_file=$(find "${tmp_dir}/" -iname "*.lstate")
163176
mv "${lstate_file}" "$2"
164177
psql --dbname="${PGDATABASE}" -f "${db_file}"
165-
rm --recursive "${tmp_dir}"
178+
rm "${recursive}" "${tmp_dir}"
166179
}
167180

168181
function usage_exit {
@@ -252,15 +265,19 @@ case "${1:-""}" in
252265
check_pgpass_file
253266
check_db_exists
254267
if test $# -ne 3 ; then
255-
echo "Expecting exactly 2 more arguments, the snapshot file name template and the ledger state directory."
268+
echo "Expecting exactly 2 more arguments, the snapshot file name template and the ledger state file."
256269
exit 1
257270
fi
258271
if test -z "$2" ; then
259272
echo "Second argument should be the snapshot file name template."
260273
exit 1
261274
fi
262275
if test -z "$3" ; then
263-
echo "Third argument should be the ledger state directory."
276+
echo "Third argument should be the ledger state file."
277+
exit 1
278+
fi
279+
if test -d "$3" ; then
280+
echo "Third argument is a directory and expecting a file."
264281
exit 1
265282
fi
266283
create_snapshot "$2" "$3"

0 commit comments

Comments
 (0)