-
Notifications
You must be signed in to change notification settings - Fork 0
/
upload_to_server2020.py
executable file
·69 lines (62 loc) · 2.35 KB
/
upload_to_server2020.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 subprocess, os, socket
import numpy as np
import datetime as dt
from rpi_info import name
import psutil
def checkIfProcessRunning(processName):
'''
Check if there is any running process that contains the given name processName.
'''
#Iterate over the all the running process
i=0
for proc in psutil.process_iter():
try:
# Check if process name contains the given name string.
if processName.lower() in proc.name().lower():
script_name = proc.cmdline()[-1]
if "upload_to_server2020.py" in script_name:
i+=1
if i >=2:
print("{} is already running".format(proc.cmdline()[-1].lower()))
return True
except (psutil.NoSuchProcess, psutil.AccessDenied, psutil.ZombieProcess):
pass
return False
def terminal(command):
try:
term_output = subprocess.check_output(command, stderr=subprocess.STDOUT, shell=True)
except subprocess.CalledProcessError as e:
#uncomment line below for more detailed debugging
#print("{}: {}".format(e.cmd,e.output.decode()))
raise e
else:
return term_output.decode()
##replace this with appropriate local & remote paths for backup
copy_from = "/home/pi/APAPORIS/MOVED/"
copy_to = "/home/pi/mnt/ringnecks/winter_2022/photo/{}".format(name)
def backup_to_server():
print("####{} backup_function.py####".format(dt.datetime.now().strftime('%Y-%m-%d_%H_%M')))
#Get a list of files in original folder
files_from = os.listdir(copy_from)
#Get a list of files in backup folder
files_bup = os.listdir(copy_to)
files_to_bup = np.setdiff1d(files_from, files_bup)
#Copy Video files
print("backing up {} files".format(len(files_to_bup)))
for video in files_to_bup:
try:
command = 'mv {}{} {}'.format(copy_from,video,copy_to)
terminal(command)
except Exception as e:
print("Error uploading {} to server. Error {}.".format(video,e))
else:
print("{} backed up".format(video))
if __name__=="__main__":
running = checkIfProcessRunning("python")
print(running)
if not running:
if not os.path.isdir(copy_to):
os.mkdir(copy_to)
backup_to_server()
else:
print("backup already running")