Find the answer to your Linux question:
Results 1 to 8 of 8
Hey there, i'm currently trying to make a backup script which will automatically back up files every hour storing them under the current date and time of the back up ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Just Joined!
    Join Date
    Nov 2011
    Posts
    6

    Question BASH backup script


    Hey there, i'm currently trying to make a backup script which will automatically back up files every hour storing them under the current date and time of the back up

    Heres my script so far:
    Code:
    #!/bin/sh
    for (( ; ; ))
    do
    cd /home/user/servers/ftb/backup/
    mkdir $(date +%F--%T)
    cp "/home/user/servers/ftb/FTB-Beta-A/." "/home/user/servers/ftb/backup/$(date +%F--%T)/" -R
    sleep 1h
    done
    I've entered in this script stright to the terminal window and it works fine, so I don't think it's an issues with the script. However when placed in a script under .sh and launched it just flashes up the terminal window and no backup is taken.

    I tried adding $SHELL to the end to see it was because it wasn't staying open at the end but this didn't help

    I'd be grateful of any input or help

    Thank you

  2. #2
    Trusted Penguin
    Join Date
    May 2011
    Posts
    4,307
    Hi,

    Quote Originally Posted by LightWrath
    However when placed in a script under .sh and launched it just flashes up the terminal window and no backup is taken.
    What does placing the script under .sh mean exactly?

    Quote Originally Posted by LightWrath
    I tried adding $SHELL to the end to see it was because it wasn't staying open at the end but this didn't help
    also not sure what you are trying to do here.

    How about adding a read to the end of the script, to make the script not exit (in case it is being run in an external terminal), e.g.:

    Code:
    for...
    # your code
    done
    read -p "Hold on a sec..."
    that way you can see any errors on the screen before it exits.

    you could also encase the whole thing in a bash function and when you call the function, redirect all output/errors to a log, e.g.:

    Code:
    myfunc() {
      for ...
      # your code
      done
    }
    
    myfunc >/tmp/output.log 2>&1
    also, is the script executable?

  3. #3
    Just Joined!
    Join Date
    Nov 2011
    Posts
    6
    Hey there Atreyu
    thanks for taking the time to reply

    What does placing the script under .sh mean exactly?
    As in to place the script into a text file, save it and make it executable to run the script. It was my understanding that such scripts are generally saved with the .sh extention.

    also not sure what you are trying to do here.
    The idea of this was to keep the terminal window open after it had run the script to see the output, this is from information i found on other fourms but I had no results with this.

    I tried adding the 2 suggestions to pause or give an output to a log. However once running the script from the file the terminal window is only open for a second and nothing executed no information been output in logs or in the window.

    I've used the chmod +x filename command and also done it from gnome, apon running I get prompted to run in terminal

  4. #4
    Trusted Penguin
    Join Date
    May 2011
    Posts
    4,307
    it sounds like the script is barfing before you get to the read call. you can put a read call at the very beginning of the script to see if that works.

    is there anything in the log file that should have been created (/tmp/output.log)?

    just so i understand, are you trying to execute the script by double-clicking on it in a file browser (like nautilus, etc.)?

  5. #5
    Just Joined!
    Join Date
    Nov 2011
    Posts
    6
    The script doesn't seem to reactor to having the read command at the start of it, still closes right at the start without any output.
    Regarding the log, a log file isn't even created when the code you suggested is added

    I'm using the script from a file browser and opening it that way, I run gnome on Debain 6 Squeeze, and double clicking the script file, it opens with the prompt to run in terminal, display, cancel or just run. Run does nothing, and run in terminal just flashes the terminal window.
    As in the image below
    servscript.jpg

  6. #6
    Trusted Penguin
    Join Date
    May 2011
    Posts
    4,307
    okay, i think the problem is your for loop syntax. Try this instead:

    Code:
    while :;
    do
    ...
    done
    apparently, other versions of Bash are more forgiving. i didn't figure it out until I busted out my Debian 6 laptop and tried your scrip verbatim.

  7. #7
    Just Joined!
    Join Date
    Nov 2011
    Posts
    6
    Hey there Atreyu,

    I tried this in which you listed before and it works perfect, even was able add some echo commands to list in the terminal the back up information.
    Thanks for your efforts and resolution to the problem

    -LightWrath

  8. #8
    Trusted Penguin
    Join Date
    May 2011
    Posts
    4,307
    Excellent, glad its sorted!

    I'll mark this one as Solved for you (btw, you can do that yourself any time you want via the Thread Tools link at the top of the page).

Posting Permissions

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