Changes

Jump to: navigation, search

OPS245 Scripting Exercises Lab 2 dev

1,216 bytes added, 22:14, 24 January 2023
no edit summary
= 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 back up backup all three of your virtual machines. We will improve on this backup script in a later exercise. To begin, open your text editor (vi) on c7host and copy the following template in. Be sure to modify Author and Date with the correct information.
<pre>
#!/usr/bin/env python3
<pre>
currentuser = os.geteuid()
</pre>
 
=== 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.
<pre>
if currentuser != 0:
 
print("You are currently logged in as:",whoami)
 
sys.exit("You must be root")
</pre>
 
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.
<pre>
else:
 
backupAll=input("Do you wish to backup all VMs? (y | n) ")
</pre>

Navigation menu