Shell script to do a backup and then shutdown the machine. - Suse

This is a discussion on Shell script to do a backup and then shutdown the machine. - Suse ; Hi there! I hope you don't mind some newbie questions about shell scripts. I would like to write a shell script which will be executed when the machine boots. The computer's BIOS has an option which will switch on the ...

+ Reply to Thread
Results 1 to 3 of 3

Thread: Shell script to do a backup and then shutdown the machine.

  1. Shell script to do a backup and then shutdown the machine.

    Hi there!

    I hope you don't mind some newbie questions about shell scripts.

    I would like to write a shell script which will be executed when the
    machine boots. The computer's BIOS has an option which will switch on
    the computer at a certain time each day (like 2 a.m.)

    The script rsyncs another file server's data (creating a backup) and
    then shuts down the computer.

    The script
    1. Wait 30 secs before continuing. (I am hoping that I can abort the
    script with ctl-z during this wait period in case I need to do
    something else on the computer.) I don't know what command to execute
    to do this.

    2. Run a rsync command: rsync -vza root@10.0.0.7:/home/. /backup/file-
    server/home

    3. Shut down the computer, I assume that this is the correct command:
    "shutdown -h now"

    I've never written a shell script before, so any help would be
    appreciated.

    After the script is written where should I put it to be executed
    during startup?

    Thanks in advance,
    Dan

  2. Re: Shell script to do a backup and then shutdown the machine.

    DanEskildsen wrote:
    > The script
    > 1. Wait 30 secs before continuing. (I am hoping that I can abort the
    > script with ctl-z during this wait period in case I need to do
    > something else on the computer.) I don't know what command to execute
    > to do this.


    Wait 30 seconds is `sleep 30`. `killall scriptname` will stop the
    script.

    > 2. Run a rsync command: rsync -vza root@10.0.0.7:/home/. /backup/file-
    > server/home


    OK.

    > 3. Shut down the computer, I assume that this is the correct command:
    > "shutdown -h now"


    OK

    > I've never written a shell script before, so any help would be
    > appreciated.


    First read http://tldp.org/LDP/Bash-Beginners-Guide/html/ and
    http://www.tldp.org/LDP/abs/html/

    I think you might be a bit over your head with it as this is your first
    one with such an important task.

    > After the script is written where should I put it to be executed
    > during startup?


    So what your PC will be doing is start up, do the backup and then shut
    down again. That means you won't be doing anything else with it?

    First the script. Basicaly you put all the commands you do in a terminal
    in a file that you start with a sha-bang. e.g. (untested)
    #!/bin/bash
    set -x
    sleep 30
    rsync -vza root@10.0.0.7:/home/. /backup/file-server/home
    shutdown -h now

    Next you do a `chmod +x` on that file. Test it by doing `./script`. Do
    not forget the ./ or any other way to go directly to the script.

    If that works, put a # in front of the `set -x` and test again. Then
    move it to either /usr/local/bin or in this case, as it needs to be run
    by root to /usr/local/sbin

    Once all that works, use groups.google on how to run something on
    startup.

    Some remarks:
    1) As you descibe it, after the booting, the script will run and shut
    down again. You won't be able to use the PC for anything else. Be aware
    of that
    2) As you describe it, there is no testing or fallback whatsoever, so if
    the backup fails, you will not have a backup.
    3) As you descibe it, you will just backup /home and not other importand
    data on that machine
    4) There is a difference between a backup and a copy. Be aware that with
    this, you will backup errors as well.

    Writing scripts is not hard, but I would not start with a backup script.

    houghi
    --
    This was written under the influence of the following:
    | Artist : Rabauken
    | Song : hey, mein freund
    | Album : hey mein freund

  3. Re: Shell script to do a backup and then shutdown the machine.

    On Fri, 17 Oct 2008, in the Usenet newsgroup alt.os.linux.suse, in article
    <5d69caba-5abc-4c72-a607-effbc44c463b@t54g2000hsg.googlegroups.com>,
    DanEskildsen wrote:

    NOTE: Posting from groups.google.com (or some web-forums) dramatically
    reduces the chance of your post being seen. Find a real news server.

    >I hope you don't mind some newbie questions about shell scripts.


    Why not?

    >I would like to write a shell script which will be executed when the
    >machine boots. The computer's BIOS has an option which will switch on
    >the computer at a certain time each day (like 2 a.m.)
    >
    >The script rsyncs another file server's data (creating a backup) and
    >then shuts down the computer.


    That's pretty risky - I would put a lot of checks before doing this.

    >The script
    >1. Wait 30 secs before continuing. (I am hoping that I can abort the
    >script with ctl-z during this wait period in case I need to do
    >something else on the computer.) I don't know what command to execute
    >to do this.


    [compton ~]$ whatis sleep
    sleep (1) - delay for a specified amount of time
    sleep (3) - Sleep for the specified number of seconds
    [compton ~]$

    However, what I would do would be to allow the computer to boot, then
    run a script that checks what time it is...

    [compton ~]$ whatis date
    date (1) - print or set the system date and time
    [compton ~]$ date +%X
    14:26:50
    [compton ~]$ date +%H
    14
    [compton ~]$

    and if the hour is the middle of the night, THEN run this backup
    command.

    >2. Run a rsync command: rsync -vza root@10.0.0.7:/home/. /backup/file-
    >server/home
    >
    >3. Shut down the computer, I assume that this is the correct command:
    >"shutdown -h now"


    if [ `date +%H` = "02" ] ; then
    rsync -vza root@10.0.0.7:/home/. /backup/file-server/home
    sleep 90
    /sbin/shutdown -h now
    exit
    else
    exit 1
    fi

    which says to see if the hour is 02:xx, and if so, run the rsync
    command you've shown, then wait 90 seconds and shut this system down.
    If it's NOT 02:xx, the script exits, and the system is available for
    you to use. NOTE: This isn't tested, as my systems remain on 24/7.

    >I've never written a shell script before, so any help would be
    >appreciated.


    Start with the howtos - specifically

    -rw-rw-r-- 1 gferg ldp 31540 Jul 27 2000 Bash-Prog-Intro-HOWTO

    then wander over to the Linux Documentation Project, and grab

    * Bash Guide for Beginners
    version: 1.10
    author: Machtelt Garrels,
    last update: Jun 2008
    ISBN: 0-9744339-4-2
    available formats:
    1. HTML (read online) 2. HTML (read online, single file, 531k)
    3. HTML (tarred and gzipped package, 399k) 4. PDF (1.2M)
    5. PostScript (1.0M) 6. text (106k)
    7. PluckerDB (165k)

    The Bash Guide for Beginners gets you started with Bash scripting
    and bridges the gap between the Bash HOWTO and the Advanced Bash
    Scripting Guide. Everybody who wants to make life easier on
    themselves, power users and sysadmins alike, can benefit from
    reading this practical course. The guide contains lots of examples
    and exercises at the end of each chapter, demonstrating the theory
    and helping you practice. Bash is available on a wide variety of
    UNIX, Linux, MS Windows and other systems.

    * Advanced Bash-Scripting Guide
    version: 5.4
    author: Mendel Cooper,
    last update: Jul 2008
    available formats:
    1. HTML (read online) 2. HTML (read online, single file, 2.1M)
    3. HTML (tarred and gzipped package, 596K) 4. PDF (2.4M)
    5. PostScript (1.3M) 6. text (469K)
    7. PluckerDB (617K)

    This document is both a tutorial and a reference on shell scripting
    with Bash. It assumes no previous knowledge of scripting or
    programming, but progresses rapidly toward an intermediate/advanced
    level of instruction. The exercises and heavily-commented examples
    invite active reader participation. Still, it is a work in progress.
    The intention is to add much supplementary material in future
    updates to this document, as it evolves into a comprehensive book
    that matches or surpasses any of the shell scripting manuals in
    print.

    >After the script is written where should I put it to be executed
    >during startup?


    This depends on the distribution you are using. As this newsgroup is
    alt.os.linux.suse, that probably means SuSE, and that means
    the boot.local file

    Old guy

+ Reply to Thread