Open main menu

CDOT Wiki β

Changes

OPS235 Lab 3 - CentOS7

3,729 bytes added, 06:56, 8 April 2015
no edit summary
You will continue our use of Bash Shell scripting by first creating a Bash Shell script that will allow the Linux sysadmin to select their created VMs for backup to root's home directory. Afterwards you will download, view and run a couple Bash Shell scripts that use the virsh command to start and stop your virtual machines. #xOpen a Bash shell terminal and login as root.# Use a text editor (such as <b><code><span style="color:#3366CC;font-size:1.2em;">vi</span></code></b> or <b><code><span style="color:#3366CC;font-size:1.2em;">nano</span></code></b>) to create a Bash Shell script called: <b><code><span style="color:#3366CC;font-size:1.2em;">backupVM.bash</span></code></b> in /root's home directory.# Enter the following text content into your text-editing session:<code style="color:#3366CC;font-family:courier;font-size:.9em;margin-left:20px;"><br>::&#35;!/bin/bash # Forces script to run in the bash shell::::::&#35; backupVM.bash::&#35; Purpose: Creates system info report::&#35;<br>&#35; USAGE: ./report.bash::&#35;::&#35; Author: *** INSERT YOUR NAME ***::&#35; Date: *** CURRENT DATE ***::::::if [ $HOME != "root" ] # only runs if logged in as root::then<br>&nbsp;echo "You must be logged in as root." >&2::&nbsp;exit 1::fi</code><br><ol><li value="4">Save your editing session, but remain in the text editor.</li><li>This shell script is designed particularly for your centos1, centos2, and centos3 VMS.</li><li>The code displayed below will prompt the user if they wish for all VMs to be backed-up; otherwise, allow the user the option of specifying which VMs to be backed-up. Add the following code</li></ol><br><code style="color:#3366CC;font-family:courier;font-size:.9em;"> :: read -p "Backup all VMs? (y|n):" answer # prompt if all VMs to be backed-up::::if [ "$answer" = "y" ] # Backup all VMs if answer is yes::then::&nbsp;for num in 1 2 3 # Determinant loop for 3 arguments: 1, 2, and 3::&nbsp;do::&nbsp;&nbsp;echo "Backing up VM #$num"::&nbsp;&nbsp;gzip < /var/lib/libvirt/images/centos$num.img > /root/centos$num.img.backup.gz::&nbsp;&nbsp;echo "VM #$num BACKUP DONE"::&nbsp;done ::elif [ "$answer" = "n" ]::then::&nbsp;read -p "Which VM should be backed up? (1/2/3): " numanswer::&nbsp;until echo $numanswer | grep "^[123]$" >> /dev/null # Look for match of single digit: 1,2, or 3::&nbsp;do::&nbsp;&nbsp;read -p "Invalid Selection. Select 1, 2, or 3: " numanswer::&nbsp;done::&nbsp;echo "Backing up VM #$numanswer"::&nbsp;gzip < /var/lib/libvirt/images/centos$numanswer.img > /root/centos$numanswer.img.backup.gz::&nbsp;echo "VM #$numanswer BACKUP DONE":::else::&nbsp;echo "Invalid Selection... Aborting program"::&nbsp;exit 2::fi </code>   <ol><li value="7">Save, set permissions, and then run that shell script to backup centos1. Confirm that this script did backup this image to root's home directory</li><li>Use the <b><code>wget</code></b> command to download, study, and run the following shell scripts on-line:<blockquote><b><code><span style=" pointer-events:none;cursor:default;color:#3366CC;font-size:1.2em;">https://scs.senecac.on.ca/~murray.saul/vm-start-text.bash<br>https://scs.senecac.on.ca/~murray.saul/vm-stop-text.bash</span></code></b><br><b><code><span style=" pointer-events:none;cursor:default;color:#3366CC;font-size:1.2em;">https://scs.senecac.on.ca/~murray.saul/vm-start.bash<br>https://scs.senecac.on.ca/~murray.saul/vm-stop.bash</span></code></b></blockquote></li><li>Try to understand what these Bash Shell scripts do.</li><li>#You have completed lab2. Proceed to Completing The Lab, and follow the instructions for "lab sign-off".</li></ol>'''Answer all observations / questions in your lab log book.'''
'''Answer Investigation 3 observations / questions in your lab log book.'''
13,420
edits