[SLUG] Bash Scripts

Mark Feather akwe-xavante at firenet.uk.com
Sun Apr 13 22:54:00 BST 2003


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!

Some info has been deliberately changed such as usernames, passwords and ip
addresses!

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#####

Example Two
Script Name: create
Script Permissions: 700
To execute the script type "./create VariableName" at the prompt (without
the quotes) where the VariableName is the name of the parent directory to be
created as defined as $1 in the script.

Example: ./create yoursurname

#####Contents of Script#####

#!/bin/bash
#Auto-Create new web user directory & cgi-bin

echo
echo Creating New Web Directories!

#Declaration of Variables
     mkdir $1    #$1=VariableName#

     mkdir /usr/local/ftp/$1
     mkdir /usr/local/ftp/$1/cgi-bin

echo
echo Changing Directory Ownership

     chown $1 /usr/local/ftp/$1
     chown $1 /usr/local/ftp/$1/cgi-bin
echo
echo Changing Directory Permissions

     chmod 775 /usr/local/ftp/$1
     chmod 755 /usr/local/ftp/$1/cgi-bin

echo
echo Creating User Account Symlink

    cd /home/$1
    rmdir public_html
    ln -s /usr/local/ftp/$1 html

echo
echo New Web Directories Created
echo

#####End of Script Content#####





More information about the Scarborough mailing list