-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdrive-backup.py
More file actions
105 lines (88 loc) · 2.9 KB
/
drive-backup.py
File metadata and controls
105 lines (88 loc) · 2.9 KB
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
import subprocess
import sys
import time
import re
drive_dir = "/home/ignite/GoogleDrive/"
flashpoint_dir = "/home/ignite/workspace/flashpoint/"
compID_regex = r"[a-zA-Z][a-zA-Z][a-zA-Z][a-zA-Z][a-zA-Z]?[0-9]?_" # matches for any 4/5 letters, optional number, and underscore after
invalids = [
"FRC", "TBD", "FF", "rio"
]
def backupLogs():
list_files = [
"ls", "-1", # list all files one per line
flashpoint_dir+"telemetry/"
]
listRes = subprocess.run(list_files, capture_output=True).stdout.decode()
for fileName in listRes.splitlines():
print("File: "+fileName, end=" ")
tmpFileName = fileName
for index in range(len(invalids)):
tmpFileName.replace(invalids[index], "")
compID_match = re.search(compID_regex, tmpFileName)
drive_teledir = ""
if compID_match:
compID = ""
for i in range(compID_match.start(), compID_match.end()-1): # -1 removes underscore from matching
compID = compID+tmpFileName[i]
drive_teledir = drive_dir+"Programming/Telemetry/LandingZone/"+compID
print("Comp id: "+compID)
else:
drive_teledir = drive_dir+"Programming/Telemetry/LandingZone/nocomp"
print("Comp id: none")
make_folder = [
"mkdir", "-p", # make folder, all parent directories, and don't complain if existing
drive_teledir,
]
mkdirRes = subprocess.run(make_folder, capture_output=True).stdout.decode()
if mkdirRes != "":
print(repr(mkdirRes)) # repr to force printing non-printing chars, such as newlines
move_file = [
"cp", flashpoint_dir+"telemetry/"+fileName, drive_teledir # move file to drive_directory
]
moveCMD = subprocess.run(move_file, capture_output=True)
moveRes = moveCMD.stdout.decode()
moveErr = moveCMD.stdout.decode()
if moveRes != "":
print(repr(moveRes)) # repr to force printing non-printing chars, such as newlines
def backupDB():
print("Copying robot.db file...")
drive_db_dir = drive_dir+"Programming/Telemetry/LandingZone/dbbackups/"
LT = time.localtime()
strDate = str(LT.tm_year)+str(LT.tm_mon)+str(LT.tm_mday)
strTime = str(LT.tm_hour)+str(LT.tm_min)+str(LT.tm_sec)
#print(strDate+"_"+strTime)
fullNewFilePath = drive_db_dir+"backup_"+strDate+"_"+strTime+".db"
copy_file = [
"cp", flashpoint_dir+"db/robot.db", # copy the robot db file
fullNewFilePath
]
copyCMD = subprocess.run(copy_file, capture_output=True)
copyRes = copyCMD.stdout.decode()
copyErr = copyCMD.stderr.decode()
if copyRes != "":
print(copyRes)
if copyErr != "":
print(copyErr)
def main():
print("Starting...")
if len(sys.argv) > 1:
if "--only-db" in sys.argv:
backupDB()
return
elif "--only-logs" in sys.argv:
backupLogs()
return
else:
backupLogs()
backupDB()
elif len(sys.argv) == 2:
print("Usage: drive-backup.py [OPTIONS]\n")
print("Options:\n")
print("--db backup database (robot.db) file")
return
if __name__ == "__main__":
try:
main()
except KeyboardInterrupt:
print("\nMonitoring stopped by user")