-
Notifications
You must be signed in to change notification settings - Fork 1
/
backup_usb.py
69 lines (59 loc) · 2.17 KB
/
backup_usb.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
#import pyudev
import shutil,os,re
from distutils.dir_util import copy_tree
import datetime
#print(pyudev.__version__)
#context = pyudev.Context()
#for device in context.list_devices(subsystem='block'):
# print(device)
#monitor = pyudev.Monitor.from_netlink(context)
#monitor.filter_by('block')
#polling status (blocking any loop)
#for device in iter(monitor.poll, None):
# if 'ID_FS_TYPE' in device:
# print('{0} partition {1}'.format(device.action, device.get('ID_FS_LABEL')))
# print(device.get('ID_FS_TYPE'))
# print(device.action)
# if device.action == 'add':
# print("usb added")
# print("my name is " + device.get('ID_FS_LABEL'))
#
# path = "/dev/" + device.get('ID_FS_LABEL')
devices = os.popen('sudo blkid').readlines()
usbs = []
for u in devices:
loc = [u.split(':')[0]]
if '/dev/sd' not in loc[0]:
continue # skip
loc+=re.findall(r'"[^"]+"',u)
columns = ['loc']+re.findall(r'\b(\w+)=',u)
usbs.append(dict(zip(columns,loc)))
for u in usbs:
print ('Device %(LABEL)s is located at $(loc)s with UUID of $(UUID)s'%u )
name = "%(LABEL)s" %u
name = name[:-1]
name = name[1:]
print(name)
os.system('sudo mount $(loc)s /myusb'%u)
dest = "/media/pi/"+ name +"/NeueTestFile.jpg"
shutil.copy("/home/pi/programs/newimage/new.jpg", dest)
#from pi to usb stick
dest = "/media/pi/"+ name +"/programs_backup"
copy_tree("/home/pi/programs", dest)
#from usb stick to pi
source = "/media/pi/"+ name +"/fotobox_vorlagen/overlay.jpg"
shutil.copy(source, "/home/pi/programs/countdown/overlay.jpg")
source = "/media/pi/"+ name +"/fotobox_vorlagen/stripes.png"
shutil.copy(source, "/home/pi/programs/countdown/stripes.png")
today = str(datetime.datetime.now())
with open('/home/pi/programs/log_backup.txt', 'w') as f:
f.write(today)
f.close()
quit()
#def log_event(action, device):
# if 'ID_FS_TYPE' in device:
# with open('filesystems.log', 'a+') as stream:
# print('{0} - {1}'.format(action, device.get('ID_FS_LABEL')), file=stream)
#
#observer = pyudev.MonitorObserver(monitor, callback=log_event)
#observer.start()