Is it possible to make a cron to backup my database at specific times?

Hosting Control Panel | Updated March 2026

Yes, you can schedule automatic database backups using cron jobs in cPanel. This ensures you always have a recent backup without having to remember to do it manually. This guide covers the cron command, security best practices, and backup rotation.

01. Basic Backup Command

The mysqldump command exports your database to a SQL file. Piping it through gzip compresses it (typically 80-90% smaller). The find command at the end deletes backups older than 7 days so your disk space doesn't fill up.

Before setting up the cron, create the backup directory:

mkdir ~/backups

Replace dbuser, password, and dbname with your actual database credentials. You can find these in your application's config file (e.g., wp-config.php for WordPress) or in cPanel > MySQL Databases.

Remember that database usernames and names are prefixed with your cPanel username (e.g., cpuser_dbname).

02. Securing Your Credentials

Putting the password directly in the cron command is a security risk because it's visible in the process list and crontab. A better approach is to store credentials in a MySQL options file:

# Create ~/.my.cnf
cat > ~/.my.cnf << 'EOF'
[mysqldump]
user=cpuser_dbuser
password=your_password_here
EOF

# Restrict permissions so only you can read it
chmod 600 ~/.my.cnf

Now mysqldump reads credentials automatically and you can simplify the cron command:

mysqldump dbname | gzip > ~/backups/db_$(date +\%Y\%m\%d).sql.gz && find ~/backups -name "db_*.sql.gz" -mtime +7 -delete
Tip

The ~/.my.cnf file with 600 permissions is the MySQL-recommended way to store credentials for automated scripts. It's more secure than command-line passwords and environment variables.

03. Setting Up the Schedule

In cPanel, go to Advanced > Cron Jobs and add the backup command with your preferred schedule:

Daily at 3 AM: 0 3 * * *

Every 6 hours: 0 */6 * * *

Weekly (Sunday 2 AM): 0 2 * * 0

Add >/dev/null 2>&1 at the end of the full command to suppress email notifications. For more on cron timing, see How to Create a Cron Job.

04. Backing Up Multiple Databases

If you have multiple databases, you can back them all up in one command:

mysqldump --all-databases | gzip > ~/backups/all_dbs_$(date +\%Y\%m\%d).sql.gz

Or back up specific databases individually so you can restore them independently:

for db in cpuser_db1 cpuser_db2 cpuser_db3; do
  mysqldump $db | gzip > ~/backups/${db}_$(date +\%Y\%m\%d).sql.gz
done
find ~/backups -name "*.sql.gz" -mtime +7 -delete

05. WordPress Database Backup

For WordPress, your database name, username, and password are in wp-config.php. Find the lines with DB_NAME, DB_USER, and DB_PASSWORD and use those values in the mysqldump command.

Alternatively, use WP-CLI for WordPress-aware backups:

wp db export ~/backups/wp_$(date +\%Y\%m\%d).sql --path=~/public_html

For complete backup strategies including files and databases, see How to Back Up Your Website and How to Backup MySQL.

Need Help With Backups?

If you need help setting up automated backups or restoring from a backup, open a ticket.

Open a Support Ticket

Quick Recap: Automated Database Backups

  1. Use mysqldump with gzip for compressed backups
  2. Store credentials in ~/.my.cnf (chmod 600) instead of the cron command
  3. Add a find -delete to rotate old backups automatically
  4. Schedule in cPanel > Advanced > Cron Jobs
  5. Add >/dev/null 2>&1 to suppress cron emails

Last updated March 2026 · Browse all Hosting CP articles

  • 420 Users Found This Useful

Was this answer helpful?

Related Articles

Add Flash Chat to your Website

Obsolete Technology | 2026 This Technology Is No Longer Available Adobe Flash was...

Reseller: Unable to find an IP address in when creating an account

General | Updated 2026 The "Unable to find an IP address" error in WHM occurs when trying to...

How to Speed Up Your Website

Website Tools & SEO | Updated March 2026 A slow website loses visitors and hurts search...

Updating an Old Docker Version to Community Edition

Obsolete | 2026 This Article Is Outdated Docker versioning and installation methods have...

Cross Origin Request Error (CORS)

General | Updated March 2026 A Cross-Origin Request (CORS) error occurs when a web page on...



Save 30% on web hosting - Use coupon code Hosting30