From 7918ee8d8e998210d7e2cdd65ae5cda4e60b2e65 Mon Sep 17 00:00:00 2001 From: hubobel Date: Sun, 20 Apr 2025 10:27:30 +0200 Subject: [PATCH] initial --- rclone_tele.py | 108 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 108 insertions(+) create mode 100644 rclone_tele.py diff --git a/rclone_tele.py b/rclone_tele.py new file mode 100644 index 0000000..d6545ae --- /dev/null +++ b/rclone_tele.py @@ -0,0 +1,108 @@ +import json +import os +import requests +import datetime +import math +import subprocess + +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 = "6828697809:AAHsdnom53SQGaYdgQBUrxrD2cMkwurupKI" + +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): + params = {"parse_mode": "HTML", "chat_id": telegram_chat_id, "text": text} + url = f"https://api.telegram.org/bot{telegram_token}/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 + +os.remove(logfilepath) + +notify_telegram(("Backup Statistics:\n\n%s\n\nStart Time: %s\nEnd Time: %s\nDuration: %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])))