Exporting a MySQL database: Difference between revisions

>Samthecrazyman
m Changed date format to be correct an work, spelling correction
m Changed date code in cron command to correct order
 
(2 intermediate revisions by 2 users not shown)
Line 32: Line 32:


=== Creating the cron job ===
=== Creating the cron job ===
* Log on as <code>root</code> and start KDE Task Scheduler from the Kickstart menu (this can be found by typing 'cron'.
* Log on as <code>root</code> and start KDE Task Scheduler from the Kickstart menu (this can be found by typing 'cron').
* Select 'System Cron' from the radio buttons in the top task bar.
* Select 'System Cron' from the radio buttons in the top task bar.
* Select 'New entry'.
* Select 'New entry'.
* In the command box, we need to formulate a command. The following command gives most of the things you may need in the command and can be adapted to suit your needs:<br><pre>mysqldump -A -u USER -pPASSWORD > /path/to/backup/backup-name-$(date +%Y-%m-%d).sql</pre>
* In the command box, we need to formulate a command. The following command gives most of the things you may need in the command and can be adapted to suit your needs:<br><pre>mysqldump -A -u USER -pPASSWORD > /path/to/backup/backup-name-$(date +%d-%m-%Y).sql</pre>
** <code>-A</code> switch makes <code>mysqldump</code> backup all databases.  
** <code>-A</code> switch makes <code>mysqldump</code> backup all databases.  
** <code>USER</code> should be changed to the user.  
** <code>USER</code> should be changed to the user.  
** <code>PASSWORD</code> should be changed to the password used for the user. Notice, no space between the switch <code>-p</code> and <code>PASSWORD</code> or the job will fail.
** <code>PASSWORD</code> should be changed to the password used for the user. Notice, no space between the switch <code>-p</code> and <code>PASSWORD</code> or the job will fail.
** <code>/path/to/backup</code> should be the full path to the location you want the backup to take place.  
** <code>/path/to/backup</code> should be the full path to the location you want the backup to take place.  
** <code>backup-name</code> should be the name of the backup file, <code>$(date +%Y-%m-%d)</code> adds the date to the place you place it in the file name, especially handy if you want to keep your backups every day. Starting with the year as opposed to the day makes sorting the files in lists a lot easier.
** <code>backup-name</code> should be the name of the backup file, <code>$(date +%d-%m-%Y)</code> adds the date to the place you place it in the file name, especially handy if you want to keep your backups every day. Starting with the year as opposed to the day makes sorting the files in lists a lot easier.
* Select the user you want this to run under (probably root)
* Select the user you want this to run under (probably root)
* Now choose the time and date you would like the backup to take place. If you would like it to happen every day, select the 'Run every day' tick box and this will select all the relevant options, leaving you just to choose a time.
* Now choose the time and date you would like the backup to take place. If you would like it to happen every day, select the 'Run every day' tick box and this will select all the relevant options, leaving you just to choose a time.
Line 51: Line 51:
** Make sure MySQL is running
** Make sure MySQL is running
** Make sure the user has the correct privileges
** Make sure the user has the correct privileges
* The % on the date part of the command must be escaped with a \ or the code will stop past the %. Cron interprets % as the end of the line and must be escaped to make it pass it.
* Passwords with any special characters may make cron fail as it uses some characters for commands. You may have to change your MySQL password to an alphanumeric only password.
* Passwords with any special characters may make cron fail as it uses some characters for commands. You may have to change your MySQL password to an alphanumeric only password.
* KDE task scheduler has a problem at the time of writing that when you edit a job, the user randomly changes. Make sure that when you edit a job, you change the user back to root or the job will more than likely fail.
* KDE task scheduler has a problem at the time of writing that when you edit a job, the user randomly changes. Make sure that when you edit a job, you change the user back to root or the job will more than likely fail.