-
Notifications
You must be signed in to change notification settings - Fork 2
/
nextcloud-scanner.py
66 lines (60 loc) · 2.69 KB
/
nextcloud-scanner.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
import time
from datetime import datetime
import systemd.daemon
import os
import yaml
import logging
import sys
import signal
import subprocess
def sig_handler(sig, frame):
sys.exit(0)
if __name__ == '__main__':
time.sleep(1)
systemd.daemon.notify('READY=1')
#Config file is expected at /lib/systemd/system/nextcloud-scanner.d/. Update below as well as Systemd unit file if changed
try:
with open("/lib/systemd/system/nextcloud-scanner.d/config.yml", "r") as yfile:
cfg = yaml.load(yfile, Loader=yaml.FullLoader)
except:
logging.warning("Config file not found")
signal.signal(signal.SIGINT, sig_handler)
#Set values from config file. Will need to restart service if config.yml updated
try:
root_path = cfg["paths"]["root_path"]
log_path = cfg["paths"]["log_path"]
owner_uid = cfg["params"]["uid"]
owner_gid = cfg["params"]["gid"]
occ_path = cfg["nextcloud"]["occ_path"]
nc_user = cfg["nextcloud"]["nc_user"]
except:
logging.warning("Config file not formatted correctly or missing values")
signal.signal(signal.SIGINT, sig_handler)
#Main loop. Change time.sleep(5) if longer or shorter check interval needed
while True:
for root, dirs, files in os.walk(root_path, topdown=False):
for name in files:
path = os.path.join(root, name)
perm = (oct(os.stat(path).st_mode))[-3:]
owner = os.stat(path).st_uid
group = os.stat(path).st_gid
#Uses octal format, update last 3 digits for desired permission
if ((perm != "664") or (owner != owner_uid) or (group != owner_gid)):
now = datetime.now()
now = time.strftime("%H:%M:%S")
f = open(log_path, "a")
f.write(now)
f.write( f" {perm} {owner} {group} {path} >> ")
#If comparision above updated, update os.chmod below to match
os.chmod(path, 0o0664)
os.chown(path, owner_uid, owner_gid)
path = os.path.join(root, name)
perm = (oct(os.stat(path).st_mode))[-3:]
owner = os.stat(path).st_uid
group = os.stat(path).st_gid
f.write( f"{perm} {owner} {group} {name}")
f.write("\n")
f.close()
#Run Nextcloud database update process
nc_output = (subprocess.check_output(f"sudo -u www-data php {occ_path} files:scan --path={nc_user}", shell=True)).decode('UTF-8').rstrip()
time.sleep(5)