-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvalheimbacku.py
executable file
·92 lines (72 loc) · 2.58 KB
/
valheimbacku.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
#!/usr/bin/env python3
from ftplib import FTP, all_errors
import configparser, sys, os
# function to handle possible errors I've been able to produce
def handleErrors(e, world=None):
# Lower level network errors
if e.__class__.__name__ == "TimeoutError":
print("Timed out, check your host and port in config.ini")
sys.exit()
elif e.__class__.__name__ == "gaierror":
print("Socket error, host address is wrong in config.ini")
sys.exit()
# it's an error with the ftp then
else:
err = str(e).split(None, 1)[0]
if err == "530":
print('Login Failed')
sys.exit()
if err == "550":
print(f"World not found: {world}")
return world
# get the config info from the ini file, using os.path since crons run in a different working dir
config = configparser.ConfigParser()
config_file = os.path.join(os.path.dirname(__file__), 'config.ini')
config.read(config_file)
usr = config['login']['usr']
pwd = config['login']['pwd']
host = config['server']['host']
port = int(config['server']['port']) # must be an int
# file extensions/types to download
ext = [".db", ".fwl", ".fwl.old"]
# list of files to delete if the world isn't found
deleteme = []
# get the list of worlds and strip any whitespace remove duplicates and empty elements
worlds = config['worlds']['world'].split(',')
for i in range(0, len(worlds)):
worlds[i] = worlds[i].strip()
worlds = set(filter(None, worlds))
# os path to the backups folder, needed since crons run in a different working dir
worlds_path = os.path.join(os.path.dirname(__file__), 'backups/')
# check if the backups dir exists, if not then create it
if not os.path.exists(worlds_path):
os.makedirs('backups')
# setup the FTP connection
ftp = FTP()
try:
ftp.connect(host, port)
except Exception as e:
handleErrors(e)
try:
ftp.login(usr, pwd)
except all_errors as e:
handleErrors(e)
# switch to the worlds dir
ftp.cwd('.config/unity3d/IronGate/Valheim/worlds')
# loop through the worlds and download a backup file
for filename in worlds:
for xt in ext:
with open(f"{worlds_path}{filename}{xt}", "wb") as fp:
try:
ftp.retrbinary(f"RETR {filename}{xt}", fp.write)
print(f"Received {filename}{xt}")
except all_errors as e:
d = handleErrors(e, filename+xt)
if d:
deleteme.append(d)
# remove the worlds that were not found from the local backup folder
if len(deleteme) > 0:
for f in deleteme:
os.remove(f"backups/{f}")
# end
ftp.quit()