[SLUG] Bash Scripts

Al Girling al21 at firenet.uk.com
Mon Apr 14 12:56:01 BST 2003


On Sun, 13 Apr 2003 22:50:32 +0100
Mark Feather wrote:

> Somebody asked me to post a Bash Script at the last meeting, here are two
> working examples.  Bash Scripts are excellent for performing regular
> repetitive tasks like backing up files and directories scattered all over
> the place or auto creating sets of directories, sub-directories and files
> and changing ownership and permissions etc.  If anybody can find anything
> wrong with them or improve them at all please let me know.  Please bear in
> mind these are my first bash scripts!
> Example One
> Script Name: backup
> Script Permissions: 700
> To execute the script type "./backup" at the prompt (without the quotes)
> 
> Example: ./backup
> 
> #####Contents of Script#####
> 
> #!/bin/bash
> #backup of all web content
> #backup of customised configuration files & other important files
> #initiate file transfer to remote computer
> 
> echo
> 
> echo Creating Backup!
> cd /
> echo
> echo Creating Temporary Directories
> 
>      mkdir temp.dir.$(date +%d.%m.%y)
>      mkdir temp.dir.$(date +%d.%m.%y)/config.files
> echo
> echo Zipping Web Directories
> 
>       cd /usr/local
>       tar -czf ftp-$(date +%d.%m.%y).tgz ftp
>       mv ftp-$(date +%d.%m.%y).tgz /temp.dir.$(date +%d.%m.%y)
> 
>       tar -czf httpd-$(date +%d.%m.%y).tgz httpd
>       mv httpd-$(date +%d.%m.%y).tgz /temp.dir.$(date +%d.%m.%y)
> 
> cd /
> echo
> echo Copying Configuration Files & other important files!
> 
>      cp /etc/profile.local /temp.dir.$(date +%d.%m.%y)/config.files
>      cp backup /temp.dir.$(date +%d.%m.%y)/config.files
>      cp create /temp.dir.$(date +%d.%m.%y)/config.files
>      cp /home/feather/.alias /temp.dir.$(date +%d.%m.%y)/config.files
>      cp /etc/proftpd.conf /temp.dir.$(date +%d.%m.%y)/config.files
>      cp /etc/hosts.allow /temp.dir.$(date +%d.%m.%y)/config.files
>      cp /etc/hosts.deny /temp.dir.$(date +%d.%m.%y)/config.files
>      cp /etc/httpd/httpd.conf /temp.dir.$(date +%d.%m.%y)/config.files
> 
> cd /temp.dir.$(date +%d.%m.%y)
> echo
> echo Zipping Configuration Files
> 
>      tar -czf configs-$(date +%d.%m.%y).tgz config.files
>      rm -r config.files
> 
> cd /
> echo
> echo Moving Backed up Files
> 
>      mv temp.dir.$(date +%d.%m.%y) /home/feather/backup.$(date +%d.%m.%y)
>      chown feather /home/feather/backup.$(date +%d.%m.%y)
> 
> echo
> echo Backup ready for Remote Transfer, establishing remote connection
> 
>     cd /home/feather
>     ftp username&RemoteIpAddress
> 
> #manual entry of Password and file transfer required, haven't worked out how
> to write this into the script yet, i can
> #do it in a Windows script file but not Linux!!
> 
> echo
>     echo Remote Connection Closed, Backup and File transfer Complete
> echo
> 
> #####End of Script Content#####

Go on Mark.  You're just trying to scare people aren't you!  ;)

I like the use of 'echo' as it lets you know things are going O.K.  But I think you can thin your script quite a lot.

I had a good think this morning and came up with this script.  It's a little different as I'm saving the final file to a zip disc instead of using ftp to transfer it to another machine, but the idea is close enough.

My backup script;

#####script start#####

#!/bin/bash

cd /

echo Creating backup files!

mkdir bak
mkdir bak/conf

cp -a /home/al bak
cp -a /etc bak/conf

echo Compressing files!

tar czf bak-$(date +%d.%m.%y).tgz bak

echo Moving backup files!

mv bak*.tgz /mnt/zip

echo Removing temp files!

rm -dr bak

echo Done!

#####script end#####

I've added this script to /sbin so I can now 'su' to root.  Insert a backup zip disc.  Type 'bakup' on the command line, and watch it work! (O.K. slightly nervously ;) )

I've taken an easy option and chosen all of /etc and /home/al to backup.  Which grabs all I need with other files as well.  It would be easy to specify others with the 'cp -a' command.  I thought the script should clear up after itself also.  Hence the 'rm -dr' at the end.

A couple of things I'd like to add are a way of being notified if I've not put a disc in.  Something like a message saying.

"No disc found.  Please insert disc and press 'Enter' to continue."

Also, in the event that a Zip disc becomes full during the transfer process.  I'd like to be prompted for another disc.  I've know idea how either may be acheived so over to you!

As Mark said, it's very early days with writing bash scripts for both of us.  I'm sure Mark would agree that we'd both like to see our scripts improved.  So I'll offer a reward of a box of jelly babies to the person who writes a backup bash script the lug as a whole thinks is worthy of everyday use!  To be presented at the next meeting.

Al




More information about the Scarborough mailing list