Project: Automated Directory Backup with Cron Scheduling
In this project, we will build a Bash script that compresses a target directory into a timestamped archive, uploads it to an AWS S3 bucket, and cleans up local archive files older than 30 days. We will then schedule it using Cron to run automatically every night.
1. The Backup Script
Create backup.sh:
#!/bin/bash
# Automated backup script: compress, upload to S3, clean old files
set -e
SOURCE_DIR="/var/www/myapp/uploads"
BACKUP_DIR="/var/backups/uploads"
S3_BUCKET="s3://my-backup-bucket/uploads"
RETENTION_DAYS=30
TIMESTAMP=$(date +%Y-%m-%d_%H-%M-%S)
ARCHIVE_NAME="uploads_backup_$TIMESTAMP.tar.gz"
ARCHIVE_PATH="$BACKUP_DIR/$ARCHIVE_NAME"
# Ensure backup directory exists
mkdir -p "$BACKUP_DIR"
# 1. Compress the directory
echo "Compressing $SOURCE_DIR..."
tar -czf "$ARCHIVE_PATH" -C "$(dirname "$SOURCE_DIR")" "$(basename "$SOURCE_DIR")"
echo "Archive created: $ARCHIVE_NAME ($(du -sh "$ARCHIVE_PATH" | awk '{print $1}'))"
# 2. Upload to S3
echo "Uploading to S3..."
aws s3 cp "$ARCHIVE_PATH" "$S3_BUCKET/$ARCHIVE_NAME"
echo "Upload complete: $S3_BUCKET/$ARCHIVE_NAME"
# 3. Clean up local archives older than 30 days
echo "Cleaning up archives older than $RETENTION_DAYS days..."
find "$BACKUP_DIR" -name "*.tar.gz" -mtime +"$RETENTION_DAYS" -delete
echo "Cleanup complete."
echo "Backup completed at $TIMESTAMP"2. Scheduling with Cron
Cron is a time-based job scheduler built into Unix/Linux systems. Open the cron table editor:
crontab -eAdd this line to run the backup every night at 2:00 AM:
0 2 * * * /path/to/backup.sh >> /var/log/backup.log 2>&1Cron schedule format: minute hour day-of-month month day-of-week command
| Field | Value | Meaning |
0 | minute | At 0 minutes past the hour |
2 | hour | At 2 AM |
* | day | Every day |
* | month | Every month |
* | weekday | Every day of the week |
Published on Last updated: