WEB Unix command question...

Discussion in 'OT Technology' started by Josh, Jun 16, 2008.

  1. Josh

    Josh Guest

    I'm trying to set up a cron job to backup my database every day...

    I want it to automatically use the date and time timestamp in the file name...

    So here is what I have...

    mysqldump -h localhost -u USER -pPASSWORD DBNAME > /home/animetap/sometimedump.sql

    how do i change sometime to use the current time?

    Thanks
     
  2. TheDarkHorizon

    TheDarkHorizon \xC0\xFF\xEE

    Joined:
    Sep 26, 2002
    Messages:
    2,396
    Likes Received:
    0
    Location:
    San Francisco, CA
    Code:
    mysqldump -h localhost -u USER -pPASSWORD DBNAME > `date +"%m%d%y-%H%M"`-dump.sql
    Read the man page for date and change the formatting as you please.
     
  3. Josh

    Josh Guest

    AYE! I was putting the ` ` marks around the static text. Thanks a lot, works now!
     
  4. Josh

    Josh Guest

    actually it just works in shell, but when i put it into the cron job manager with cpanel is produces an error:

    cron job: mysqldump -h localhost -u animetap**** -p***** animetap_wrdp1 > /home/animetap/`date +%Y%m%d%H%M%S`-dump.sql

    error :

    /bin/sh: -c: line 0: unexpected EOF while looking for matching ``'
    /bin/sh: -c: line 1: syntax error: unexpected end of file
     
  5. Josh

    Josh Guest

    hellllp :)
     
  6. Ricky

    Ricky █▄ █▄█ █▄ ▀█▄

    Joined:
    Jun 17, 2005
    Messages:
    38,767
    Likes Received:
    6
    look like the % is fucking it up
     
  7. Ricky

    Ricky █▄ █▄█ █▄ ▀█▄

    Joined:
    Jun 17, 2005
    Messages:
    38,767
    Likes Received:
    6
    try \%
     
  8. retorq

    retorq What up bitch??

    Joined:
    Dec 14, 2006
    Messages:
    6,061
    Likes Received:
    0
    Location:
    Mohave Desert
    Why not put that shit into a script and call the script from the cron processor??
     
  9. Josh

    Josh Guest

    I tried, for some reason the exec wont run it right
     
  10. RaginBajin

    RaginBajin Have you punched a donkey today?

    Joined:
    Dec 24, 2001
    Messages:
    8,740
    Likes Received:
    0
    Location:
    NoVA
    This is what I use for my backup process. This keeps the last 7 days worth of backups. It also compresses the files.

    #!/bin/bash
    #
    # Backup script
    #

    BACKUPDIR=/mnt/backups
    TDATE=`/bin/date '+%y%m%d'`
    DELDATE=`/bin/date '+%y%m%d' -d "7 days ago"`


    /opt/mysql/bin/mysqldump -A --default-character-set=latin1 | gzip - > $BACKUPDIR/db03-$TDATE.sql.gz

    # delete last week's backup
    rm -f $BACKUPDIR/db03-$DELDATE.sql.gz 2>&1 >/dev/null

    # end of file
     
  11. Josh

    Josh Guest

    What are the steps to set that up? Do you save that line of code into a php file and run it on a cron job?
     
  12. loedown

    loedown Active Member

    Joined:
    Sep 7, 2007
    Messages:
    3,851
    Likes Received:
    0
    Location:
    Austin, TX
    You would put that into a file and make it executable, then set it to run every day as a cron
    something like sh backupfile.sh
     
  13. BlazinBlazer Guy

    BlazinBlazer Guy Witness to The De-Evolution of Mankind.

    Joined:
    Jul 24, 2002
    Messages:
    18,783
    Likes Received:
    0
    Location:
    Lansing, MI USA
    cd /home/animetap/
    vi cronjob.sh

    #!/bin/bash
    mysqldump -h localhost -u animetap**** -p***** animetap_wrdp1 > /home/animetap/`date +%Y%m%d%H%M%S`-dump.sql

    <escape> :wq <enter>

    chmod +x cronjob.sh

    Then in cPanel, just plug in "/home/animetap/cronjob.sh" and tell it to run when you want it.



    Of course you could just go to the command line and do it with 'crontab -e', but then every time upcp runs the entry will probably go away.
     
  14. Logik

    Logik Livin la vida broka

    Joined:
    Jun 30, 2000
    Messages:
    20,667
    Likes Received:
    1
    Location:
    The Steel City
    suggesting someone on OT use vi.. :rofl:

    nano, joe, emacs, pico were made for people on OT... :mamoru:
     
  15. Logik

    Logik Livin la vida broka

    Joined:
    Jun 30, 2000
    Messages:
    20,667
    Likes Received:
    1
    Location:
    The Steel City
    it should be noted that the path to mysqldump may be different for everyone... if just "mysqldump" doesnt work... find it by typing "locate mysqldump" and use the full path.
     
  16. retorq

    retorq What up bitch??

    Joined:
    Dec 14, 2006
    Messages:
    6,061
    Likes Received:
    0
    Location:
    Mohave Desert
    Since we're all posting our cron backups scripts here's one of mine, this does files and db in one script:

    Code:
    #!/bin/bash
    DBHOST=database.addr.com
    BACKUPDIR=/usr431/home/r/o/route66/backups
    DBNAMES=route66
    DATE=`date +%Y-%m-%d`
    OLDDATE=`date -v-7d +%Y-%m-%d`
    
    PATH=/usr/local/bin:/usr/bin:/bin
    
    # Run commands before we begin
    mysql --user=user --password=password --host=$DBHOST $DBNAMES < shutdown.sql
    rm -fR public_html/tmp/*
    rm -fR backups/route66_forums.files.gzip
    
    # Normal Backup
    mysqldump --user=user --password=password --host=$DBHOST $DBNAMES > "$BACKUPDIR/$DBNAMES-$DBHOST-$DATE.sql"
    gzip -f "$BACKUPDIR/$DBNAMES-$DBHOST-$DATE.sql"
    
    # Run commands when we're done
    mv public_html/uploads/ .
    mv public_html/media/ .
    tar cf backups/route66_forums.files public_html/*
    mv uploads/ public_html/
    mv media/ public_html/
    gzip -f backups/route66_forums.files
    mysql --user=user --password=password --host=$DBHOST $DBNAMES < startup.sql
    rm -f "$BACKUPDIR/$DBNAMES-$DBHOST-$OLDDATE.sql.gz"
    
     
  17. BlazinBlazer Guy

    BlazinBlazer Guy Witness to The De-Evolution of Mankind.

    Joined:
    Jul 24, 2002
    Messages:
    18,783
    Likes Received:
    0
    Location:
    Lansing, MI USA
    What can I say? I'm a sys-admin... I use real tools to get my job done :eek3:

    None of that Fisher-Price emacs bullshit :mamoru:
     

Share This Page