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@xxxxxxxxxxxxxxxxxxxxxxxxxxxx>,
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@xxxxxxxx:/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@xxxxxxxx:/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, <tille>
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, <thegrendel(at)theriver.com>
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
.



Relevant Pages

  • Re: [ Attn: Randy ] Ad-hoc Parsing?
    ... thing I've been doing is comparing bash and batch scripting in Win/DOS, ... > Isn't this a compare of Unix and DOS shell scripts? ...
    (alt.lang.asm)
  • Re: Crazy(?) thought - run z/Linux binaries on z/OS UNIX.
    ... admin is free to either use packages coming from the well-tested frozen ... Now, I may be an ignorant, but what prevents you from compiling newer bash ... environment variables LD_LIBRARY_PATH and PATH one can force the OS to ... my opinion to write them in some other scripting language. ...
    (bit.listserv.ibm-main)
  • Re: Shell Games
    ... Scripting languages like PERL and Bash serve diffrent ... a great book is UNIX Shell ... > Just used a different scripting language. ...
    (freebsd-newbies)
  • Re: bash script mailing list?
    ... When you are scripting dialog using bash, ... > scripting related questions? ...
    (RedHat)
  • Re: Pulling Fields from LDAP
    ... I know this is an ADSI pull, but am I on the right track? ... A general AD programming guide? ... Inside Active Directory: A System Administrator's Guide ... Windows NT/2000 ADSI Scripting for System Administration ...
    (microsoft.public.win2000.active_directory)