> ## Documentation Index
> Fetch the complete documentation index at: https://kb.hosting.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Backing up MySQL databases using cron jobs

> Learn to back up MySQL databases with cron jobs through our guide featuring a video walkthrough, step-by-step instructions, and code snippets.

This article describes how to set up a cron job that automatically backs up a MySQL database to a file at specific intervals.

## Automated database backups using cron jobs

There are two ways to run cron jobs that back up a MySQL database. You can either include MySQL login information in the cron job command itself, or you can use a configuration file to store the MySQL login information.

### Method #1: Include MySQL login information in the cron job command

You can run scheduled backups of a MySQL database by creating a cron job that runs the following command:

```bash theme={null}
/usr/bin/mysqldump --routines -u dbusername -p'dbpassword' dbname > ${HOME}/path/backup.sql
```

Replace ***dbusername*** with the database user, ***dbpassword*** with the database user's password, ***dbname*** with the database to back up, and ***path*** with the path where you want to store the backup file. This example uses *backup.sql* for the backup's filename, but you can use any filename you want.

<Tip>
  * The single quote (') characters around the password ensure that any special characters are processed correctly.

  * When the command runs successfully, no output is generated. However, if there is an error or misconfiguration, depending on your account configuration you may receive an e-mail message that contains the command output. To suppress e-mail messages when the command fails, add the following text to the end of the cron job command:

  ```bash theme={null}
  2>/dev/null
  ```

  Most people, however, want to be notified when a cron job fails.
</Tip>

For information about how to use cPanel to create a cron job, please see [this article](/docs/cron-jobs).

### Method #2: Use a configuration file to store MySQL login information

Alternatively, you can create a configuration file in your home directory that stores MySQL login information. Using this method, you do not have to include login information in your cron job commands. To do this, follow these steps:

1. Create a file named **.my.cnf** in your */home/* ***username*** directory, replacing ***username*** with your account username.
   > 🚧 Important
   >
   > Make sure you include the leading **.** in the filename.

2. Copy and paste the following text into the **.my.cnf** file. Replace ***dbusername*** with the database user and replace ***dbpassword*** with the database user's password:

   ```
   [client]
   user = dbusername
   password = "dbpassword"
   host = localhost
   ```

3. Create a cron job that runs the following command. Replace ***dbname*** with the name of the database to back up, and replace ***path*** with the path to store the backup file. This example uses *backup.sql* for the backup's filename, but you can use any filename you want:

   ```bash theme={null}
   /usr/bin/mysqldump --routines dbname > /path/backup.sql
   ```

   > 👍 Tip
   >
   > When the command runs successfully, no output is generated. However, if there is an error or misconfiguration, depending on your account configuration you may receive an e-mail message that contains the command output. To suppress e-mail messages when the command fails, add the following text to the end of the cron job command:
   >
   > ```bash theme={null}
   > 2>/dev/null
   > ```
   >
   > Most people, however, want to be notified when a cron job fails.

For information about how to use cPanel to create a cron job, please see [this article](/docs/cron-jobs).

## More information

For more information about the *mysqldump* program, please visit [http://dev.mysql.com/doc/refman/5.1/en/mysqldump.html](http://dev.mysql.com/doc/refman/5.1/en/mysqldump.html).

## Related articles

* [Cron jobs](/docs/cron-jobs)

* [Connecting to MySQL from the command line](/docs/connect-to-mysql-from-the-command-line)

* [Importing and exporting a MySQL database](/docs/import-and-export-a-mysql-database)

* [Managing MySQL databases, users, and tables from the command line](/docs/managing-mysql-databases-and-users-from-the-command-line)
