Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(memory-store): Add backup cronjob #470

Merged
merged 1 commit into from
Aug 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 7 additions & 8 deletions memory-store/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
# Then copy the run.sh script to the ./run.sh file

# First stage: Build the Rust project
FROM rust:1.80.1-bookworm as builder
FROM rust:1.80.1-bookworm AS builder

# Install liburing-dev
RUN apt-get update && apt-get install -y liburing-dev
Expand Down Expand Up @@ -32,26 +32,25 @@ FROM debian:bookworm-slim
# Install dependencies
RUN \
apt-get update -yqq && \
apt-get install -y ca-certificates tini nfs-common nfs-kernel-server procps netbase && \
apt-get install -y liburing-dev curl ca-certificates tini nfs-common nfs-kernel-server procps netbase && \
apt-get clean && \
rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/*

# Set fallback mount directory
ENV MNT_DIR /data

ENV APP_HOME /app
ENV MNT_DIR=/data APP_HOME=/app BACKUP_DIR=/backup
WORKDIR $APP_HOME

# Copy the cozo binary
COPY --from=builder /usr/local/bin/cozo $APP_HOME/bin/cozo-bin
COPY --from=builder /usr/local/bin/cozo $APP_HOME/bin/cozo

# Copy local code to the container image.
COPY ./run.sh ./run.sh
COPY ./backup.sh ./backup.sh

# Ensure the script is executable
RUN \
mkdir -p $MNT_DIR && \
chmod +x $APP_HOME/bin/cozo-bin && \
mkdir -p $MNT_DIR $BACKUP_DIR && \
chmod +x $APP_HOME/bin/cozo && \
chmod +x $APP_HOME/run.sh

# Use tini to manage zombie processes and signal forwarding
Expand Down
37 changes: 37 additions & 0 deletions memory-store/backup.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
#!/usr/bin/env bash

set -eo pipefail # Exit on error
set -u # Exit on undefined variable

# Ensure environment variables are set
if [ -z "$COZO_PORT" ] || [ -z "$COZO_AUTH_TOKEN" ]; then
echo "COZO_PORT or COZO_AUTH_TOKEN is not set"
exit 1
fi

COZO_BACKUP_DIR=${COZO_BACKUP_DIR:-/backup}
TIMESTAMP=$(date +%Y-%m-%d_%H-%M-%S)
MAX_BACKUPS=${MAX_BACKUPS:-10}

curl -X POST \
http://0.0.0.0:$COZO_PORT/backup \
-H 'Content-Type: application/json' \
-H "X-Cozo-Auth: ${COZO_AUTH_TOKEN}" \
-d "{\"path\": \"${COZO_BACKUP_DIR}/cozo-backup-${TIMESTAMP}.bk\"}" \
-w "\nStatus: %{http_code}\nResponse:\n" \
-o /dev/stdout

# Print the number of backups
echo "Number of backups: $(ls -l ${COZO_BACKUP_DIR} | grep -c "cozo-backup-")"

# If the backup is successful, remove the oldest backup if the number of backups exceeds MAX_BACKUPS
if [ $(ls -l ${COZO_BACKUP_DIR} | grep -c "cozo-backup-") -gt $MAX_BACKUPS ]; then
oldest_backup=$(ls -t ${COZO_BACKUP_DIR}/cozo-backup-*.bk | tail -n 1)

if [ -n "$oldest_backup" ]; then
rm "$oldest_backup"
echo "Removed oldest backup: $oldest_backup"
else
echo "No backups found to remove"
fi
fi
49 changes: 0 additions & 49 deletions memory-store/backup/backup.py

This file was deleted.

203 changes: 0 additions & 203 deletions memory-store/backup/poetry.lock

This file was deleted.

16 changes: 0 additions & 16 deletions memory-store/backup/pyproject.toml

This file was deleted.

8 changes: 0 additions & 8 deletions memory-store/deploy.sh

This file was deleted.

17 changes: 16 additions & 1 deletion memory-store/docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ services:
- COZO_AUTH_TOKEN=${COZO_AUTH_TOKEN}
- COZO_PORT=${COZO_PORT}
- MNT_DIR=/data
- COZO_BACKUP_DIR=/backup
container_name: memory-store
volumes:
- cozo_data:/data
Expand All @@ -17,5 +18,19 @@ services:
ports:
- "9070:9070"

labels:
ofelia.enabled: "true"
ofelia.job-exec.backupcron.schedule: "@every 1h"
ofelia.job-exec.backupcron.environment: '["COZO_PORT=${COZO_PORT}", "COZO_AUTH_TOKEN=${COZO_AUTH_TOKEN}", "COZO_BACKUP_DIR=${COZO_BACKUP_DIR}"]'
ofelia.job-exec.backupcron.command: bash /app/backup.sh

memory-store-backup-cron:
image: mcuadros/ofelia:latest
depends_on:
- memory-store
command: daemon --docker -f label=com.docker.compose.project=${COMPOSE_PROJECT_NAME}
volumes:
- /var/run/docker.sock:/var/run/docker.sock:ro

volumes:
cozo_data:
cozo_data:
5 changes: 2 additions & 3 deletions memory-store/run.sh
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,8 @@ export COZO_ROCKSDB_DIR=${COZO_ROCKSDB_DIR:-cozo.db}
echo $COZO_AUTH_TOKEN > $MNT_DIR/${COZO_ROCKSDB_DIR}.rocksdb.cozo_auth

# Start server
${APP_HOME:=.}/bin/cozo-bin server \
${APP_HOME:=.}/bin/cozo server \
--engine rocksdb \
--path $MNT_DIR/${COZO_ROCKSDB_DIR} \
--bind 0.0.0.0 \
--port ${COZO_PORT:=9070} \
--token-table __tokens
--port ${COZO_PORT:=9070}
Loading