my-recipes/backend/CRON_SETUP.md

4.4 KiB

Automated Backup with Cron (Linux/Production)

Quick Setup

cd backend
chmod +x setup_cron.sh
./setup_cron.sh

Then select:

  • Option 1: Every 1 minute (for testing)
  • Option 2: Weekly (Sunday 2 AM) - after testing works

Option 2: Manual Setup

1. Make script executable

cd backend
chmod +x run_backup.sh

2. Edit crontab

crontab -e

3. Add one of these lines:

For Testing (every 1 minute):

* * * * * cd /path/to/backend && ./run_backup.sh

For Production (weekly - Sunday 2 AM):

0 2 * * 0 cd /path/to/backend && ./run_backup.sh

For Daily (2 AM):

0 2 * * * cd /path/to/backend && ./run_backup.sh

Replace /path/to/backend with your actual path, e.g.:

* * * * * cd /home/user/my-recipes/backend && ./run_backup.sh

4. Save and exit

  • Press Ctrl+X, then Y, then Enter (if using nano)
  • Or :wq (if using vim)

Verify It's Working

1. Check if cron job is installed

crontab -l | grep backup

2. Wait 2-3 minutes (for 1-minute test)

3. Check the log

cd backend
tail -f backup.log

Expected output:

[2025-12-21 15:30:45] Starting backup...
[2025-12-21 15:30:47] Creating database dump...
[2025-12-21 15:30:49] Compressing file...
[2025-12-21 15:30:51] Uploading to R2...
[2025-12-21 15:30:53] ✅ Backup completed: recipes_db_20251221_153045.sql.gz

4. Check R2 bucket

  • Should see new backup files appearing
  • Files named: recipes_db_YYYYMMDD_HHMMSS.sql.gz

Change from Testing to Weekly

Method 1: Using setup script

cd backend
./setup_cron.sh

Select option 2 (Weekly)

Method 2: Manual edit

crontab -e

Change this line:

* * * * * cd /path/to/backend && ./run_backup.sh

To this:

0 2 * * 0 cd /path/to/backend && ./run_backup.sh

Save and exit.

Cron Schedule Reference

* * * * * command
│ │ │ │ │
│ │ │ │ └─── Day of week (0-7, 0 and 7 are Sunday)
│ │ │ └───── Month (1-12)
│ │ └─────── Day of month (1-31)
│ └───────── Hour (0-23)
└─────────── Minute (0-59)

Examples:

  • * * * * * - Every minute
  • 0 2 * * * - Daily at 2:00 AM
  • 0 2 * * 0 - Weekly on Sunday at 2:00 AM
  • 0 2 * * 1 - Weekly on Monday at 2:00 AM
  • 0 */6 * * * - Every 6 hours

Troubleshooting

Cron job not running

1. Check cron service is running:

sudo systemctl status cron
# or
sudo systemctl status crond

2. Check cron logs:

# Ubuntu/Debian
grep CRON /var/log/syslog

# CentOS/RHEL
tail -f /var/log/cron

3. Test script manually:

cd backend
./run_backup.sh
cat backup.log

No backup.log file

Check permissions:

ls -la run_backup.sh
# Should be: -rwxr-xr-x

chmod +x run_backup.sh

Test Python script:

cd backend
python3 backup_db.py

Script runs but backup fails

Check backup.log for errors:

cat backup.log

Common issues:

  • Database credentials wrong (check .env)
  • R2 credentials wrong (check .env)
  • pg_dump not installed: sudo apt install postgresql-client
  • Python packages missing: pip install boto3

Remove Cron Job

crontab -e

Delete the line with run_backup.sh, save and exit.

Docker/Container Environment

If running in Docker, add to your docker-compose.yml or Dockerfile:

# docker-compose.yml
services:
  backend:
    # ... other config
    command: >
      sh -c "
        echo '0 2 * * 0 cd /app && python backup_db.py >> backup.log 2>&1' | crontab - &&
        crond -f -l 2
      "

Or use a separate container with a cron image.

Production Recommendations

  1. Use weekly backups - Daily can consume too much storage
  2. Monitor logs - Set up log monitoring/alerts
  3. Test restore - Periodically test restoring from backups
  4. Set up retention - Automatically delete old backups (not implemented yet)
  5. Use separate backup server - Don't backup to same server as database

Success Checklist

  • run_backup.sh is executable
  • Cron job is installed (crontab -l shows it)
  • Test run completed successfully
  • backup.log shows successful backup
  • R2 bucket contains backup files
  • Changed from 1-minute to weekly schedule