Created
June 5, 2015 07:06
-
-
Save fourbytes/4183d739c75778d26051 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
import os, time, subprocess, sys | |
#import libvirt | |
BACKUP_DIR = "/mnt/backups/vms/DAILY/" | |
MO_BACKUP_DIR = "/mnt/backups/vms/MONTHLY/" | |
TODAY = time.strftime('%Y%m%d') | |
print('Backup procedure started...') | |
print('=====\n=====\n=====\nDO NOT TERMINATE THIS SCRIPT UNTIL IT HAS COMPLETED\n=====\n=====\n=====\n') | |
print ('[Stage 1] Inspecting existing backups...') | |
backups = sorted(name for name in os.listdir(BACKUP_DIR) | |
if os.path.isdir(os.path.join(BACKUP_DIR, name))) | |
if len(backups) != 0 and backups[0] == TODAY: | |
print ('[Stage 1] We have already run today. Cya!') | |
sys.exit(0) | |
if len(backups) > 7: | |
print('[Stage 1] Number of daily backups exceeds limit (7)...') | |
if backups[0][-2:] is '01': | |
print('[Stage 1] Backup to be deleted will be stored as monthly backup...') | |
print subprocess.check_output('mv ' + BACKUP_DIR + backups[0] + ' ' + MO_BACKUP_DIR + backups[0], shell=True) | |
print('[Stage 1] Backup migrated...') | |
else: | |
print('[Stage 1] Backup is going to be purged. Bye bye!') | |
print subprocess.check_output('rm -R ' + BACKUP_DIR + backups[0], shell=True) | |
print('[Stage 1] Removal complete.') | |
else: | |
print('[Stage 1] Everything looks A-OK!') | |
print ('\n[Stage 2] Making a new directory for today!') | |
NEW_DIR = BACKUP_DIR + TODAY | |
print subprocess.check_output('mkdir -p ' + NEW_DIR, shell=True) | |
print('[Stage 2] Getting a list of virtual machines...') | |
machines = subprocess.check_output("virsh list --all | awk '{print $2}' | tail -n +3 | head -n -1", shell=True).split() | |
for m in machines: | |
print('[Stage 2] Machine found: ' + m) | |
print ('[Stage 3] Iterating over each machine...') | |
for m in machines: | |
print('[Stage 3] Looking at ' + m + '...') | |
blks = subprocess.check_output(("virsh domblklist " + m + " | awk '{print $2}' | grep \"/mnt/vms/\""), shell=True).split() | |
diskargs = [] | |
for blk in blks: | |
snapimg = blk + '.tpbkus' | |
disk = subprocess.check_output("virsh domblklist --details " + m + " | grep \"" + blk.rsplit('.', 1)[0] + "\" | awk '{print $3}'", shell=True).replace("\n", "") | |
diskargs.append('--diskspec ' + disk + ',file=' + snapimg) | |
print(diskargs) | |
snapshotter = 'virsh snapshot-create-as --domain ' + m + ' tpbku' + m + ' --disk-only ' + ' '.join(diskargs) + ' --atomic --no-metadata' | |
print('[Stage 3] SNAPSHOT COMMAND: ' + snapshotter) | |
print subprocess.check_output(snapshotter, shell=True) | |
for blk in blks: | |
print('[Stage 3] Working on volume at "' + blk + '" ...') | |
disk = subprocess.check_output("virsh domblklist --details " + m + " | grep \"" + blk.rsplit('.', 1)[0] + "\" | awk '{print $3}'", shell=True).replace("\n", "") | |
blockcommit = 'virsh blockcommit ' + m + ' ' + disk + ' --active --verbose --pivot' | |
#filecopy = 'gzip < ' + blk + ' > ' + NEW_DIR + '/' + os.path.basename(blk) + '.gz' | |
filecopy = 'cp ' + blk + ' ' + NEW_DIR + '/' + os.path.basename(blk) | |
print('[Stage 3] Volume will be snapshotted to "' + snapimg + '" and restored once copied.') | |
print('[Stage 3] COPY COMMAND: ' + filecopy) | |
print('[Stage 3] COMMIT (RESTORE) COMMAND: ' + blockcommit) | |
print('[Stage 3] ==== PERFORMING BACKUP JOB ====') | |
print subprocess.check_output(filecopy, shell=True) | |
print subprocess.check_output(blockcommit, shell=True) | |
print('[Stage 3] ==== END OF JOB ====\n\n') | |
print('[Stage 4] Cleaning up...') | |
print subprocess.check_output('rm -R /mnt/vms/*.tpbkus', shell=True) | |
print (' ========== SCRIPT COMPLETE. ==========') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment