diff --git a/backup_sqlite.sh b/backup_sqlite.sh new file mode 100644 index 0000000..d2dff60 --- /dev/null +++ b/backup_sqlite.sh @@ -0,0 +1,79 @@ +#!/bin/bash + +# Usage: ./backup_script.sh -l true or ./backup_script.sh -l false + +# Parse command-line argument for local_db flag +while getopts "l:" opt; do + case $opt in + l) + LOCAL_DB=$OPTARG + ;; + *) + echo "Usage: $0 -l [true|false]" + exit 1 + ;; + esac +done + +# Validate LOCAL_DB argument +if [ "$LOCAL_DB" != "true" ] && [ "$LOCAL_DB" != "false" ]; then + echo "Error: Invalid value for -l. Must be 'true' or 'false'." + exit 1 +fi + +# Set the database path and backup destination based on LOCAL_DB value +if [ "$LOCAL_DB" = true ]; then + DB_PATH="/Users/donov/databases/nkode.db" + BACKUP_DIR="/var/tmp" +else + DB_PATH="//remote/path/to/db" + BACKUP_DIR="/remote/path/to/backup/dir" +fi + +BACKUP_NAME="backup_nkode_$(date +'%Y%m%d%H%M%S').tar.gz" + +# Check if the database file exists +if [ ! -f "$DB_PATH" ]; then + echo "Error: Database file not found at $DB_PATH" + exit 1 +fi + +# Perform a WAL checkpoint to flush the WAL file to the main DB file +echo "Checkpointing WAL..." +sqlite3 "$DB_PATH" "PRAGMA wal_checkpoint(FULL);" +if [ $? -ne 0 ]; then + echo "Error: Failed to checkpoint WAL" + exit 1 +fi + +# Check if related files (.wal, .shm) exist +WAL_FILE="${DB_PATH}-wal" +SHM_FILE="${DB_PATH}-shm" + +# Create a list of files to back up (main DB, WAL, and SHM if they exist) +FILES_TO_BACKUP=("$DB_PATH") +if [ -f "$WAL_FILE" ]; then + FILES_TO_BACKUP+=("$WAL_FILE") +fi +if [ -f "$SHM_FILE" ]; then + FILES_TO_BACKUP+=("$SHM_FILE") +fi + +# Create a tar.gz archive of the database and related files +echo "Creating backup..." +tar -czf "$BACKUP_DIR/$BACKUP_NAME" "${FILES_TO_BACKUP[@]}" +if [ $? -eq 0 ]; then + echo "Backup successful: $BACKUP_DIR/$BACKUP_NAME" +else + echo "Error: Backup failed" + exit 1 +fi + +# Upload backup to S3 +aws s3 cp "$BACKUP_DIR/$BACKUP_NAME" s3://nkode-db-backup +if [ $? -eq 0 ]; then + echo "Backup uploaded to S3 successfully." +else + echo "Error: Failed to upload backup to S3." + exit 1 +fi diff --git a/deploy_api.sh b/deploy_api.sh new file mode 100644 index 0000000..4a61e2b --- /dev/null +++ b/deploy_api.sh @@ -0,0 +1,22 @@ +#!/bin/bash + +# Create a temporary directory to hold the files +mkdir -p /tmp/nkodeapi + +cp -r ./core/* /tmp/nkodeapi/ +cp -r ./hashset/* /tmp/nkodeapi/ +cp -r ./py-builtin/* /tmp/nkodeapi/ +cp -r ./util/* /tmp/nkodeapi/ + +cp go.mod /tmp/nkodeapi/ +cp main.go /tmp/nkodeapi/ + + +# Disable extended attributes and create the tar file +export COPYFILE_DISABLE=1 +tar -cvf go-nkode.tar -C /tmp nkodeapi + +# Clean up the temporary directory after the archive is created +rm -rf /tmp/webapp + +scp go-nkode.tar dkelly@api.nkode.tech:/home/dkelly