13,420
edits
Changes
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>::#!/bin/bash # Forces script to run in the bash shell::::::# backupVM.bash::# Purpose: Creates system info report::#<br># USAGE: ./report.bash::#::# Author: *** INSERT YOUR NAME ***::# Date: *** CURRENT DATE ***::::::if [ $HOME != "root" ] # only runs if logged in as root::then<br> echo "You must be logged in as root." >&2:: 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:: for num in 1 2 3 # Determinant loop for 3 arguments: 1, 2, and 3:: do:: echo "Backing up VM #$num":: gzip < /var/lib/libvirt/images/centos$num.img > /root/centos$num.img.backup.gz:: echo "VM #$num BACKUP DONE":: done ::elif [ "$answer" = "n" ]::then:: read -p "Which VM should be backed up? (1/2/3): " numanswer:: until echo $numanswer | grep "^[123]$" >> /dev/null # Look for match of single digit: 1,2, or 3:: do:: read -p "Invalid Selection. Select 1, 2, or 3: " numanswer:: done:: echo "Backing up VM #$numanswer":: gzip < /var/lib/libvirt/images/centos$numanswer.img > /root/centos$numanswer.img.backup.gz:: echo "VM #$numanswer BACKUP DONE":::else:: echo "Invalid Selection... Aborting program":: 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.'''