implement docker cron job

This commit is contained in:
2024-12-04 13:26:13 -06:00
parent b75d8e06f2
commit 1f6247d3d3
6 changed files with 105 additions and 0 deletions

52
backup_sqlite.sh Normal file
View File

@@ -0,0 +1,52 @@
#!/bin/bash
DB_PATH="/jobs/data/sqlite/nkode.db"
BACKUP_DIR="/var/tmp"
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