/
backupWin32.py
72 lines (67 loc) · 2.75 KB
/
backupWin32.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
#!/usr/bin/env python
#-*- coding: UTF-8 -*-
# File: backupWin32.py
# Copyright (c) 2013 by None
#
# GNU General Public Licence (GPL)
#
# This program is free software; you can redistribute it and/or modify it under
# the terms of the GNU General Public License as published by the Free Software
# Foundation; either version 2 of the License, or (at your option) any later
# version.
# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
# FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
# details.
# You should have received a copy of the GNU General Public License along with
# this program; if not, write to the Free Software Foundation, Inc., 59 Temple
# Place, Suite 330, Boston, MA 02111-1307 USA
#
__author__ = 'Costas Tyfoxylos <costas.tyf@gmail.com>'
__docformat__ = 'plaintext'
__date__ = '15/11/2013'
import sys
import time
import wmi
from backup import BackUp
import logging
logger = logging.getLogger(__name__)
def getDriveLetterFromSerial(serial):
try:
logger.debug('Trying to get device and drive letter from serial.')
c = wmi.WMI ()
for disk in c.Win32_DiskDriveToDiskPartition():
deviceSerial = disk.Antecedent.PNPDeviceID.split('\\')[-1].split('&')[0]
if deviceSerial == serial:
systemName = disk.Antecedent.SystemName
logger.debug('Device system name : {0}'.format(systemName))
diskIndex = disk.Dependent.DiskIndex
for partition in c.Win32_LogicalDiskToPartition():
if partition.Antecedent.DiskIndex == diskIndex:
driveLetter = partition.Dependent.Name
logger.debug('Device drive letter : {0}'.format(driveLetter))
return systemName, driveLetter
except Exception, e:
logger.error('Something happened with the drive query.')
logger.error('Traceback : ', exc_info=True)
raise SystemExit
if __name__=='__main__':
try:
serial = sys.argv[1]
logger.debug('Got device serial : {0}'.format(serial))
except IndexError:
logger.error('Not enough arguments. Exiting')
raise SystemExit
try:
device, partition = getDriveLetterFromSerial(serial)
except TypeError:
logger.error('Something happened with the drive query.')
logger.error('Traceback : ', exc_info=True)
raise SystemExit
logger.debug('Got device : {0}'.format(device))
logger.debug('Got partition : {0}'.format(partition))
backUp = BackUp()
backUp.setDrive(device, partition)
backUp.enableEmail()
backUp.setJobDetails(serial)
backUp.run()