Skip to content

A collection of modular Bash scripts to automate system and database backups using cron jobs. Follows best practices for scheduling, environment configuration, and cleanup routines.

Notifications You must be signed in to change notification settings

ahadnawaz585/cron-jobs

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

18 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

πŸ—ƒοΈ cron Jobs Practice - Automated Backup Scripts

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.


πŸ“ Repository Structure

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 backups

βœ… best-practice/ Folder (Primary Focus)

This 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 Overview

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

βš™οΈ Setting Up the Environment

  1. Clone the Repository

    git clone https://github.com/<your-username>/cron-jobs.git
    cd cron-jobs/best-practice
  2. Copy the example .env file

    cp .env.backup.example .env.backup
  3. Edit .env.backup to match your system

    BACKUP_DIR="/path/to/backups"
    DB_HOST="localhost"
    DB_USER="your_user"
    DB_PASSWORD="your_password"
    DB_NAME="your_db"
    RETENTION_DAYS=7
  4. Set ENV_FILE in laod_env.sh

    ENV_FILE="$(dirname "$0")/.env.backup"

πŸ“Œ Usage Guide

Run any script manually or schedule via crontab.

πŸ”„ Manual Execution

# Load env first
source ./laod_env.sh

# Example:
./hourly_backup.sh
./cleanup_backups.sh

πŸ•’ Scheduling with Crontab

Open crontab:

crontab -e

Paste 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.sh

πŸ§ͺ Other Folders

πŸ“ db-backup-routine/

Contains DB-focused backup scripts:

  • 1 day.db.linux.sh
  • script.linux.sh
  • test.linux.sh

These test out database dump techniques, compression, and rotation.


πŸ“ echo-file/

Minimal test scripts:

  • script.linux.sh and script.window.sh echo file contents.
  • Useful for debugging crontab commands.

πŸ“ multiple_db/

Single script handling multiple database backups:

  • Uses loops and arrays to backup a list of databases.
  • Ideal for multi-tenant systems.

πŸ’‘ Pro Tips

  • Make scripts executable:

    chmod +x *.sh
  • View crontab logs (Linux):

    grep CRON /var/log/syslog
  • Test environment sourcing:

    bash -x ./hourly_backup.sh

πŸ“‚ Example: .env.backup.example

# 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=5

🀝 Contributing

Feel free to fork and submit pull requests for improvements, new strategies, or enhancements in modularity and compatibility!


πŸ“œ License

This project is licensed under the MIT License.


πŸ‘¨β€πŸ’» Author

Ahad
Practicing automation with love β™₯
Happy Hacking : )

About

A collection of modular Bash scripts to automate system and database backups using cron jobs. Follows best practices for scheduling, environment configuration, and cleanup routines.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages