|
| 1 | +#!/bin/bash |
| 2 | + |
| 3 | +set -e |
| 4 | +set +v |
| 5 | +set +x |
| 6 | + |
| 7 | +Color_Off='\033[0m' # Text Reset |
| 8 | + |
| 9 | +# Regular Colors |
| 10 | +Red='\033[0;31m' # Red |
| 11 | +Green='\033[0;32m' # Green |
| 12 | +Yellow='\033[0;33m' # Yellow |
| 13 | + |
| 14 | + |
| 15 | +echo "Started sync.sh" |
| 16 | + |
| 17 | +# NOTE (clintonb): This is a hack, but it works! |
| 18 | +# I tried lsyncd + rsync, but the changes were never properly reflected on the |
| 19 | +# host without reloading the entire database with `.load`. Also, lsyncd uses inotify |
| 20 | +# events, which don't work with LiteFS + FUSE. This solution works because we |
| 21 | +# execute `.restore` to properly update the DB. |
| 22 | +sync_database() { |
| 23 | + name="$1" |
| 24 | + path="$LITEFS_DB_DIRECTORY/$name" |
| 25 | + |
| 26 | + echo "Attempting to sync $path..." |
| 27 | + |
| 28 | + if test -f $path; then |
| 29 | + sourcePath="$LITEFS_DB_DIRECTORY/$name" |
| 30 | + destPath="/host-data/$name" |
| 31 | + |
| 32 | + stat "$sourcePath" |
| 33 | + |
| 34 | + if test -s $sourcePath; then |
| 35 | + # NOTE: We execute the restore in the container since it is more performant than restoring on a host volume. |
| 36 | + echo -e "${Yellow}Copying+restoring $sourcePath to $destPath...${Color_Off}" |
| 37 | + time sqlite3 $destPath ".restore $sourcePath" |
| 38 | + |
| 39 | + echo -e "${Green}Successfully synced ${name} to host$Color_Off" |
| 40 | + sqlite3 $destPath ".mode table" "SELECT * FROM metadata;" |
| 41 | + sqlite3 $destPath "SELECT COUNT(*) || ' departments' FROM departments;" |
| 42 | + sqlite3 $destPath "SELECT COUNT(*) || ' tax rates' FROM tax_rates;" |
| 43 | + sqlite3 $destPath "SELECT COUNT(*) || ' products' FROM products;" |
| 44 | + sqlite3 $destPath "SELECT COUNT(*) || ' product barcodes' FROM product_barcodes;" |
| 45 | + sqlite3 $destPath "SELECT COUNT(*) || ' promotions' FROM promotions;" |
| 46 | + sqlite3 $destPath "SELECT COUNT(*) || ' offers' FROM offers;" |
| 47 | + sqlite3 $destPath "SELECT COUNT(*) || ' offer benefits' FROM offer_benefits;" |
| 48 | + sqlite3 $destPath "SELECT COUNT(*) || ' offer conditions' FROM offer_conditions;" |
| 49 | + sqlite3 $destPath "SELECT COUNT(*) || ' product ranges' FROM product_ranges;" |
| 50 | + sqlite3 $destPath ".mode table" "ANALYZE; SELECT * FROM sqlite_stat1;" |
| 51 | + else |
| 52 | + echo -e "$Red$sourcePath has not been fully replicated from LiteFS Cloud$Color_Off" |
| 53 | + fi |
| 54 | + else |
| 55 | + echo -e "$Red$path does not yet exist$Color_Off" |
| 56 | + fi |
| 57 | +} |
| 58 | + |
| 59 | +sync_databases() { |
| 60 | + sync_database $DOMAIN_DB_NAME |
| 61 | +} |
| 62 | + |
| 63 | +sync_databases; |
| 64 | + |
| 65 | +# NOTE: We watch the internal data directory because the DB directory uses FUSE, |
| 66 | +# and cannot be easily monitored with fswatch. |
| 67 | +watched_path="$LITEFS_INTERNAL_DATA_DIRECTORY/dbs/$DOMAIN_DB_NAME" |
| 68 | +echo "Running fswatch for $watched_path" |
| 69 | +fswatch -or "$watched_path" | while read f; do echo "Change detected in $f files" && sync_databases; done |
0 commit comments