Share your knowledge at the LQ Wiki.
Go Back > Forums > Linux Forums > Linux - Software
User Name
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.


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

Rep: Reputation: 67
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
Registered: Aug 2005
Posts: 239
Blog Entries: 1

Rep: Reputation: 31
The best thing about writing shell scripts is that you can run each part (usually) from the command lines to test it
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
mkdir $TODAY
(might want to add a path).
I am not familure with the mySQLdump but lots of examples exist so lets assume it creates mySQL.dump
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
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
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
Senior Member
Registered: Dec 2004
Posts: 1,025

Original Poster
Rep: Reputation: 67
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
Registered: Mar 2008
Posts: 101

Rep: Reputation: 15
variables can be created in simple ways.

PARAMS="`echo test`"
echo $PARAMS

PARAMS2="`echo $PARAMS| .lst`"
echo $PARAMS2

exit - to halt your script during error.


Last edited by prad77; 04-17-2008 at 03:27 AM.
Old 03-25-2008, 09:02 PM   #5
LQ Guru
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.9, Centos 7.3
Posts: 17,400

Rep: Reputation: 2395Reputation: 2395Reputation: 2395Reputation: 2395Reputation: 2395Reputation: 2395Reputation: 2395Reputation: 2395Reputation: 2395Reputation: 2395Reputation: 2395

You assign and create (declare) a shell var in one step:

Concatenate by putting the vars next to each other:

echo $h

The parentheses tell the interpreter where each varname starts & finishes.

Read these docs:


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 > Forums > Linux Forums > Linux - Software

All times are GMT -5. The time now is 05:06 AM.

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