Cron Schedule for Daily Backup โ Write the Right Cron Expression
Scheduling a daily backup at 2 AM with cron is simple once you understand the five-field format. Here are the expressions for common backup schedules.
Backups that depend on someone remembering to run them don't get run. Cron jobs fix this. They're the backbone of automated server tasks on Linux and Unix systems, and scheduling a daily database backup with cron takes about five minutes to set up.
What a cron job is
Cron is a time-based task scheduler built into Linux, macOS, and most Unix-like systems. A cron job is a line in a configuration file (crontab) that tells the system to run a command at a specific time or interval. Once configured, it runs automatically without any human intervention.
A typical daily backup cron expression
Cron expressions have five fields: minute, hour, day of month, month, day of week. An asterisk means "every."
# Run backup script at 2:30 AM every day 30 2 * * * /home/user/scripts/backup.sh # Run at midnight every day 0 0 * * * /home/user/scripts/backup.sh # Run at 3 AM on weekdays only 0 3 * * 1-5 /home/user/scripts/backup.sh # Run every 6 hours 0 */6 * * * /home/user/scripts/backup.sh
A simple database backup script
#!/bin/bash DATE=$(date +%Y-%m-%d) BACKUP_DIR="/backups/db" DB_NAME="myapp" # Create backup with date in filename mysqldump -u root -p"$DB_PASSWORD" $DB_NAME > "$BACKUP_DIR/$DB_NAME-$DATE.sql" # Delete backups older than 30 days find $BACKUP_DIR -name "*.sql" -mtime +30 -delete
Adding the job to crontab
Run crontab -e to open your personal crontab. Add the cron expression pointing to your script. Save and close. The cron daemon picks up the change automatically, no restart needed.
Where to start if cron expressions confuse you
The Cron Generatorlets you select your schedule visually and generates the correct five-field expression. Paste it directly into your crontab. It also explains what an existing expression means, which is useful for inherited scripts where the schedule isn't documented.