PDA

View Full Version : Inserting the date in shell scripts



Currawong
6th April 2006, 03:07 PM
Today I manually ran a command to backup a mysql database on my site thus:

mysqldump -uusername -p -Q currawong_net_geeklog | gzip -c > cnet-20060406.sql.gz

However, i can't help wondering if there isn't a way to script this so today's date is inserted into the filename automatically. Does anyone have any idea how I might go about this?

zbaron
6th April 2006, 04:10 PM
Have a go at this.


mysqldump -uusername -p -Q currawong_net_geeklog | gzip -c > cnet-`/bin/date +%Y%m%d`.sql.gz

Quamen
6th April 2006, 04:16 PM
date +"%Y%m%d"

will give you your date in the format above.

You just need to dump the date into a variable and then construct another variable to create the filename.

something like....


#!/bin/sh

DATE=/bin/date

filestart="cnet-"
fileend=".sql.gz"
date=`$DATE +"%Y%m%d"`

echo $filestart$date$fileend


will print you a filename in the format you want.

Currawong
6th April 2006, 05:52 PM
Excellent! I had a feeling it was simple. I'd got as far as working out what the date command did, but not how to insert the output.

I ended up creating two shell scripts, one for manually backups, and the other for automated backups:


# /usr/bin/mysqldump -uuser -ppassword -S /tmp/mysql.sock -Q $1 | gzip -c > /Users/user/backups/$1-`/bin/date +%Y%m%d-%H%M%S`.sql.gz


/usr/bin/mysqldump -uuser -ppassword -S /tmp/mysql.sock -Q db1 | gzip -c > /Users/user/backups/db2-`/bin/date +%Y%m%d-%H%M%S`.sql.gz
/usr/bin/mysqldump -uuser -puser -S /tmp/mysql.sock -Q db2 | gzip -c > /Users/user/backups/db2-`/bin/date +%Y%m%d-%H%M%S`.sql.gz

These work perfectly. Thanks for your help people :)

Quamen
6th April 2006, 10:15 PM
YUCK YUCK YUCK.

Code repetition is bad. From a script maintenance and portability standpoint you should be coding all of your application paths into variables. That way if you move system, or need to add some more calls to the same application you can do so easily. Same applies to backup locations, usernames etc.


DATE=`/bin/date +%Y%m%d-%H%M%S`
DUMP=/usr/bin/mysqldump
SQLSOCK=/tmp/mysql.sock
GZIP=/path/to/gzip

user="user"
password="password"

database="db1"
backup="/Users/user/backups/db2-"
backuptail=".sql.gz"

$DUMP -u $user -p $password -S $SQLSOCK -Q $database | $GZIP -c > $backup$date$backuptail


If you really wanted to get funky you would make one backup script, that had a default database that could be over-riden based on a command line flag with something else.

For auto backup
./backup


For manual
./backup -db databasename

Currawong
6th April 2006, 10:44 PM
Instead of database="db1" could you have database="$1" instead?

Quamen
7th April 2006, 07:03 AM
Yes you could do that. Don't think you would need the quotation marks though.

You would use a conditional statement to check the existence of $1 and then set database accordingly to create a flexible multipurpose backup script.

Have a play with it. If you need some example scripts let me know, I've got a whole pile from when i did Unix Systems Administration at uni.

Currawong
7th April 2006, 05:22 PM
I'd love any scripts you have. I played with shell stuff years ago, but never got back into it much. I can comprehend what scripts do, but haven't written many myself.

At the moment, I've got as far as:


if [ $1="" ]; then
echo "You need to specify which database you want to backup.";
else
$DUMP -u $user -p $password -S $SQLSOCK -Q $database | $GZIP -c > $backup$1-$date$backuptail
fi


But either my "if" is wrong, or "else" should be "else if".

zbaron
8th April 2006, 06:58 PM
if [ "$1" == "" ]
then
echo
else
$DUMP
fi


Should do the trick.

Quamen
10th April 2006, 10:33 AM
I'll dig up some scripts when i get home. Send me a PM to remind me Curra.