LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Software (https://www.linuxquestions.org/questions/linux-software-2/)
-   -   i want to cron a backup script (https://www.linuxquestions.org/questions/linux-software-2/i-want-to-cron-a-backup-script-630571/)

sneakyimp 03-25-2008 02:30 PM

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?

rednuht 03-25-2008 03:16 PM

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
runs the date command setting the format to YYYY_MM_DD storing it in a variable called TODAY and if that command succedes then echoing its value back to you.
then (in the script) you can just add a line under the first
Code:

mkdir $TODAY
(might want to add a path).
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
c= create
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
I think that using "mtime 1" is a bit dodgy as its not what is new but what was modified/created in the last 1 day.
There are other ways
Code:

man find
the -print0 / xargs -0 handle bad file names.

you might be better off asking short specific questions, but please let us know how you get on.

sneakyimp 03-25-2008 04:17 PM

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?

prad77 03-25-2008 04:35 PM

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

chrism01 03-25-2008 09:02 PM

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.