diff --git a/Dockerfile b/Dockerfile index 3ffbc51..fe11458 100644 --- a/Dockerfile +++ b/Dockerfile @@ -10,8 +10,10 @@ ENV PYTHONUNBUFFERED 1 RUN mkdir /corpus WORKDIR /corpus -# Install dependencies -RUN apt-get update && apt-get install -y gcc libpq-dev sqlite3 +# Install dependencies including cron +RUN apt-get update && \ + apt-get install -y gcc libpq-dev sqlite3 postgresql-client rclone cron && \ + apt-get clean # Install Python dependencies COPY corpus/requirements.txt . @@ -26,7 +28,26 @@ COPY env.example .env # Copy the application COPY corpus/ . -RUN chmod +x start_dev.sh +# Copy the backup script +COPY scripts/backup.sh /corpus/backup.sh +# Make scripts executable +RUN chmod +x start_dev.sh backup.sh + +# Copy the cron job file for backups +COPY scripts/backup.cron /etc/cron.d/backup + +# Set permissions for the cron job +RUN chmod 0644 /etc/cron.d/backup + +# Apply the cron job +RUN crontab /etc/cron.d/backup + +# Create a log file for cron logs +RUN touch /var/log/cron.log + +# Expose port 8000 for the Django app EXPOSE 8000 -ENTRYPOINT [ "/corpus/start_dev.sh" ] + +# Start the cron daemon and then run the entry point for Django application +CMD cron && tail -f /var/log/cron.log & /corpus/start_dev.sh \ No newline at end of file diff --git a/prod-docker-compose.yml b/prod-docker-compose.yml index 281ca67..e758ab6 100644 --- a/prod-docker-compose.yml +++ b/prod-docker-compose.yml @@ -84,7 +84,21 @@ services: - nginx networks: - corpus_network - command: certonly --webroot --webroot-path=/var/www/certbot -d ieee.nitk.ac.in --agree-tos --email anirudhprabhakaran3@gmail.com --non-interactive + command: certonly --webroot --webroot-path=/var/www/certbot -d ieee.nitk.ac.in --agree-tos --email anirudhprabhakaran3@gmail.com --non-interactive + +backup: + build: . + restart: "no" + depends_on: + postgres: + condition: service_healthy + volumes: + - ./backups:/backups + env_file: + - .env + networks: + - corpus_network + entrypoint: ["/corpus/backup.sh"] networks: corpus_network: @@ -95,4 +109,4 @@ volumes: postgres_data: certbot_etc: certbot_var: - certbot_www: + certbot_www: \ No newline at end of file diff --git a/scripts/.env.example b/scripts/.env.example new file mode 100644 index 0000000..704296b --- /dev/null +++ b/scripts/.env.example @@ -0,0 +1,3 @@ +DB_NAME=my_project_db +DB_USER=my_project_user +DB_PASSWORD=my_secure_password \ No newline at end of file diff --git a/scripts/backup.cron b/scripts/backup.cron new file mode 100644 index 0000000..8128d33 --- /dev/null +++ b/scripts/backup.cron @@ -0,0 +1,2 @@ +# Run backup.sh at 2 AM every day +0 2 * * * /corpus/backup.sh >> /var/log/cron.log 2>&1 \ No newline at end of file diff --git a/scripts/backup.sh b/scripts/backup.sh new file mode 100644 index 0000000..d069806 --- /dev/null +++ b/scripts/backup.sh @@ -0,0 +1,33 @@ +#!/bin/bash + +# Configuration +DATE=$(date +"%Y-%m-%d_%H-%M-%S") +BACKUP_DIR="/backups" +DB_NAME=${POSTGRES_DB} +DB_USER=${POSTGRES_USER} +DB_PASSWORD=${POSTGRES_PASSWORD} +DB_HOST="postgres" +BACKUP_FILE="$BACKUP_DIR/db_backup_$DATE.dump" + +# Export password for pg_dump +export PGPASSWORD=$DB_PASSWORD + +# Create the backup +pg_dump -h $DB_HOST -U $DB_USER -F c $DB_NAME > $BACKUP_FILE + +# Unset the password for security +unset PGPASSWORD + +# Verify if the backup was successful +if [ $? -eq 0 ]; then + echo "Database backup successful: $BACKUP_FILE" +else + echo "Database backup failed!" + exit 1 +fi + +# Upload to OneDrive using rclone +rclone copy $BACKUP_FILE onedrive_remote:/backups + +# Optional: Delete local backups older than 7 days +find $BACKUP_DIR -type f -name "*.dump" -mtime +7 -exec rm {} \; \ No newline at end of file