Welcome to MacTalk Australia

the largest Australian community for Apple discussions and topics

Join the discussions, Register Now!
Results 1 to 10 of 10
  1. #1

    Join Date
    Feb 2004
    Posts
    9,050

    Default

    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?
    The discussion has continued at AppleTalk Australia.

  2. #2

    Join Date
    Jan 2005
    Location
    Brisbane
    Posts
    517

    Default

    Have a go at this.

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

  3. #3

    Join Date
    May 2004
    Location
    Melbourne
    Posts
    2,087

    Default

    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....
    Code:
    #!/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.

    garethtownsend.info

    Melbourne Cocoaheads! - we're cuckoo for cocoa apps!

  4. #4

    Join Date
    Feb 2004
    Posts
    9,050

    Default

    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:

    Code:
    # /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
    Code:
    /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
    The discussion has continued at AppleTalk Australia.

  5. #5

    Join Date
    May 2004
    Location
    Melbourne
    Posts
    2,087

    Default

    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.
    Code:
    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
    Code:
    ./backup

    For manual
    Code:
    ./backup -db databasename
    garethtownsend.info

    Melbourne Cocoaheads! - we're cuckoo for cocoa apps!

  6. #6

    Join Date
    Feb 2004
    Posts
    9,050

    Default

    Instead of database="db1" could you have database="$1" instead?
    The discussion has continued at AppleTalk Australia.

  7. #7

    Join Date
    May 2004
    Location
    Melbourne
    Posts
    2,087

    Default

    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.
    garethtownsend.info

    Melbourne Cocoaheads! - we're cuckoo for cocoa apps!

  8. #8

    Join Date
    Feb 2004
    Posts
    9,050

    Default

    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:
    Code:
    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".
    The discussion has continued at AppleTalk Australia.

  9. #9

    Join Date
    Jan 2005
    Location
    Brisbane
    Posts
    517

    Default

    Code:
    if [ "$1" == "" ]
    then
            echo
    else
            $DUMP
    fi
    Should do the trick.

  10. #10

    Join Date
    May 2004
    Location
    Melbourne
    Posts
    2,087

    Default

    I'll dig up some scripts when i get home. Send me a PM to remind me Curra.
    garethtownsend.info

    Melbourne Cocoaheads! - we're cuckoo for cocoa apps!

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •