Automate MySQL Database Backup via Cronjob: Difference between revisions
Docs admin (talk | contribs) No edit summary |
Docs admin (talk | contribs) |
||
Line 45: | Line 45: | ||
</syntaxhighlight> | </syntaxhighlight> | ||
This is set to rotate daily, keeping the 3 newest days. They will be called something similar to mydatabase.sql, mydatabase.sql.1. mydatabase.sql.2, and the oldest | This is set to rotate daily, keeping the 3 newest days. They will be called something similar to mydatabase.sql, mydatabase.sql.1. mydatabase.sql.2, and the oldest will rotate out when the next one is created. | ||
<br> | <br> | ||
==Set up the Log Rotation cron job== | ==Set up the Log Rotation cron job== |
Revision as of 12:04, 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.
<syntaxhighlight lang="bash">
/usr/bin/mysqldump -uDBUSERNAME -pPASSWORD dbname > /home/CPANELUSERNAME/dbname.sql
</syntaxhighlight>
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
</syntaxhighlight>
This will create a sql file in your /home/cpanelusername directory with a name of: mydatabase1.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:
<syntaxhighlight lang="bash">
/home/CPANELUSER/mydatabase.sql {
daily
rotate 3
compress
}
</syntaxhighlight>
This is set to rotate daily, keeping the 3 newest days. They will be called something similar to mydatabase.sql, mydatabase.sql.1. mydatabase.sql.2, and the oldest will rotate out when the next one is created.