LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   newbie attempting to create a script and seriously need help (https://www.linuxquestions.org/questions/programming-9/newbie-attempting-to-create-a-script-and-seriously-need-help-4175435106/)

completelinuxnube 11-01-2012 01:04 PM

newbie attempting to create a script and seriously need help
 
hello friends. I am attempting to create an rsync backup script to backup three linux servers. I have tried for a while now with no real luck. I have figured out the commands that I want to use, but when it comes to adding them all together in the script, I have no idea. I have do many hours of research and have came up with nothing that I can really use, or at least lack the knowledge to use. Everything fails, mostly due to syntax errors, but I am new to bash scripting and new to Linux. I am now faced with a deadline and I am running out of time. Please help. I really want to learn and I will as soon as I get this project out of the way. I will post some information below about the servers and what I am looking to accomplish. If requested I will post some examples of what I have tried to do also.


Server Backup Info

Devices being backed up

Dns1:

IP: 192.168.1.1
NetBios Name: dns1
Rsync version: 2.6.9 protocol version 29
Backup storage location on nas2: /raid0/data/backups/dns1/
Rsync command: rsync –avvh –exclude-from=rsync-exclude –progress –e ssh / root@192.168.1.100:/raid0/data/backups/dns1/

Services:

IP: 192.168.1.7
NetBios Name: services
Rsync version: 2.6.9 protocol version 29
Backup storage location on nas2: /raid0/data/backups/services/
Rsync command: rsync –avvh --exclude-from=rsync-exclude –progress –e ssh / root@192.168.1.100:/raid0/data/backups/services/

Inet1:

IP: 192.168.1.2
NetBios Name: inet1
Rsync version: 3.0.8 protocol version 30
Backup storage location on nas2: /raid0/data/backups/inet1/
Rsync command: rsync –avvXh –exclude-from=rsync-exclude –progress –e ssh / root@192.168.1.100:/raid0/data/backups/inet1/

Storage device for backups

Nas2:

IP: 192.168.1.100
NetBios Name: nas2
Rsync version: 3.0.8 protocol version 30
Location of server backups: /raid0/data/backups/



What the script should do:

1) Upon starting the script it should display the present date and show a welcome message such as the example below:

Welcome to the Server Backup Program
Today is Wed Oct 31 10:34:22 CDT 2012

2) Next, this message would appear: What would you like to do? Backup or Restore? <Here I would select either B for Backup or R for Restore.
If I were to choose Backup, it would ask: Would you like to backup a single server or multiple servers?
If I choose single, by pressing S, it would ask: Which server would you like to backup? At this point, I could choose the server from a list by inputting a number or letter.
Then it would display a message saying: You are about to backup <server name here>, are you sure you wish to continue? At this point I would select Y for Yes or N for
No. Selecting Yes would run the command associated with that server (commands are listed above) and selecting No, would exit the program or revert back to the very
start of the program.

If I were to select M for multiple servers, it would ask: Which servers would you like to backup? <I would select them from a list by inputting the number that
corresponds to those servers. After making my selection, the program would display a message saying: You are about to backup the following servers: <server1>
<server2> <server3>, do you wish you proceed? I would select Y for Yes or N for No. Selecting Yes would run all three Rsync backup commands listed above. Selecting
no would exit the program or revert to program start.


If I were to choose Restore, it would ask: Which server would you like to restore? <At this point I would select the server from a list by inputting a number or letter.
After making my selection, a message would be displayed: You are about to Restore <server name here>, are you sure you wish to continue? I would enter Y for Yes or N
for No. Selecting Yes would run the script and selecting no would exit the program or revert back to the very start of the program.


3) After the process has completed, I would like a message to be displayed stating if the backup succeeded or if it failed. I would also need a log file to be created in the home
directory.


This is how I envision the Program:

Welcome to the Server Backup Program
Today is Oct 31 10:34:22 CDT 2012

What would you like to do? [B]ackup or [R]estore?
Please press B for Backup or R for Restore.


(I pressed B in this example)

Would you like to backup a single server or multiple servers? Please press S for Single or M for Multiple.
(I pressed S in this example)

Please select a server to backup.
1 dns1
2 services
3 inet1

(I pressed 1 in this example)

You are about to backup dns1, are you sure you wish to proceed? [Y]es or [N]o.
Please enter Y for Yes or N for No.

(I chose yes in this example)
Now Backing up dns1………..

Progress


And



Verbosity


Here


(The script finishes & this message is displayed if successful)

The backup operation was successful! A log file was created and placed in your home directory.

(The program terminates)


All responses will be greatly appreciated. Thanks.

unSpawn 11-01-2012 01:16 PM

Quote:

Originally Posted by completelinuxnube (Post 4819866)
(..)I am attempting to create an rsync backup script(..)

Please post what you tried already.

completelinuxnube 11-01-2012 01:42 PM

I do not have any really relevant, just a bunch of different attempts at scripts to become familiar with some of the commands that I think I might would need to use. Like instance the select and case commands. None of which has actually been applied toward a backup script of any kind yet, as I am still trying to figure out how actually to use the commands. Would you like me to post these? There will be several.

TobiSGD 11-01-2012 02:12 PM

Well, you have already your rsync commands (watch your options, it is --progress, not -progress), so all you need is the user interface. It should not really be a problem to generate some kind of basic skeleton for the script that prints out the information you want and waits for input, especially since you have an exact plan how the script should react.
May be you just begin with posting what you have done in that area. Please use code-tags for that to preserve the formatting and make your code more readable.

H_TeXMeX_H 11-01-2012 02:29 PM

It's not that difficult, and surely you should have been able to complete a few of the numbers:

1) Use echo and date.
2) Use a loop and nested case statements.
3) You use an 'if' statement to check that rsync succeeded, as well as the verbose option and pipe it to a file.

Here's some links that may help:
http://www.tldp.org/LDP/abs/html/index.html
http://www.grymoire.com/Unix/

completelinuxnube 11-01-2012 03:15 PM

This was just an experiment, trying to figure out how to actually use the commands. It does not relate to my needs 100%, but I had to try something. As you can imagine, it did not work and has plenty of syntax errors, but I am completely new to all of this.

Code:

#!/bin/bash
#
#This is a test script for backup
#
#
echo "Welcome to the Server Backup Program."
echo "Today is "`date`""
echo
echo "Would you like to create a backup or restore from one?"
read -p "Press [B] for Backup or [R] for Restore."
select br in "Backup" "Restore"; do
        case $br in
                        Backup ) echo "You are about to backup the system, are you sure?"
                                      select yn in "Yes" "No"; do
                                                case $yn in
                                                                Yes ) echo "Backup has started."; exec mybackup.sh
                                                                        No ) echo "The program will now exit."; exit;;
                                                        esac               
                                Restore ) echo "You are about to restore the system, are you sure?"
                        select yn in "Yes" "No"; do
                                case $yn "Yes" "No"; do
                                        Yes ) echo "The restore process has begun."; exec restore.sh
                                        No ) echo "The program will now exit."; exit;;
                                esac
                esac
done


Snark1994 11-01-2012 03:43 PM

Syntax errors are easy to fix: I can spot
  • 4 missing ';;'s (it needs to be at the end of each option in a case statement)
  • 2 missing 'done's (each 'do' needs a matching 'done')
  • 'case $yn "Yes" "No"; do' should be 'case $yn in' (you're getting mixed up with "select...")
  • Finally, you have a "read" statement which says "Press [B] for backup etc." and then in the next line you have "select br in "Backup" "Restore""! Keep the 'select' one, as that's the one which is actually doing something

Other than that, your programme looks fine - you just need to create mybackup.sh. As for working out where the errors are, just run the programme and see what syntax errors it complains about. The errors normally aren't that helpful per se unless you're experienced, but they should tell you the line on which they occurred, so you can work out what you need to do from there.

Hope this helps,

completelinuxnube 11-05-2012 08:40 AM

thanks everyone.

linosaurusroot 11-05-2012 10:25 AM

case "$yn" in

The quotes force the script to make sense even if you arrive there with the variable not set.

completelinuxnube 11-16-2012 01:04 PM

Hello Friends, I'm back with some new stuff. Can you help me figure this out?
 
Hello Again Friends

I'm still working on a backup and restore script. What I have ended up with is this. I have a task selector script
that I've written. This script allows you too select either the backup or restore task. Once chosen, a secondary script
pertaining to that task is launched. Say you chose the backup task, then the backup script launches. Here you are asked
which server you want to backup or if you want to backup them all, then you are asked to confirm your choice. The problem
is, I don't know how to fit the third script that I written into the second one to make it function correctly. Also, I don't
know if I should write individual scripts for each server or have just one script with all the rsync commands for the different
servers. I really have no idea what I'm doing. Can someone please help? I have given a good go. I will post my scripts below in
order. Thanks Friends.

PHP Code:

Here is my first script called task_select.sh 

Code:

#!/bin/bash
#
# This is the Task Selector script for Our Backup and Restore
# subscripts.
#
RIGHT_NOW=$(date +"%x %r %Z")
clear
echo "                         
                          ***************************************
                          *                Welcome              *
                                *    to Bearing Service & Supply's  *
                                *        Task Selection Program      *
                                * Today is $RIGHT_NOW *                 
                          ***************************************"
               
echo
echo "Please select a task:"
echo "
-----------------
      Tasks
-----------------
b - Backup
r - Restore
-----------------
q - Quit Program
"
echo -n "Enter your selection:> "
read task; echo ""
case $task in
        b ) clear; echo "Entering the backup program. Please wait..."; sleep 4; exec ./backup.sh
            ;;
        r ) echo "Entering the restore program. Please wait..."; sleep 4; # exec ./restore.sh
            ;;
        q ) echo "Exiting the Program. Goodbye!"; echo ""; sleep 2; exit
            ;;       
        * ) echo ""; echo "You failed to choose a task. Please try again."; sleep 4; exec ./task_select.sh
            ;;       
esac


PHP Code:

Here is my second script called backup.sh 

Code:

#!/bin/bash
#
# This script is a subscript of the main
# task_selector script. This subscript contains
# the backup portion of the program.
#
#
clear
RIGHT_NOW=$(date +"%x %r %Z")
echo "
                                *******************
                                *  Welcome to the *
                                *  Backup Program *
                                *******************"
echo "This program will allow us to create backups of our Linux servers.
You may backup a single server or all three."
echo ""
echo "Please make a choice from the list below and press enter:"
echo "Note: You must choose from the available options or the program will restart!"
echo "
  ----------------
      Servers
  ----------------   
  1 - dns1
  2 - services
  3 - inet1
  4 - all
  q - quit
  "
echo -n "Please make a selection:> "
read -r choice; echo ""
case $choice in
        1 ) echo "So you want to backup dns1?"
                ;;
        2 ) echo "So you want to backup services?"
                ;;
        3 ) echo "So you want to backup inet1?"
                ;;
        4 ) echo "So you want to backup all servers?"
                ;;
        q ) echo "Exiting the program!"; echo ""; sleep 3; exit
                ;;
        * ) echo "You made an invalid choice, the program is restarting!"; sleep 4; exec ./backup.sh
                ;;
esac               
echo -n "yes or no:> "
read -r answer; echo ""; clear
  case $answer in
        yes ) echo "Requesting subsript. Press ctrl-c to cancel."; sleep 3
              ;;
        no ) echo "Ok, action aborted. Program restarting."; sleep 3; exec ./backup.sh
            ;; 
        * ) echo "You should have typed yes or no. Try again. Program Restarting"; sleep 3; exec ./backup.sh
            ;; 
  esac


PHP Code:

Here are my actual backup scriptsthis one is called backup_all.sh 

Code:

#!/bin/bash

#
# This script will backup all 3 Linux Servers
# at the same time.
#

# Variable Declarations
LOG=/home/johnny/log/rsync_backup."$(date +%Y%m%d)".log    # Added quotes around date since PHP
                                                          # was messing up the display.
#LOG=                                        # Uncomment after testing
RSYNC=/usr/bin/rsync                                     

# Destination Server where backups will be stored
SERVER=testbox                        # For testing purposes, comment when complete
#SERVER=nas2                    # Uncomment this when testing is complete       

# Rsync Exclusion Lists
EXCL1=/home/johnny/backup/dns1-exclude                      # All lists are in the same directory
EXCL2=/home/johnny/backup/services-exclude
EXCL3=/home/johnny/backup/inet1-exclude

# Servers being backed up
SERV1=dns1
SERV2=services
SERV3=inet1

# Target Storage Location for backup
STOR1=/home/johnny/backup/dns1/
STOR2=/home/johnny/backup/services/
STOR3=/home/johnny/backup/inet1/

# User
USER=root

# Date
DATE=/bin/date
ECHO=/bin/echo

# Starting Task......
echo
echo "This is the multi-server backup program.
It will backup all servers, one by one."
echo
echo "Starting backup task...."; sleep 2
echo "" >> $LOG 2>&1
$ECHO "Task begin...." >> $LOG 2>&1
$ECHO "*****************************************************************************" >> $LOG 2>&1
$DATE >> $LOG 2>&1
$ECHO "*****************************************************************************" >> $LOG 2>&1
echo
# Rsync Command.....
echo "" >> $LOG 2>&1
$ECHO "Starting backup of DNS1 ...." >> $LOG 2>&1
$ECHO "*****************************************************************************" >> $LOG 2>&1
$DATE >> $LOG 2>&1
$ECHO "*****************************************************************************" >> $LOG 2>&1
$RSYNC -avh --exclude-from=$EXCL1 --progress -e ssh $USER@$SERV1:/ $STOR1 >> $LOG 2>&1
echo
echo "" >> $LOG 2>&1
$ECHO "Starting backup of SERVICES ...." >> $LOG 2>&1
$ECHO "*****************************************************************************" >> $LOG 2>&1
$DATE >> $LOG 2>&1
$ECHO "*****************************************************************************" >> $LOG 2>&1
$RSYNC -avh --exclude-from=$EXCL2 --progress -e ssh $USER@$SERV2:/ $STOR2 >> $LOG 2>&1
echo
echo "" >> $LOG 2>&1
$ECHO "Starting backup of INET1 ...." >> $LOG 2>&1
$ECHO "*****************************************************************************" >> $LOG 2>&1
$DATE >> $LOG 2>&1
$ECHO "*****************************************************************************" >> $LOG 2>&1
$RSYNC -avXh --exclude-from=$EXCL3 --progress -e ssh $USER@$SERV3:/ $STOR3 >> $LOG 2>&1
echo
# Ending Task.....
$ECHO "*****************************************************************************" >> $LOG 2>&1
$ECHO "Completing task...." >> $LOG 2>&1
$DATE >> $LOG 2>&1
$ECHO "*****************************************************************************" >> $LOG 2>&1
$ECHO "Task end......" >>$LOG 2>&1

PHP Code:

And my final script is an individual script for my dns server dns1.sh 

Code:

#!/bin/bash
#
echo "Starting rsync backup for dns1."; sleep 3
rsync -avvh --exclude-from=dns1-exclude --progress -e ssh root@dns1:/ /home/johnny/backup/dns1/

As you can tell, I'm far from finished. I haven't even created a restore script yet and have two more individual backup scripts to create for 2 other servers.
If I knew what I was doing, I could have probably put all this together in one script, but I haven't gotten a clue.

unSpawn 11-16-2012 03:13 PM

Code:

#!/bin/bash -vxe
progn=${0//*\//}; progn=${progn//.*/}
LANG=C; LC_ALL=C; export LANG LC_ALL
_help() { echo "${progn}: [ -a (all) | -d (DNS only) | -m menu | -h ] "; exit 0; }
[ $# -eq 0 -o "${1:0:1}" != "-" ] && _help
RSYNC=/usr/bin/rsync; SERVER=testbox; USER=root; DATE=/bin/date; ECHO=/bin/echo
EXCL1=/home/johnny/backup/dns1-exclude; EXCL2=/home/johnny/backup/services-exclude;
EXCL3=/home/johnny/backup/inet1-exclude; SERV1=dns1; SERV2=services; SERV3=inet1
STOR1=/home/johnny/backup/dns1/; STOR2=/home/johnny/backup/services/; STOR3=/home/johnny/backup/inet1/
LOG="/home/johnny/log/rsync_backup.$(date +%Y%m%d).log"
_backupDNS() { $RSYNC -avh --exclude-from=$EXCL1 --progress -e ssh $USER@$SERV1:/ $STOR1 >> $LOG 2>&1; }
_backupSVCS() { $RSYNC -avh --exclude-from=$EXCL2 --progress -e ssh $USER@$SERV2:/ $STOR2 >> $LOG 2>&1; }
_backupINET1() { $RSYNC -avXh --exclude-from=$EXCL3 --progress -e ssh $USER@$SERV3:/ $STOR3 >> $LOG 2>&1; }
_backupNAS() { echo whatever /raid0/data/backups/; }
_backupEVERYTHING() { _backupDNS; _backupSVCS; _backupINET1; }
doMainMenu() { echo "Select backup source: "
select ITEM in quit DNS Services INET1 NAS Everything; do case "${ITEM}" in quit) exit 0;;
DNS) _backupDNS;; Services) _backupSVCS;; INET1) _backupINET1;; NAS) _backupNAS;; Everything)
_backupEVERYTHING;; *) _help;; esac; done; }
while getopts admh OPT; do case "${OPT}" in a) _backupEVERYTHING;; d) _backupDNS;;
m) doMainMenu;; h) _help;; esac; done
exit 0

Not free from improvement and YMMV(VM).

completelinuxnube 11-19-2012 03:01 PM

@unSpawn, I want to thank you for the info, but keep in mind I am just a newbie with all of this. Can you please explain to me, what it is exactly that you have posted?

Thanks

unSpawn 11-19-2012 08:44 PM

This should be easier to read:
Code:

#!/bin/bash

_help() {
 echo "myscript: [ -a (all) | -d (DNS only) | -m menu | -h ] "; exit 0
 }

if [ $# -eq 0 -o "${1:0:1}" != "-" ]; then
 _help
fi

RSYNC=/usr/bin/rsync
SERVER=testbox
USER=root
DATE=/bin/date
ECHO=/bin/echo

EXCL1=/home/johnny/backup/dns1-exclude
EXCL2=/home/johnny/backup/services-exclude;
EXCL3=/home/johnny/backup/inet1-exclude

SERV1=dns1
SERV2=services
SERV3=inet1

STOR1=/home/johnny/backup/dns1/
STOR2=/home/johnny/backup/services/
STOR3=/home/johnny/backup/inet1/

LOG="/home/johnny/log/rsync_backup.$(date +%Y%m%d).log"

doMainMenu() {
 echo "Select backup source: "
 select ITEM in quit DNS Services INET1 NAS; do
 case "${ITEM}" in
      quit) exit 0;;
        DNS)  $RSYNC -avh --exclude-from=$EXCL1 --progress -e ssh $USER@$SERV1:/ $STOR1 >> $LOG 2>&1;;
  Services)  $RSYNC -avh --exclude-from=$EXCL2 --progress -e ssh $USER@$SERV2:/ $STOR2 >> $LOG 2>&1;;
      INET1)  $RSYNC -avXh --exclude-from=$EXCL3 --progress -e ssh $USER@$SERV3:/ $STOR3 >> $LOG 2>&1;;
        NAS)  echo whatever /raid0/data/backups/;;
          *) _help;;
 esac
done
 }

while getopts dmh OPT; do
 case "${OPT}" in
  d) _backupDNS;;
  m) doMainMenu;;
  h) _help;;
 esac
done

exit 0

just try it and check some basic Bash scripting guides while you're at it:
http://www.tldp.org/HOWTO/Bash-Prog-Intro-HOWTO.html
http://www.tldp.org/LDP/Bash-Beginne...tml/index.html
http://www.gnu.org/software/bash/man...ode/index.html
http://www.grymoire.com/Unix/Sh.html
http://mywiki.wooledge.org/BashFAQ
http://mywiki.wooledge.org/BashPitfalls
http://www.tldp.org/LDP/abs/html/

completelinuxnube 11-20-2012 08:19 AM

@unSpawn, thank you very much and yes that is much easier to read and I understand more clearly now. I will do as you have instructed and view the guides you have suggested. I will write back soon with an update on the knowledge that I have gained.

Thanks again unSpawn, it's people like you that make a difference!!!


All times are GMT -5. The time now is 04:42 AM.