 |
| Categories |
|
 |
|
|
|
| Survey |
|
 |
|
|
|
|
 |
 |
Title : Scripting: Using Date Function To Automate Saving Of Mysql Database Files |
 |
 |
 |
 |
 |

|
We Show You How To Use The Date Function To Save Reoccurring Files Which Would Otherwise Get Overwritten
One of the first things I ran into when beginning my FreeBSD adventure was finding a way to automatically backup my web servers mysql database files. I found that even webmin (an application management interface) wasn't able to handle saving of my mysql database files without overwriting the previously saved (day before) files.
As you may already know it's especially important with websites that you have progressive saves of the database in case you find a problem that requires you use a version of your site that happened 7 days previously. If you are new to scripting then all of this may not be something you will understand right off the bat, but eventually you'll understand the power of using the date function within your scripts when saving files. Essentially what this date function does is add a time stamp to the end of your file name so that it is completely unique because it's based off the current time of when the file is being saved.
What I did in my case was create a small script called websiteback.sh that I then placed in a bin directory within my home such as /root/bin/websiteback.sh. For this example I simplified the code within this file just for the sake of simplicity for those new to scripting. Basically what this file contains a line for each database within my system as follows:
<-----Begin File Code----->
#!/bin/sh
/usr/local/bin/mysqldump -u root --password=aBcD1234 dataBaseName > /location/you/want/to/save/backup/dataBaseName.`date +%m%d%Y%H%M%S`
/usr/local/bin/mysqldump -u root --password=aBcD1234 dataBaseName2 > /location/you/want/to/save/backup/dataBaseName2.`date +%m%d%Y%H%M%S`
<-----End File Code------>
The first line executes this file as a shell script via sh which needs to be set as an executable otherwise it will not run. See man chmod if you aren't sure how to set it executable.
Once you have created this file, the only thing left to do is create a cron job to execute the above file on a daily basis. For example you would create a cron job like the following:
0 8 * * * /root/bin/websiteback.sh
See man crontab for more information about how to set a cron job within FreeBSD. That's all there is to it!
Using the date function is very versatile and you can use it directly within your scripts and can use them within paths as I've shown above. I hope you find it as useful as I do and if you have any questions or comments, please feel free to post them here.
Posted on Friday, November 30, 2007 @ 22:16:20 EST by Michael
|
|
 |
 |
 |
 |
 |
 |
 |
 |
| Using Date Function To Automate Saving Of Mysql Database Files | Login/Create an Account | 0 comments | | The comments are owned by the poster. We aren't responsible for their content. |
|
 |
 |
 |
 |
| |
| Related Links |
|
 |
|
|
|
| Article Rating |
Average Score: 5 Votes: 1

|
|
 |
|
|
|
| Options |
|
 |
|
|
|
|
 |
|
 |
|
 |