Back up shell script instance code

Source: Internet
Author: User

1. backup_run.sh Copy codeThe Code is as follows :#! /Bin/sh
# Backup_run
# Script to run the backups
# Loads in a setting file for the user to change
SOURCE =/home/bob/backup. defaults
Check_source ()
{
# Check_source
# Can we load the file
# Backup. defaults is the source file containing config/functions
# Make sure your path already des this directory you are runing from
If [-r $ SOURCE]; then
. $ SOURCE # load $ source
Else
Echo "'basename $ 0': cannot locate default file"
Exit 1
Fi
}
Header ()
{
# Header
USER = 'whoam'
MYDATE = 'date + % A "" % e "of" % B-% y'
Clear
Cat <HH
User: $ USER $ MYDATE
NETWORK SYSTEM BACKUP
==================================
HH
}
Change_settings ()
{
# Change_settings
# Let the user see the default settings ..
Header
Echo "Valid Entries Are ..."
Echo "Tape Device: rmt0, rmt1, rmt3"
Echo "Mail Admin: yes, no"
Echo "Backup Type: full, normal, sybase"
While:
Do
Echo-n-c "\ n Tape Device To Be Used For This Backup [$ _ DEVICE]:"
Read T_DEVICE
:$ {T_DEVICE :=$ _ DEVICE}
Case $ T_DEVICE in
Rmt0 | rmt1 | rmt3) break

*) Echo "The device are either... rmt0, rmt1, rmt3"

Esac
Done
# If the user hits return on any of the fields, the default value will be used
While:
Do
Echo-n "Mail Admin When Done [$ _ INFORM]:"
Read T_INFORM
:$ {T_INFORM :=$ _ INFORM}
Case $ T_INFORM in
Yes | Yes) break

No | No) break

*) Echo "The choices are yes, no"

Esac
Done
While:
Do
Echo-n "Backup Type [$ _ TYPE]:"
Read T_TYPE
:$ {T_TYPE: =$ _ TYPE}
Case $ T_TYPE in
Full | full) break

Normal | normal) break

Sybase | sybase) break

*) Echo "The choices are either... full, normal, sybase"
Esac
Done
# Re-assign the temp varialbes back to original variables that
# Were loaded in
_ DEVICE = $ T_DEVICE; _ INFORM = $ T_INFORM; _ TYPE = $ T_TYPE
}
Show_settings ()
{
# Display current settings
Cat <HH
Default Settings Are...
Tape Device To Be Used: $ _ DEVICE
Mail Admin When Done: $ _ INFORM
Type Of Backup: $ _ TYPE
Log File Of Backup: $ _ LOGFILE
HH
}
Get_code ()
{
# Users get 3 attempts at entering the correct code
# _ CODE is loaded in from the source file
Clear
Header
_ COUNTER = 0
Echo "you must enter the correct code to be able to change default settings"
While:
Do
_ COUNTER = 'expr $ _ COUNTER + 1'
Echo-n "Enter the code to change the settings :"
Read T_CODE
# Echo $ _ COUNTER
If ["$ T_CODE" = "$ _ CODE"]; then
Return 0
Else
If ["$ _ COUNTER"-gt 3]; then
Echo "Sorry incorrect code entered, you cannot change the settings ..."
Return 1
Fi
Fi
Done
}
Check_drive ()
{
# Make sure we can rewind the tape
Mt-f/dev/$ _ DEVICE rewind>/dev/null 2> & 1
If [$? -Ne 0]; then
Return 1
Else
Return 0
Fi
}
#==================================== Main ================= ========
# Can we source the file
Check_source
Header
# Display the loaded in variables
Show_settings
# Ask user if he/she wants to change any settings
If continue_prompt "Do you wish To Change Some of The System Defaults" "Y ";
Then
Echo $?
# Yes then enter code name
If get_code; then
# Change some settings
Change_settings
Fi
Fi
# ---------------- Got settings... now do backup
If check_drive; then
Echo "tape OK ..."
Else
Echo "Cannot rewind the tape .. Is it in the tape drive ??? "
Echo "check it out"
Exit 1
Fi
# File system paths to backup
Case $ _ TYPE in
Full | full)
BACKUP_PATH = "sybase syb/suppor etc var bin apps usr/local"

Normal | normal)
BACKUP_PATH = "etc var bin apps usr/local"

Sybase | sybase)
BACKUP_PATH = "sybase syb/suppor"

Esac
# Now for backup
Cd/
Echo "Now starting backup ......"
Find $ BACKUP_PATH-print | cpio-ovB-O/dev/$ _ DEVICE >>$ _ LOGFILE 2> & 1
# If the above cpio does not work on your system try cpio below, instead
# Find $ BACKUP_PATH-print | cpio-ovB>/dev/$ _ DEVICE >>$ _ LOGFILE 2> & 1
# To get more information on the tape change-ovB to-ovcC66536
If ["$ _ INFORM" = "yes"]; then
Echo "Backup finished check the log file" | mail admin
Fi

2. backup. defaultsCopy codeThe Code is as follows :#! /Bin/sh
# Backup. defaults
# Configuration default file for network backups
# Edit this file at your own
# Name backup. defaults
#--------------------------------------------
# Not necessary for the environments to be in quotes ..
_ CODE = "comet"
_ LOGFILE = "/appdva/backup/log. 'date + % y % m % d '"
_ DEVICE = "rmt0"
_ INFORM = "yes"
_ TYPE = "Full"
#---------------------------------------------
Continue_prompt ()
{
# Continue_prompt
# To call: continue_prompt "string to display" default_answer
_ STR = $1
_ DEFAULT = $2
# Check we have the right params
If [$ #-lt 1]; then
Echo "continue_prompt: I need a string to display"
Return 1
Fi
While:
Do
Echo-n "$ _ STR [Y .. N] [$ _ DEFAULT]:"
Read _ ANS
If ["$ _ ANS" = ""]; then
:$ {_ ANS: =$ _ DEFAULT}
Case $ _ ANS in
Y) return 0

N) return 1

Esac
Fi
# User has selected something
Case $ _ ANS in
Y | Y | Yes | YES)
Return 0

N | N | No | NO)
Return 1

*) Echo "Answer either Y or N, default is $ _ DEFAULT"

Esac
Echo $ _ ANS
Done
}

3. Run:Copy codeThe Code is as follows: $./backup_run.sh backup. defaults

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.