126 lines
No EOL
3.9 KiB
Python
126 lines
No EOL
3.9 KiB
Python
import json
|
|
import os
|
|
import requests
|
|
import datetime
|
|
import math
|
|
import subprocess
|
|
import base64
|
|
|
|
logfiledir = '/home/hubobel/'
|
|
logfilepath = logfiledir + 'rclone.log'
|
|
#logfilepath = 'rclone.txt'
|
|
host = 'vhost'
|
|
source = 'ultracc:/downloads/qbittorrent/'
|
|
target = '/mnt/extern/usenet/torrent/'
|
|
result_msg = []
|
|
|
|
start = datetime.datetime.now()
|
|
|
|
telegram_chat_id = "322673713"
|
|
telegram_token ='NjgyODY5NzgwOTpBQUhzZG5vbTUzU1FHYVlkZ1FCVXJ4ckQyY01rd3VydXBLSQ=='
|
|
|
|
def parse_log(logfile, source, target):
|
|
|
|
with open(logfile) as f:
|
|
data = [json.loads(line.rstrip()) for line in f if line[0] == "{"]
|
|
stats = []
|
|
operations = []
|
|
|
|
for obj in data:
|
|
|
|
if 'accounting/stats' in obj['source']:
|
|
stats.append(obj)
|
|
elif 'operations/operations' in obj['source']:
|
|
operations.append(obj)
|
|
for obj in stats:
|
|
obj['stats']['speed'] = float(obj['stats']['speed'])
|
|
json_body = [
|
|
{
|
|
"measurement": "stats",
|
|
"tags": {
|
|
"host": host,
|
|
"level": obj['level'],
|
|
"log_entry_source": obj['source'],
|
|
"source": source,
|
|
"target": target
|
|
},
|
|
"time": obj['time'],
|
|
"fields": obj['stats']
|
|
}
|
|
]
|
|
|
|
for obj in operations:
|
|
json_body = [
|
|
{
|
|
"measurement": "operations",
|
|
"tags": {
|
|
"host": host,
|
|
"level": obj['level'],
|
|
"log_entry_source": obj['source'],
|
|
"objType": obj['objectType'],
|
|
"msg": obj['msg'],
|
|
"source": source,
|
|
"target": target
|
|
},
|
|
"time": obj['time'],
|
|
"fields": {
|
|
"obj": obj['object']
|
|
}
|
|
}
|
|
]
|
|
|
|
return stats[0]['stats']['totalChecks'], stats[0]['stats']['totalTransfers'], stats[0]['stats']['totalBytes']
|
|
|
|
def convert_size(size_bytes):
|
|
if size_bytes == 0:
|
|
return "0B"
|
|
size_name = ("B", "KB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB")
|
|
i = int(math.floor(math.log(size_bytes, 1024)))
|
|
p = math.pow(1024, i)
|
|
s = round(size_bytes / p, 2)
|
|
return "%s %s" % (s, size_name[i])
|
|
|
|
def notify_telegram(text):
|
|
base64_string = telegram_token
|
|
base64_bytes = base64_string.encode("ascii")
|
|
sample_string_bytes = base64.b64decode(base64_bytes)
|
|
sample_string = sample_string_bytes.decode("ascii")
|
|
params = {"parse_mode": "HTML", "chat_id": telegram_chat_id, "text": text}
|
|
url = f"https://api.telegram.org/bot{sample_string}/sendMessage"
|
|
requests.post(url, params=params)
|
|
|
|
basecmd = ('rclone ' + str('copy') + ' --log-file=' + str(logfilepath) +
|
|
' --use-json-log ' + '--log-level=INFO --stats=90m --fast-list').split()
|
|
|
|
basecmd.append(str(source))
|
|
basecmd.append(str(target))
|
|
|
|
response = subprocess.run(basecmd)
|
|
|
|
checks, transfers, bytes = parse_log(logfilepath, source, target)
|
|
statistic = "\n ⤷ Checks: " + str(checks) + ", Transfers: " + str(transfers) + ", Bytes: " + \
|
|
str(convert_size(bytes))
|
|
icon = "✅ " if transfers > 0 else "🆗 "
|
|
result_msg.append(icon + source + " -> " + target + statistic)
|
|
|
|
end = datetime.datetime.now()
|
|
duration = end - start
|
|
|
|
|
|
|
|
|
|
if duration.total_seconds() >0:
|
|
notify_telegram(("<b>Backup Statistics:</b>\n\n%s\n\n<i>Start Time</i>: %s\n<i>End Time</i>: %s\n<i>Duration</i>: %s minutes" %
|
|
("\n\n".join(result_msg), start.strftime("%Y-%m-%d %H:%M:%S"), end.strftime("%Y-%m-%d %H:%M:%S"),
|
|
divmod(duration.total_seconds(), 60)[0])))
|
|
with open('rclone.log') as f:
|
|
data = [json.loads(line.rstrip()) for line in f if line[0] == "{"]
|
|
stats = []
|
|
operations = []
|
|
|
|
for obj in data:
|
|
if 'object' in obj:
|
|
print(obj['object'])
|
|
notify_telegram(obj['object'])
|
|
|
|
os.remove(logfilepath) |