Hi, I need python shell scripting help: Here is my script instrction- In your bin directory, create
Fantastic news! We've Found the answer you've been seeking!
Question:
Hi, I need python shell scripting help:
Here is my script instrction-
- In your bin directory, create the file backupVM.py, and populate with our standard beginning #!/usr/bin/env python3 # backupVM.py # Purpose: Backs up virtual machines # # USAGE: ./backupVM.py # # Author: *** INSERT YOUR NAME *** # Date: *** CURRENT DATE *** import os currentuser = os.popen('whoami') if currentuser.read()!= 'root': print("You must be root") exit() else: for machine in ('centos1','centos2','centos3'): print('Backing up ' + machine) os.system('gzip < /var/lib/libvirt/images/' + machine + '.qcow2 > ~YourRegularUsername/backups/' + machine + '.qcow2.gz')
- Try to run that script. You'll notice it does not work. No matter what you do, it always says you are not root.
- Modify the print statement that tells the user they must be root to also include the current username, then run the program again.
- It should print out root, but with an extra new-line. You may have noticed this in your other python scripts so far: the data we get from os.popen() has an extra new-line on the end. We will need to modify the string(s) it gives us a bit. See the side-bar for hints on how to do so.
- Modify the if statement so it is just getting the current username, not the username and a newline. You can do this using several steps and several variables, but it can also be done in a single line.
- Now that the script recognizes you as being root (or at least running the script with root permissions), it should work. Notice how we've used the + to combine several strings together to pass to the os.system command. We did this because this script needs the python variable to be evaluated before the whole line gets handed over to os.system. If you left the variable names inside the quotes, python will ignore them as just being part of a string. By putting them outside of a string, and concatenating their value to that string, we can evaluate them and feed them into that command.
- Test your script to make sure it works. If it doesn't, go back and fix it. Do not continue until it successfully makes backups of your VMs.
- There is a weakness to this script as written. Every time you run it, it will make a backup of all three VMs. But what if you only made a change to one of them? Do we really need to wait through a full backup cycle for two machines that didn't change? As the script is currently written, we do. But we can make it better.
- Before the for loop that backs up each machine add a prompt to ask the user if they want to back up all machines. Use an if statement to check if they said yes.
- if they did say yes, back up all the VMs using your existing for loop.
- If they didn't say yes, do nothing for now.
- Test your script to make sure it works. Check what happens if you say 'yes' to the prompt, and check what happens if you say things other than 'yes'.
- Now we have a script that asks the user if they want to back up all VMS, and if they say they do it does. But if they don't want to back up every VM, it currently does nothing.
- Add an else statement to handle the user not wanting to back up every VM. Inside that else clause ask the user which VM they would like to back up (you can even give them the names of available VMs (Centos1, Centos2, Centos3).
- Now nest an if statement inside that else so that your script can handle what your user just responded with. If they asked for Centos1, back up Centos1. If they want to back up Centos2, only back up Centos2, etc. Hint: You might want to use elif for this.
- Test your script again. You should now have a script that:
- Makes sure the user is running the script with elevated permissions.
- Asks the user if they want to back up every VM.
- If they want to back up every VM, it backs up every VM.
- If the user does not want to back up every VM, the script asks them which VM they do want to back up.
- If the user selected a single VM, the script will back up that one VM.
After running I need to modify the script below I give it
Modify your backupVM.py script from lab2 to backup any VM on your c7host machine, regardless of its name or where the image file is stored. Note: The --name option for the virsh list command will prove helpful. So will the fact that the source file is one of the values stored in each machine's xml definition.
Related Book For
Income Tax Fundamentals 2013
ISBN: 9781285586618
31st Edition
Authors: Gerald E. Whittenburg, Martha Altus Buller, Steven L Gill
Posted Date: