-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBackup Manager.py
218 lines (206 loc) · 7.2 KB
/
Backup Manager.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
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
import os, shutil
from time import sleep
# Constants
PROGRAM_TITLE = "Backup Manager"
USERNAME = os.getlogin()
MINECRAFT_FOLDER = f'C:/Users/{USERNAME}/AppData/Roaming/.minecraft'
SAVES_FOLDER = f'{MINECRAFT_FOLDER}/saves'
BACKUP_FOLDER = f'{MINECRAFT_FOLDER}/Backup Manager'
# Vars
exit = False
# QOL
os.system('echo off')
os.system('color 0a')
os.system(f'title {PROGRAM_TITLE}')
# Checks to see if the backup folder exists. If not, it prompts to create it.
def ensureBackupFolder() -> None:
if not os.path.exists(BACKUP_FOLDER):
os.system('cls')
print("Backup folder not found.")
print("Would you like to create a backup folder?")
print("1. Yes")
print("2. No")
while True:
choice = input("Choice: ")
if choice == "1":
os.mkdir(BACKUP_FOLDER)
print(f"Backup folder created as '{BACKUP_FOLDER}'.")
os.system('cls')
break
elif choice == "2":
print("Exiting...")
global exit
exit = True
return
else:
print("Invalid choice.")
# Deletes the backup folder if it exists (And everything in it, obviously).
def deleteBackupFolder() -> None:
os.system('cls')
print("Are you sure you want to delete the backup folder?")
print("1. Yes")
print("2. No")
while True:
choice = input("Choice: ")
if choice == "1":
print("Deleting backup folder...")
shutil.rmtree(BACKUP_FOLDER)
print("Backup folder deleted.")
break
elif choice == "2":
break
else:
print("Invalid choice.")
# Gets the saves in the saves folder.
def getSaves() -> list:
saves = []
for file in os.listdir(SAVES_FOLDER):
saves.append(file)
return saves
# Gets the backups in the backup folder.
def getBackups() -> list:
backups = []
for file in os.listdir(BACKUP_FOLDER):
backups.append(file)
return backups
# Creates a backup of any or all worlds chosen by the user.
def backupWorld() -> None:
os.system('cls')
print("Which world would you like to backup?")
saves = getSaves()
if len(saves) == 0:
print("No saves found.")
sleep(1)
return
for i, v in enumerate(saves):
print(f"{i + 1}. {v}")
print(f'{len(saves) + 1}. All.')
print(f'{len(saves) + 2}. Cancel.')
while True:
choice = input("Choice: ")
if choice.isdigit() and int(choice) <= len(saves):
choice = int(choice) - 1
choice = saves[choice]
os.system('cls')
print(f"Backing up '{choice}'...")
if os.path.exists(f'{BACKUP_FOLDER}/{choice}'):
shutil.rmtree(f'{BACKUP_FOLDER}/{choice}')
worldPath = f'{SAVES_FOLDER}/{choice}'
shutil.copytree(src=worldPath, dst=f'{BACKUP_FOLDER}/{choice}', copy_function = shutil.copy2)
break
if choice.isdigit() and int(choice) == len(saves) + 1:
os.system('cls')
for save in getSaves():
print(f"Backing up '{save}'...")
if os.path.exists(f'{BACKUP_FOLDER}/{save}'):
shutil.rmtree(f'{BACKUP_FOLDER}/{save}')
shutil.copytree(src=f'{SAVES_FOLDER}/{save}', dst=f'{BACKUP_FOLDER}/{save}', copy_function = shutil.copy2)
break
elif choice.isdigit() and int(choice) == len(saves) + 2:
os.system('cls')
return print("Cancelled.")
else:
print("Invalid choice.")
print("Backup complete.")
# Deletes a backup of any or all worlds chosen by the user.
def deleteBackup() -> None:
os.system('cls')
print("Which world would you like to delete?")
backups = getBackups()
if len(backups) == 0:
print("No backups found.")
sleep(1)
return
for i, v in enumerate(backups):
print(f"{i + 1}. {v}")
print(f'{len(backups) + 1}. All.')
print(f'{len(backups) + 2}. Cancel.')
while True:
choice = input("Choice: ")
if choice.isdigit() and int(choice) <= len(backups):
choice = int(choice) - 1
choice = backups[choice]
os.system('cls')
print(f"Deleting '{choice}'...")
break
elif choice.isdigit() and int(choice) == len(backups) + 1:
os.system('cls')
for backup in getBackups():
print(f"Deleting '{backup}'...")
shutil.rmtree(f'{BACKUP_FOLDER}/{backup}')
break
elif choice.isdigit() and int(choice) == len(backups) + 2:
os.system('cls')
return print("Cancelled.")
else:
print("Invalid choice.")
if os.path.exists(f'{BACKUP_FOLDER}/{choice}'):
shutil.rmtree(f'{BACKUP_FOLDER}/{choice}')
print("Backups deleted.")
# Restores a backup of any or all worlds chosen by the user.
def restoreBackup() -> None:
os.system('cls')
print("Which world would you like to restore?")
backups = getBackups()
if len(backups) == 0:
print("No backups found.")
sleep(1)
return
for i, v in enumerate(backups):
print(f"{i + 1}. {v}")
print(f'{len(backups) + 1}. All.')
print(f'{len(backups) + 2}. Cancel.')
while True:
choice = input("Choice: ")
if choice.isdigit() and int(choice) <= len(backups):
choice = int(choice) - 1
choice = backups[choice]
os.system('cls')
print(f"Restoring '{choice}'...")
worldPath = f'{SAVES_FOLDER}/{choice}'
if os.path.exists(worldPath):
shutil.rmtree(worldPath)
shutil.copytree(src=f'{BACKUP_FOLDER}/{choice}', dst=worldPath, copy_function = shutil.copy2)
break
elif choice.isdigit() and int(choice) == len(backups) + 1:
os.system('cls')
for save in getSaves():
print(f"Restoring '{save}'...")
worldPath = f'{SAVES_FOLDER}/{save}'
if os.path.exists(worldPath):
shutil.rmtree(worldPath)
shutil.copytree(src=f'{BACKUP_FOLDER}/{save}', dst=worldPath, copy_function = shutil.copy2)
break
elif choice.isdigit() and int(choice) == len(backups) + 2:
os.system('cls')
return print("Cancelled.")
else:
print("Invalid choice.")
print("Restore complete.")
def main() -> None:
os.system('cls')
global exit
while exit == False:
ensureBackupFolder()
if exit == True: return
print("What would you like to do?")
print("1. Backup a world.")
print("2. Restore a backup.")
print("3. Delete a backup.")
print(f"4. Delete {PROGRAM_TITLE} Folder.")
print("5. exit.")
choice = input("Choice: ")
if choice == "1":
backupWorld()
elif choice == "2":
restoreBackup()
elif choice == "3":
deleteBackup()
elif choice == "4":
deleteBackupFolder()
elif choice == "5":
exit = True
else:
print("Invalid choice.")
if __name__ == "__main__":
main()