-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbackup.py
executable file
·79 lines (71 loc) · 3.22 KB
/
backup.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
from fabric.api import *
from fabric.colors import *
import datetime
import common
import sendemail
import vboxmgmt
import os
@task
def avimark():
''' Runs the compres avimark and gets the gz from remote server and puts it locally'''
compress_avimark()
cleanup_oldbackups()
@task
def compress_avimark():
''' Backs up Avimark Share, excluding *.zip files'''
out = common.getSMBStatus() #sudo('smbstatus')
if 'No locked files' in out:
do_avimarkTar()
else:
sendemail.sendmail('Avimark Backup could not be started', 'Possible reasons are: There are open files or could not connect to Sunnyx02 \n ' + out + ' \n Will try to Stop Samba to Start backup')
smbstatus = common.getSMBServiceStatus()
if 'Active: active (running)' in smbstatus:
common.stopSamba()
#common.getSMBStatus()
do_avimarkTar()
common.startSamba()
def do_avimarkTar():
''' Performs the actual TAR of the avimark folder. Can't call as a single task. Must be call from within file'''
bkpfolder = '/apps/backups/'
bkpname = 'avimark_fullbackup_' + common.getdate() + '.tgz'
with cd('/apps/shares'):
sudo("tar cvfz " + bkpfolder + bkpname + " avimark/ --exclude=avimark/*.zip --exclude=avimark/Backup")
get_avimarkTar()
@task
def get_avimarkTar():
'''Gets the compress avimark backup files'''
get('/apps/backups/*.tgz', '/apps/backups/avimark/', use_sudo=True)
out = local('ls -lah /apps/backups/avimark/', capture=True)
smbstatus = common.getSMBStatus()
smbservicestatus = common.getSMBServiceStatus()
sendemail.sendmail('Avimark Backup', 'Avimark has been completed, please verify at your earliest convenience \n ' + out + '\n Samba Service Status: ' + smbservicestatus + '\n Samba Shares Satus: ' + smbstatus)
with cd('/apps/backups/'):
sudo('rm *.tgz')
@task
def cleanup_oldbackups():
''' Deletes backup files older than 5 days'''
local('find /apps/backups/avimark/*.tgz -mtime +10 -delete')
@task
def backup_vm(All=True):
''' Backs up virtual machines. Could be one or all. Defaults to True meaning All VMs will be back up '''
#status = vboxmgmt.status()
#Still need to work on the boolean check for single or all vms
vms = '/apps/vms'
vmstatus = vboxmgmt.status()
for d in os.listdir(vms):
if d in vmstatus and 'running' in vmstatus:
print red('VM with Name=' + d + ' is still running... Will try to perform action=poweroff now')
vboxmgmt.stopvm(d)
copy_tar_vm(d)
else:
print green('VM with Name=' + d + ' seems to be poweroff, starting VM backup now...')
vboxmgmt.stopvm(d)
copy_tar_vm(d)
def copy_tar_vm(svrname=None):
''' Performs the actual tar of the VM folder and copies file to backup folder. Can't call directly. Must be call from a defined task'''
sendemail.sendmail('Sunnyx02 Backup', 'Starting Sunnyx02 backup now ...')
with lcd('/apps/vms'):
local('cp -R ' + svrname + '/ /apps/backups/vms/.')
vboxmgmt.startvm(svrname)
vmstatus = vboxmgmt.status()
sendemail.sendmail('Sunny Hills VMs Backup', 'Completed Backup for Sunny Hills VMs. Find VMs status below: \n' + vmstatus)