findb/scripts/dump_schema.sh

47 lines
837 B
Bash
Raw Normal View History

2024-01-03 00:34:39 +00:00
#!/bin/bash
set -e
2024-01-03 00:34:39 +00:00
DB_PATH=$(realpath "$1")
DUMP_PATH=$(realpath "$2")
2024-02-25 23:06:07 +00:00
SQL=$( cat <<SQL
select
m.sql||';
'
from
sqlite_master m
left join sqlite_master ptbl on ptbl.name=m.tbl_name
where
m.sql is not null
order by
m.type='table' desc,
m.type='index' desc,
m.type='trigger' and ptbl.type is not 'view' desc,
m.type='view' desc,
m.type='trigger' desc,
m.name;
SQL
)
2024-01-03 00:34:39 +00:00
MIME=$(file -b --mime-type "$DB_PATH")
if [ "$MIME" = "application/vnd.sqlite3" ]; then
2024-01-03 00:34:39 +00:00
BIN_PATH=sqlite3
else
BIN_PATH=sqlcipher
read -srp "Password: " PASS; echo '';
2024-01-03 00:34:39 +00:00
fi
# dump
$BIN_PATH "$DB_PATH" <<EOF
pragma key='$PASS';
.output '$DUMP_PATH'
$SQL
.output 'stdout'
EOF
# test
TEST_DB_PATH="$DUMP_PATH.test.db"
2024-01-03 00:48:48 +00:00
rm -f "$TEST_DB_PATH"
sqlite3 "$TEST_DB_PATH" < "$DUMP_PATH"
2024-01-03 00:34:39 +00:00
echo 'pragma foreign_key_check;' | sqlite3 "$TEST_DB_PATH"
2024-01-03 00:48:48 +00:00
rm -f "$TEST_DB_PATH"