Home Text Tools Math & Numbers Developer Tools Date & Time Image Tools Blog
Cron Job Expression Generator

Schedule a Nightly Database Backup with Cron at 2 AM

Write the cron expression for a 2 am nightly backup, plus tips on timezone, logging and overlapping jobs so backups really run.

A backup that nobody remembers to run is not a backup. Cron fixes the remembering part, as long as the schedule is correct. The classic nightly job is 0 2 * * *, and each of those five fields means something specific.

Cron Job Expression Generator FREE online toolRuns in your browser. No sign-up, nothing to install. Open the tool

Reading the expression

A standard cron line has five time fields: minute, hour, day of month, month and day of week. In 0 2 * * * the minute is 0 and the hour is 2, and the three asterisks mean every day, every month, every weekday. So the job runs at 02:00 each night.

The hour uses a 24-hour clock, so 2 is 2 am and 14 would be 2 pm. Mixing that up is a classic way to back up in the middle of the afternoon peak.

Build the line

If you only want weekday backups, change the last field: 0 2 * * 1-5 runs Monday to Friday. For a weekly full backup on Sunday at 3 am, use 0 3 * * 0. The generator's description line is a handy sanity check for simple values, but read ranges and lists such as 1-5 yourself, since the description may not spell them out fully.

  1. Type 0 in the minute field and 2 in the hour field of the Cron Job Expression Generator, and leave day, month and weekday as *.
  2. Read the description to confirm it mentions 2:00 AM and no day, month or weekday restriction.
  3. Copy the expression and add the command after it, for example the path to your backup script.
  4. Add output redirection such as appending to a log file, so you can check what happened.
  5. Open the crontab with crontab -e, paste the line, save, and list it again with crontab -l to confirm.
  6. Wait for the first run, then check the log and restore a test file from the backup.

Why 2 am

The early hours are usually the quietest for most sites, so a database dump has less competition for disk and CPU. Choosing a minute that is not exactly 0 can still be smart on shared servers, since many people schedule things on the hour and the machine can get busy then.

If your server runs in UTC but your customers live elsewhere, 2 am server time may not be quiet for them. Check the machine's timezone with the date command before deciding.

  • Run the script with full paths, because cron has a minimal environment.
  • Redirect both output and errors to a log.
  • Use a lock file or flock so a slow backup cannot overlap with the next one.
  • Delete old backups on a schedule to stop the disk filling up.

Things that go wrong

The most common failure is a script that works in your shell but not in cron, because environment variables and the PATH differ. Use absolute paths for every command and set variables inside the script.

Another trap is daylight saving. In a timezone that changes clocks, a 2 am job can be skipped or run twice on transition nights, because that hour may not exist or may occur twice. Running the server in UTC avoids the issue.

Lastly, a backup you have never restored is only a hope. Schedule a periodic test restore into a scratch database so you know the files are usable.

For a larger database, think about how long the dump takes. A job that starts at 2 am and runs for four hours is still fine if nothing else competes, but it can collide with a 4 am maintenance task. Write down the start and the typical duration of every scheduled job on a server, and leave generous gaps between them.

Try the free Cron Job Expression Generator
Runs in your browser. No sign-up, nothing to install.

Comments (0)

No comments yet. Be the first to share a thought.