This repository is a structured collection of bash scripts for scheduling and managing backups using cron jobs in Unix-based systems. It includes organized folders for best practices, test routines, and real-world script variations.
cron-jobs/
β
βββ best-practice/ # β
Recommended setup with .env and modular scripts
βββ db-backup-routine/ # Database-specific backup experiments
βββ echo-file/ # Basic test scripts using echo
βββ multiple_db/ # Script to handle multiple DB backups
βββ crontab.txt # π Crontab example for scheduling backupsThis folder contains modular and reusable bash scripts following best practices for:
- Structured backups (hourly, daily, weekly, etc.)
- Easy environment configuration
- Cleanup routines
- Directory initialization
| File Name | Description |
|---|---|
hourly_backup.sh |
Performs hourly backups (every 3 hours) |
daily_backup.sh |
Performs daily backups at midnight |
weekly_backup.sh |
Runs every Sunday at midnight |
monthly_backup.sh |
Executes on the first of each month |
yearly_backup.sh |
Executes on January 1st |
cleanup_backups.sh |
Deletes old backups beyond retention policy |
setup_backup_dirs.sh |
Initializes required backup folders |
laod_env.sh |
Loads environment variables from .env.backup |
.env.backup.example |
Example environment configuration file |
crontab.txt |
Example crontab file for scheduling scripts |
-
Clone the Repository
git clone https://github.com/<your-username>/cron-jobs.git cd cron-jobs/best-practice
-
Copy the example
.envfilecp .env.backup.example .env.backup
-
Edit
.env.backupto match your systemBACKUP_DIR="/path/to/backups" DB_HOST="localhost" DB_USER="your_user" DB_PASSWORD="your_password" DB_NAME="your_db" RETENTION_DAYS=7
-
Set
ENV_FILEinlaod_env.shENV_FILE="$(dirname "$0")/.env.backup"
Run any script manually or schedule via crontab.
# Load env first
source ./laod_env.sh
# Example:
./hourly_backup.sh
./cleanup_backups.shOpen crontab:
crontab -ePaste the contents from crontab.txt, and update SCRIPT_PATH to your absolute script directory path.
# Example with SCRIPT_PATH set
SCRIPT_PATH="/home/user/cron-jobs/best-practice"
0 */3 * * * $SCRIPT_PATH/hourly_backup.sh
0 0 * * * $SCRIPT_PATH/daily_backup.sh
0 0 * * 0 $SCRIPT_PATH/weekly_backup.sh
0 0 1 * * $SCRIPT_PATH/monthly_backup.sh
0 0 1 1 * $SCRIPT_PATH/yearly_backup.sh
0 1 * * * $SCRIPT_PATH/cleanup_backups.shContains DB-focused backup scripts:
1 day.db.linux.shscript.linux.shtest.linux.sh
These test out database dump techniques, compression, and rotation.
Minimal test scripts:
script.linux.shandscript.window.shecho file contents.- Useful for debugging crontab commands.
Single script handling multiple database backups:
- Uses loops and arrays to backup a list of databases.
- Ideal for multi-tenant systems.
-
Make scripts executable:
chmod +x *.sh -
View crontab logs (Linux):
grep CRON /var/log/syslog
-
Test environment sourcing:
bash -x ./hourly_backup.sh
# Path to store backup files
BACKUP_DIR="/var/backups/myapp"
# MySQL credentials
DB_HOST="localhost"
DB_USER="root"
DB_PASSWORD="secret"
DB_NAME="app_db"
# Retention policy
HOURLY_RETENTION=8
DAILY_RETENTION=7
WEEKLY_RETENTION=4
MONTHLY_RETENTION=12
YEARLY_RETENTION=5Feel free to fork and submit pull requests for improvements, new strategies, or enhancements in modularity and compatibility!
This project is licensed under the MIT License.
Ahad
Practicing automation with love β₯
Happy Hacking : )