Difference between revisions of "Automate MySQL Database Backup via Cronjob"
Docs admin (talk | contribs) |
Docs admin (talk | contribs) |
||
(20 intermediate revisions by the same user not shown) | |||
Line 2: | Line 2: | ||
<br> | <br> | ||
{{warning|The backup cron will continue to create backup files until your disk space is completely filled up. <br>In order to prevent this, you will create a logrotate file as well as another cronjob entry to automatically rotate out the older backups, to keep disk space down.}} | {{warning|The backup cron will continue to create backup files until your disk space is completely filled up. <br>In order to prevent this, you will create a logrotate file as well as another cronjob entry to automatically rotate out the older backups, to keep disk space down.}} | ||
− | <br> | + | <br> |
+ | __TOC__ | ||
− | ==Create the Backup cron in cPanel== | + | ==Create the Backup cron job in cPanel== |
<br> | <br> | ||
{{note|You will need to know your Database username and database password in order to configure the backup.}} | {{note|You will need to know your Database username and database password in order to configure the backup.}} | ||
Line 15: | Line 16: | ||
<br><br> | <br><br> | ||
4) Scroll down a bit, and either select a common setting (Once a day, once a week, etc) from the drop down. or enter a custom entry for days, weeks, months, etc. | 4) Scroll down a bit, and either select a common setting (Once a day, once a week, etc) from the drop down. or enter a custom entry for days, weeks, months, etc. | ||
+ | <br><br> | ||
+ | [[File:mysqlcron.png]] | ||
<br><br> | <br><br> | ||
5) For the command to run, enter the following command. Replace CPANELUSERNAME with your cpanel username. Replace DBUSERNAME with your database username. Replace PASSWORD with the database password, and CPANELUSERNAME with your cPanel username. | 5) For the command to run, enter the following command. Replace CPANELUSERNAME with your cpanel username. Replace DBUSERNAME with your database username. Replace PASSWORD with the database password, and CPANELUSERNAME with your cPanel username. | ||
<br> | <br> | ||
<syntaxhighlight lang="bash"> | <syntaxhighlight lang="bash"> | ||
− | date=`date -I`; /usr/bin/mysqldump -uDBUSERNAME -pPASSWORD dbname > /home/CPANELUSERNAME/ | + | date=`date -I`; /usr/bin/mysqldump -uDBUSERNAME -pPASSWORD dbname > /home/CPANELUSERNAME/dbname_$date.sql |
</syntaxhighlight> | </syntaxhighlight> | ||
− | + | <br> | |
− | For <syntaxhighlight lang="bash"> | + | For Example, cPanel username is example, database name is mydatabase, and database password is mypassword |
+ | <syntaxhighlight lang="bash"> | ||
date=`date -I`; /usr/bin/mysqldump -umyusername -pmypassword mydatabase > /home/example/mydatabase_$date.sql | date=`date -I`; /usr/bin/mysqldump -umyusername -pmypassword mydatabase > /home/example/mydatabase_$date.sql | ||
</syntaxhighlight> | </syntaxhighlight> | ||
<br> | <br> | ||
− | This will create a sql file in your /home/cpanelusername directory with a name | + | This will create a sql file in your /home/cpanelusername directory with a name of: mydatabase_20180101.sql |
+ | <br><br> | ||
+ | 6) Click the blue "Add New Cron Job" button when done. | ||
<br><br> | <br><br> | ||
+ | ==Create the Log Rotation config file== | ||
+ | |||
+ | You will need to create a logrotate directory under your home directory, and a my,conf fike to enter the directives you will need. Here are the contents of a sample my.conf file:<br> | ||
+ | <syntaxhighlight lang="bash"> | ||
+ | /home/CPANELUSER/mydatabase*.sql { | ||
+ | daily | ||
+ | rotate 3 | ||
+ | compress | ||
+ | } | ||
+ | </syntaxhighlight> | ||
+ | |||
+ | This is set to rotate daily, keeping the 3 newest backups. They will be called something similar to: <br> | ||
+ | mydatabase_20180103.sql.gz<br> | ||
+ | mydatabase_20180102.sql.gz<br> | ||
+ | mydatabase_20180101.sql.gz<br><br> | ||
+ | |||
+ | ==Set up the Log Rotation cron job== | ||
+ | |||
+ | This will be similar to Step 1, where you set up the mysql database backup. The command to enter would be similar to:<br> | ||
− | = | + | <syntaxhighlight lang="bash"> |
+ | /usr/sbin/logrotate -s /home/example/logrotate/my.conf > /dev/null 2>&1 | ||
+ | </syntaxhighlight> | ||
+ | <br> | ||
+ | This will run the rotation at the specified time daily, using the my.conf file for the rotation instructions.<br><br> | ||
− | + | After you click the blue Add New Cron Job button, give the cron a day and check if the backup runs. |
Latest revision as of 13:17, 6 June 2018
Automating a MySQL Database backup in cPanel will take a few minutes of work, but only needs to be set up once.
In order to prevent this, you will create a logrotate file as well as another cronjob entry to automatically rotate out the older backups, to keep disk space down.
Create the Backup cron job in cPanel
1) Log into your cPanel
2) Go to Advanced -> Cron Jobs
3) Enter the email you wish to email the results of the cron job to.
4) Scroll down a bit, and either select a common setting (Once a day, once a week, etc) from the drop down. or enter a custom entry for days, weeks, months, etc.
5) For the command to run, enter the following command. Replace CPANELUSERNAME with your cpanel username. Replace DBUSERNAME with your database username. Replace PASSWORD with the database password, and CPANELUSERNAME with your cPanel username.
date=`date -I`; /usr/bin/mysqldump -uDBUSERNAME -pPASSWORD dbname > /home/CPANELUSERNAME/dbname_$date.sql
For Example, cPanel username is example, database name is mydatabase, and database password is mypassword
date=`date -I`; /usr/bin/mysqldump -umyusername -pmypassword mydatabase > /home/example/mydatabase_$date.sql
This will create a sql file in your /home/cpanelusername directory with a name of: mydatabase_20180101.sql
6) Click the blue "Add New Cron Job" button when done.
Create the Log Rotation config file
You will need to create a logrotate directory under your home directory, and a my,conf fike to enter the directives you will need. Here are the contents of a sample my.conf file:
/home/CPANELUSER/mydatabase*.sql {
daily
rotate 3
compress
}
This is set to rotate daily, keeping the 3 newest backups. They will be called something similar to:
mydatabase_20180103.sql.gz
mydatabase_20180102.sql.gz
mydatabase_20180101.sql.gz
Set up the Log Rotation cron job
This will be similar to Step 1, where you set up the mysql database backup. The command to enter would be similar to:
/usr/sbin/logrotate -s /home/example/logrotate/my.conf > /dev/null 2>&1
This will run the rotation at the specified time daily, using the my.conf file for the rotation instructions.
After you click the blue Add New Cron Job button, give the cron a day and check if the backup runs.