LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices

Reply
 
Search this Thread
Old 03-25-2008, 02:30 PM   #1
sneakyimp
Member
 
Registered: Dec 2004
Posts: 791

Rep: Reputation: 49
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?
 
Old 03-25-2008, 03:16 PM   #2
rednuht
Member
 
Registered: Aug 2005
Posts: 239
Blog Entries: 1

Rep: Reputation: 30
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.
 
Old 03-25-2008, 04:17 PM   #3
sneakyimp
Member
 
Registered: Dec 2004
Posts: 791

Original Poster
Rep: Reputation: 49
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?
 
Old 03-25-2008, 04:35 PM   #4
prad77
Member
 
Registered: Mar 2008
Posts: 101

Rep: Reputation: 15
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

Last edited by prad77; 04-17-2008 at 03:27 AM.
 
Old 03-25-2008, 09:02 PM   #5
chrism01
Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.5, Centos 5.10
Posts: 16,225

Rep: Reputation: 2021Reputation: 2021Reputation: 2021Reputation: 2021Reputation: 2021Reputation: 2021Reputation: 2021Reputation: 2021Reputation: 2021Reputation: 2021Reputation: 2021
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
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
shell script using /etc/cron.hourly to execute cron.php file? rioguia Programming 3 06-11-2008 08:09 AM
adding a perl script to cron.daily / cron.d to setup a cron job CrontabNewBIE Linux - Software 6 01-14-2008 08:16 AM
Backup script won't run in cron job. Why? Micro420 Linux - General 19 10-31-2007 08:26 PM
Cron Backup TPupAZ Linux - Software 7 02-11-2005 07:49 AM
Can a backup be done with cron? sadly so Solaris / OpenSolaris 3 02-03-2005 01:28 AM


All times are GMT -5. The time now is 11:31 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration