diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..485dee6 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +.idea diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..b345c12 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,39 @@ +# Use Ubuntu 20.04 as the base image +FROM ubuntu:20.04 + +# Install dependencies +RUN apt-get update && apt-get install -y \ + sqlite3 \ + awscli \ + cron \ + tar \ + gzip \ + && rm -rf /var/lib/apt/lists/* + +# Create necessary directories +RUN mkdir -p /jobs/data/sqlite +RUN mkdir -p /var/tmp + +# Copy the backup script into the container +COPY backup_sqlite.sh /usr/local/bin/backup_script.sh + +# Make the script executable +RUN chmod +x /usr/local/bin/backup_script.sh + +# Create the cron log file +RUN touch /var/log/cron.log + +# Copy the cron job file into the cron.d directory +COPY backup_cron /etc/cron.d/backup_cron + +# Set permissions for the cron job file +RUN chmod 0644 /etc/cron.d/backup_cron + +# Apply the cron job +RUN crontab /etc/cron.d/backup_cron + +# Expose the cron log file as a volume +VOLUME /var/log + +# Start cron in the foreground when the container starts +CMD ["cron", "-f"] diff --git a/backup_cron b/backup_cron new file mode 100644 index 0000000..26e8e1a --- /dev/null +++ b/backup_cron @@ -0,0 +1 @@ +0 2 * * * root /usr/local/bin/backup_script.sh -l true >> /var/log/cron.log 2>&1 diff --git a/backup_sqlite.sh b/backup_sqlite.sh new file mode 100644 index 0000000..dec2de8 --- /dev/null +++ b/backup_sqlite.sh @@ -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 diff --git a/docker-compose.yaml b/docker-compose.yaml new file mode 100644 index 0000000..270f819 --- /dev/null +++ b/docker-compose.yaml @@ -0,0 +1,11 @@ +services: + go-nkode: + container_name: cron-nkode + image: registry.infra.nkode.tech/cron-nkode + volumes: + - /var/go-nkode/sqlite:/jobs/data/sqlite + + environment: + - AWS_ACCESS_KEY_ID=${AWS_ACCESS_KEY_ID} + - AWS_SECRET_ACCESS_KEY=${AWS_SECRET_ACCESS_KEY} + - AWS_REGION=${AWS_REGION} diff --git a/docker_build.sh b/docker_build.sh new file mode 100644 index 0000000..5886794 --- /dev/null +++ b/docker_build.sh @@ -0,0 +1 @@ +docker buildx build --platform linux/amd64,linux/arm64 -t registry.infra.nkode.tech/cron-nkode:latest --push .