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

Added files for backup #186

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
29 changes: 25 additions & 4 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -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 .
Expand All @@ -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
18 changes: 16 additions & 2 deletions prod-docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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 [email protected] --non-interactive
command: certonly --webroot --webroot-path=/var/www/certbot -d ieee.nitk.ac.in --agree-tos --email [email protected] --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:
Expand All @@ -95,4 +109,4 @@ volumes:
postgres_data:
certbot_etc:
certbot_var:
certbot_www:
certbot_www:
3 changes: 3 additions & 0 deletions scripts/.env.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
DB_NAME=my_project_db
DB_USER=my_project_user
DB_PASSWORD=my_secure_password
2 changes: 2 additions & 0 deletions scripts/backup.cron
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# Run backup.sh at 2 AM every day
0 2 * * * /corpus/backup.sh >> /var/log/cron.log 2>&1
33 changes: 33 additions & 0 deletions scripts/backup.sh
Original file line number Diff line number Diff line change
@@ -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 {} \;
Loading