|
| 1 | +#!/bin/bash |
| 2 | + |
| 3 | +# This script copies a synced database to the host and emits an Otel signal. |
| 4 | + |
| 5 | +set -e |
| 6 | +set +v |
| 7 | +set +x |
| 8 | + |
| 9 | +# OpenTelemetry setup |
| 10 | +export OTEL_EXPORTER_OTEL_ENDPOINT="http://host.docker.internal:4318" |
| 11 | +export OTEL_SH_LIB_PATH="opentelemetry-shell/library" |
| 12 | +# shellcheck disable=SC2034 |
| 13 | +service_version=$OTEL_SERVICE_VERSION |
| 14 | +. opentelemetry-shell/library/otel_traces.sh |
| 15 | + |
| 16 | +Normal='\033[0m' |
| 17 | +Red='\033[0;31m' |
| 18 | +Green='\033[0;32m' |
| 19 | +Yellow='\033[0;33m' |
| 20 | + |
| 21 | +name="$1" |
| 22 | +path="$LITEFS_DB_DIRECTORY/$name" |
| 23 | + |
| 24 | +echo "Attempting to copy $path..." |
| 25 | + |
| 26 | +if test -f "$path"; then |
| 27 | + sourcePath="$LITEFS_DB_DIRECTORY/$name" |
| 28 | + destPath="/host-data/$(date +"%s")-$name" |
| 29 | + |
| 30 | + stat "$sourcePath" |
| 31 | + |
| 32 | + if test -s "$sourcePath"; then |
| 33 | + # NOTE: We execute the restore in the container since it is more performant than restoring on a host volume. |
| 34 | + echo -e "${Yellow}Copying $sourcePath to $destPath...${Normal}" |
| 35 | + |
| 36 | + # Create an Otel span |
| 37 | + generatedAt=$(sqlite3 "$sourcePath" "SELECT generated_at FROM metadata;") |
| 38 | + schemaVersion=$(sqlite3 "$sourcePath" "SELECT schema_version FROM metadata;") |
| 39 | + # shellcheck disable=SC2034 |
| 40 | + span_name="Copy domain data to host" |
| 41 | + # shellcheck disable=SC2034 |
| 42 | + custom_resource_attributes=( |
| 43 | + "domain_data_generated_at:$generatedAt" |
| 44 | + "domain_data_schema_version:$schemaVersion" |
| 45 | + ) |
| 46 | + linkedTraceId=$(sqlite3 "$sourcePath" "SELECT linked_trace_id FROM metadata;") |
| 47 | + linkedTraceState=$(sqlite3 "$sourcePath" "SELECT linked_trace_state FROM metadata;") |
| 48 | + linkedSpanId=$(sqlite3 "$sourcePath" "SELECT linked_span_id FROM metadata;") |
| 49 | + echo "[Linked Span Data] linkedTraceId: $linkedTraceId, linkedTraceState: $linkedTraceState, linkedSpanId: $linkedSpanId" |
| 50 | + # shellcheck disable=SC2034 |
| 51 | + linked_span=("$linkedTraceId" "$linkedSpanId" "$linkedTraceState") |
| 52 | + otel_trace_start_parent_span cp "$sourcePath" "$destPath" |
| 53 | + |
| 54 | + echo -e "${Green}Successfully copied ${name} to ${destPath}$Normal" |
| 55 | + sqlite3 "$destPath" ".mode table" "SELECT * FROM metadata;" |
| 56 | + sqlite3 "$destPath" "SELECT COUNT(*) || ' departments' FROM departments;" |
| 57 | + sqlite3 "$destPath" "SELECT COUNT(*) || ' tax rates' FROM tax_rates;" |
| 58 | + sqlite3 "$destPath" "SELECT COUNT(*) || ' products' FROM products;" |
| 59 | + sqlite3 "$destPath" "SELECT COUNT(*) || ' product barcodes' FROM product_barcodes;" |
| 60 | + sqlite3 "$destPath" "SELECT COUNT(*) || ' promotions' FROM promotions;" |
| 61 | + sqlite3 "$destPath" "SELECT COUNT(*) || ' offers' FROM offers;" |
| 62 | + sqlite3 "$destPath" "SELECT COUNT(*) || ' offer benefits' FROM offer_benefits;" |
| 63 | + sqlite3 "$destPath" "SELECT COUNT(*) || ' offer conditions' FROM offer_conditions;" |
| 64 | + sqlite3 "$destPath" "SELECT COUNT(*) || ' product ranges' FROM product_ranges;" |
| 65 | + sqlite3 "$destPath" ".mode table" "ANALYZE; SELECT * FROM sqlite_stat1;" |
| 66 | + |
| 67 | + echo -e "${Yellow}Deleting all but the latest ${FILE_RETENTION_COUNT} files from /host-data$Normal" |
| 68 | + tailNum=$(( FILE_RETENTION_COUNT + 1 )) |
| 69 | + cd /host-data |
| 70 | + # Source: https://stackoverflow.com/a/34862475/592820 |
| 71 | + ls -tp | grep -v '/$' | tail -n +${tailNum} | xargs -I {} rm -- {} |
| 72 | + else |
| 73 | + echo -e "$Red$sourcePath has not been fully replicated from LiteFS Cloud$Normal" |
| 74 | + fi |
| 75 | +else |
| 76 | + echo -e "$Red$path does not yet exist$Normal" |
| 77 | +fi |
0 commit comments