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.
Daily database backup with auto-cleanup
mysqldump -u dbuser -p'password' dbname | gzip > ~/backups/db_$(date +\%Y\%m\%d).sql.gz && find ~/backups -name "db_*.sql.gz" -mtime +7 -delete
This backs up daily and keeps the last 7 days. Set it up in cPanel > Advanced > Cron Jobs.
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
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 TicketQuick Recap: Automated Database Backups
- Use mysqldump with gzip for compressed backups
- Store credentials in ~/.my.cnf (chmod 600) instead of the cron command
- Add a find -delete to rotate old backups automatically
- Schedule in cPanel > Advanced > Cron Jobs
- Add >/dev/null 2>&1 to suppress cron emails
Last updated March 2026 · Browse all Hosting CP articles
