i want to cron a backup script
I have a site that gets new users signing up every day. Some of them upload avatars for use in a forum. I need a script that will
1) create a folder named after today's date 2) do a mysqldump of selected tables to that folder with and zip it up with a filename like db_dump.sql.tgz 3) copy any NEW uploaded images from some folder (e.g., /var/www/html/assets/images/) and zip them inot the same folder created in step 1. I have lots of PHP experience and am familiar with the command line for various things but i have NO experience shell scripting. If anyone could help me out, I'd be very grateful. Somethings I'm wondering are: * Should I shutdown the site while I'm doing the mysql dump? * how do you get the folder to have a name that matches the current date (e.g., 2008_03_25) * what sort of 'gotcha' conditions should I be wary of? |
The best thing about writing shell scripts is that you can run each part (usually) from the command lines to test it
1. Code:
TODAY=`date +%Y_%m_%d` && echo $TODAY then (in the script) you can just add a line under the first Code:
mkdir $TODAY 2. I am not familure with the mySQLdump but lots of examples exist so lets assume it creates mySQL.dump Code:
tar cvzf db_dump.sql.tgz mySQL.dump v= verbose z= zip compression (j is for bzip) f= file modify the above thus 3. Code:
find /var/www/html/assets/images/ -type f -mtime -1 -print0 | xargs -0 tar cvzf $TODAY/images.tgz There are other ways Code:
man find you might be better off asking short specific questions, but please let us know how you get on. |
Thanks for the help!
Perhaps we could start with shell basics: * how do you declare a var in a shell script? * how do you concatenate vars to form a new var? * i've seen the if/fi statements but am wondering how to halt a script should an error occur? |
variables can be created in simple ways.
PARAMS="`echo test`" echo $PARAMS Concatenations PARAMS2="`echo $PARAMS| .lst`" echo $PARAMS2 exit - to halt your script during error. Gentoo |
No.
You assign and create (declare) a shell var in one step: PARAM="somevalue" Concatenate by putting the vars next to each other: t=tt g=gg h=${t}${g} echo $h ttgg The parentheses tell the interpreter where each varname starts & finishes. Read these docs: http://www.tldp.org/LDP/abs/html/ http://tldp.org/LDP/Bash-Beginners-G...tml/index.html http://rute.2038bug.com/index.html.gz |
All times are GMT -5. The time now is 02:55 AM. |