Page 1 of 2 12 LastLast
Results 1 to 10 of 11

Thread: Does this tar script work?

  1. #1
    Join Date
    Sep 2007
    Location
    Malaysia
    Beans
    280
    Distro
    Ubuntu 11.04 Natty Narwhal

    Post Does this tar script work?

    Hello dudes,

    I am using crontab to schedule a backup script, and record everything to a file with the date.

    does this command accomplish this?

    Code:
    crontab -l
    # m h  dom mon dow   command
    0 2 * * 1-5 /home/oj/Documents/backup > /home/oj/Desktop/backup_$(date +%Y%m%d).log
    
    oj@oj-desktop:~$
    here's my backup script, its not for a server or such, just seeing if it will work..

    Code:
    #!/bin/bash
    
    OUTPUT=/home/oj/Desktop/backup_$(date +%Y%m%d).tar.gz
    
    BUDIR=/home/oj/Documents/
    
    echo "Starting backup of $BUDIR to file $OUTPUT"
    
    if [ `date +%w` -eq 4 ] ; then
    tar -cvzpf $OUTPUT $BUDIR
    
    fi
    
    if [`date +%w` -gt 0 ] && [`date +%w` -lt 4] ; then
    
    find $BUDIR -type f -newer $OUTPUT -print 0 | tar --null -czvf $OUTPUT -T-
    
    
    if [ $? == 0 ]; then
    
    mail -s "Backup successful for $OUTPUT" oj < ~
    
    else
    mail -s "Backup failed for $OUTPUT" oj < ~
    
    fi 
    fi
    If anything's wrong here can someone please highlight it to me? Thank you!

    EDIT:

    Something's wrong here, whats wrong with line 14?

    Code:
    ./backup
    Starting backup of /home/oj/Documents/ to file /home/oj/Desktop/backup_20091026.tar.gz
    ./backup: line 14: [1: command not found
    oj@oj-desktop:~/Documents$
    Last edited by psycho5; October 26th, 2009 at 12:49 PM. Reason: error

  2. #2
    Join Date
    Sep 2006
    Beans
    1,610
    Distro
    Ubuntu 9.10 Karmic Koala

    Re: Does this tar script work?

    Corrected in later post.
    Last edited by Giblet5; October 26th, 2009 at 02:15 PM. Reason: correction

  3. #3
    Join Date
    Sep 2006
    Beans
    1,610
    Distro
    Ubuntu 9.10 Karmic Koala

    Re: Does this tar script work?

    PS: It is always a bad idea to assume that a file ($OUTPUT in this case) exists.

    That's what 'test' (aka '[') is for.

    You could probably precede line 14 with
    Code:
    test -f $OUTPUT &&
    So, the working form could be
    Code:
    test -f $OUTPUT && find $BUDIR -type f -newer $OUTPUT | tar --null -czvf $OUTPUT -T-

  4. #4
    Join Date
    Sep 2007
    Location
    Malaysia
    Beans
    280
    Distro
    Ubuntu 11.04 Natty Narwhal

    Re: Does this tar script work?

    Thanks, I wasn't aware of the "test" command.

    how about the crontab command, I can do > to_log_with_date.log ?

  5. #5
    Join Date
    Sep 2006
    Beans
    1,610
    Distro
    Ubuntu 9.10 Karmic Koala

    Re: Does this tar script work?

    Quote Originally Posted by psycho5 View Post
    Thanks, I wasn't aware of the "test" command.

    how about the crontab command, I can do > to_log_with_date.log ?
    Maybe.

    That depends on how cron decides to execute it, but yeah.

    If the log thing is an inherent function of your script, then put it in the script:
    Code:
    #!/bin/bash
    
    ## Send all subsequent output to the log file:
    LOGFILE="/home/oj/Desktop/backup_`date '+%Y%m%d'`.log"
    exec > $LOGFILE 2>&1
    ...

  6. #6
    Join Date
    Sep 2007
    Location
    Malaysia
    Beans
    280
    Distro
    Ubuntu 11.04 Natty Narwhal

    Re: Does this tar script work?

    Now I get this error:

    Code:
    Starting backup of /home/oj/Documents/ to file /home/oj/Desktop/backup_20091027.tar.gz
    tar: Removing leading `/' from member names
    tar: /home/oj/Documents/backup\n: Cannot stat: No such file or directory
    tar: Exiting with failure status due to previous errors
    If I hash out the first if statement along with the tar line and the second if statement, in other words, just let the differential backup run, I get the same error as above.

  7. #7
    Join Date
    Feb 2007
    Beans
    4,045
    Distro
    Ubuntu 9.10 Karmic Koala

    Re: Does this tar script work?

    Sounds like you are missing -print0 at the end of the find-command.

  8. #8
    Join Date
    Sep 2007
    Location
    Malaysia
    Beans
    280
    Distro
    Ubuntu 11.04 Natty Narwhal

    [solved] Re: Does this tar script work?

    Thank you, that worked I think, didn't return an error in the log file. Thanks alot! I think I'll mark it as solved now.

  9. #9
    Join Date
    Apr 2009
    Location
    Located
    Beans
    236
    Distro
    Ubuntu 10.04 Lucid Lynx

    Re: Does this tar script work?

    Quote Originally Posted by psycho5 View Post
    Now I get this error:

    Code:
    Starting backup of /home/oj/Documents/ to file /home/oj/Desktop/backup_20091027.tar.gz
    tar: Removing leading `/' from member names
    tar: /home/oj/Documents/backup\n: Cannot stat: No such file or directory
    tar: Exiting with failure status due to previous errors
    If I hash out the first if statement along with the tar line and the second if statement, in other words, just let the differential backup run, I get the same error as above.
    Just ran into this problem while creating my first cron-job script...
    For me it was a file path problem.


    See if this works for you...
    Code:
    old_dir=`pwd`; # save current dir
    cd /home/oj/ ; # change to parent dir of compress folder
    
    if [ `date +%w` -eq 4 ] ; then
    	tar -cvzpf /Documents $BUDIR # compress the 'Documents' dir
    fi
    cd $old_dir; # change back to orgenal dir
    By going to the parent dir first, the compressed file will start with 'Documents' not '/home/oj/Documents/'
    And also fix the "Removing leading `/' from member names" error.

    [edit]
    a slow typer i am
    good to see you have it working.
    _
    Last edited by iMisspell; October 27th, 2009 at 08:12 AM. Reason: alittle late:)
    |
    --------------------------------------------------------------------------------------------------------
    ~ What was once an opinion, became a fact, to be later proven wrong ~

  10. #10
    Join Date
    Feb 2007
    Beans
    4,045
    Distro
    Ubuntu 9.10 Karmic Koala

    Re: Does this tar script work?

    Be careful with cd in scripts. If cd fails, you'll be issuing commands in the wrong directory. In this case, the tar will probably just fail, but what if it was rm for instance?

    Good practice is to always check the return value of cd. E.g.
    Code:
    cd /some/path || exit 1
    On another note, [ is ancient, and if you are using bash, you might as well use bash's better features, like [[ and ((.
    Code:
    dow=$(date +%w)
    if (( dow > 0 && dow <= 4 )); then ...; fi

Page 1 of 2 12 LastLast

Tags for this Thread

Bookmarks

Posting Permissions

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