OPS245 Scripting Exercises Lab 2 dev

From CDOT Wiki
Jump to: navigation, search

Virtual Machine Backup Script

Overview

In Lab 2 Investigation 3 you performed a manual backup of all 3 virtual machines. While this is something both important and necessary, it's a tedious process. This is something you could automate through scripting. In this exercise you are going to write a script that will backup all three of your virtual machines. We will improve on this backup script in a later exercise. To begin, open the script backupVM.py (that you imported using git clone in Week 2) in your text editor (vi) on c7hostl. Be sure to modify Author and Date with the correct information.

Filling in the code

The below instructions provide all (or most) of the required code for filling in the above template. Fill in each section in sequence under the appropriate comment block.

Importing the required modules

For this script we will require the following modules:

  • os
  • system
  • sys

Using the import statement (like we have done previously), import the above modules on one line.

import os, system, sys

Storing the output of the current user and checking to see if they are root.

We will use this to indicate an error if the script is not being run as root (without sudo). Create a variable called "whoami" and store the output of the whoami command in it. We will use the subprocess module, with the check_output function to accomplish this task.

whoami = subprocess.check_output("whoami").decode("utf-8")

Check the current user (who is executing the script) and store the information in the variable "currentuser".

We will use the os.geteuid() function for this task. Linux UID's (User IDs) start at 1000 for users. Root has a UID of 0.

currentuser = os.geteuid()

Checking if the current user is root

Now that we have the information about the current user and their UID stored in variables, we can check to see if the user is root using an if/else block. If the user is not root, we will print an error (indicating they are not root and who they are currently logged in as) and exit with an error. You can use the "sys.exit()" function to display an error and exit. If no content is provided with sys.exit, the script will exit with a 0 status code. If content is provided it will display that error on the screen and exit with a 1 status code. Remember that Linux uses 0 status codes to indicate success and any non-zero value to indicate failure.

if currentuser != 0:

   print("You are currently logged in as:",whoami)

   sys.exit("You must be root")

We can use an else statement to continue, since the above checks to see if the user is not root. If the above condition is not met, the current user's UID is 0 and they are root. We can then prompt if they want to backup all VMs or not. If the answer is no, prompt again asking which VM they wish to backup.

else:
   
   backupAll=input("Do you wish to backup all VMs? (y | n) ")

Asking the user which VM they wish to backup, and performing the backup

If the user selected no above, we need to know what VM they wish to backup. We will prompt again and use a nested if statement to check their response. Then, perform the desired backup.

   if backupAll == "n":

   backupVM=input("What VM do you wish to backup? ")

   if backupVM == "centos1":

       print("Backing up centos1")

       os.system("virsh dumpxml centos1 > /home/jmcarman/backups/centos1.xml")

       os.system("gzip < /var/lib/libvirt/images/centos1.qcow2 > /home/jmcarman/backups/centos1.qcow2.gz")

  elif backupVM == "centos2":
       # Repeat above steps with replacing centos1 with centos2

Using the above sample, add the steps to backup centos2 or centos3 if appropriate.

Error checking

If the user submits an invalid VM name (not centos1, centos2 or centos3) we should generate an error and exit.

    else:
      sys.exit("Invalid choice, your VM options are centos1, centos2, centos3")

Else, we are doing a full backup (all 3 VMs)

Use the below sample to back up centos1. Modify it for centos2 and centos3.

   print("Backing up centos1")

   os.system("virsh dumpxml centos1 > /home/jmcarman/backups/centos1.xml")

   os.system("gzip < /var/lib/libvirt/images/centos1.qcow2 > /home/jmcarman/backups/centos1.qcow2.gz")

Exit with success

Finally, we should exit successfully. This isn't technically necessary, but is a good practice.

sys.exit()