Welcome Anonymousregister

FreeBSD Hacks: Forums


FreeBSD Hacks :: View topic - Learn How-To Use The Date Function To Automate Saving Files
main forum search user groups members list profile pm faq
Learn How-To Use The Date Function To Automate Saving Files
Post new topic   Reply to topic View previous topic :: View next topic
Author Message
Michael

Site Admin


Joined: Nov 21, 2007
Posts: 4

PostPosted: Sun Dec 02, 2007 8:54 pm    Post subject: Learn How-To Use The Date Function To Automate Saving Files Reply with quote
 
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:


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`



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:


Code:
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.

_________________
Michael
Director of Business Development
webmaster (AT) freebsdhacks (DOT) com
Back to top

View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic View previous topic :: View next topic
Page 1 of 1
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001-2004 phpBB Group

MetalTech commercial theme pack by MDesign mtechnik.net
Forums ©
1: Credit Repair Combat
128 times
2: My Internet Business
123 times
0.189 Seconds