-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrsync.py
115 lines (102 loc) · 3.21 KB
/
rsync.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
import json
import os
import requests
import re
import time
import subprocess
from datetime import datetime
import pytz
script_dir = os.path.dirname(os.path.abspath(__file__))
log_file_path = os.path.join(script_dir, "rsync.log")
source_dirs = ["/opt", "/home"]
target_dir = "/NAS"
webhook_url = 'https://discord.com/api/webhooks/<embed>'
start_time = time.time()
hostname = os.uname()[1]
source_dirs_string = " ".join(source_dirs)
# Create the rsync.log file if it doesn't exist
if not os.path.exists(log_file_path):
with open(log_file_path, "w") as log_file:
pass
with open(log_file_path, "a") as log_file:
result = subprocess.run(
f"rsync -rltvz -a --no-links --no-specials --no-devices --no-inc-recursive --delete --stats --info=progress2 --verbose {source_dirs_string} {target_dir}",
shell=True,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
)
output = result.stdout.decode()
log_file.write(output)
match = re.search(
r"Number of files: ([\d,]+).*Number of created files: ([\d,]+).*Number of deleted files: ([\d,]+).*Number of regular files transferred: ([\d,]+).*",
output, re.DOTALL)
if match:
synced = match.group(1)
created = match.group(2)
deleted = match.group(3)
transferred = match.group(4)
else:
synced = 0
created = 0
deleted = 0
transferred = 0
print(synced, created, deleted, transferred)
# Check for error code 23, 24, and 25 and set returncode to 0 if found
if result.returncode in [23, 24, 25]:
result.returncode = 0
if result.returncode == 0:
duration_seconds = time.time() - start_time
duration_minutes, duration_seconds = divmod(duration_seconds, 60)
duration_seconds = round(duration_seconds, 0)
tz = pytz.timezone("America/New_York")
time_ny = datetime.now(tz).strftime("%I:%M %p")
date_ny = datetime.now(tz).strftime("%m/%d/%y")
embed = {
'title': hostname,
"thumbnail": {
"url": "https://i.imgur.com/dFqM7CG.png"
},
'fields': [
{
'name': 'Sync',
'value': synced,
'inline': True
},
{
'name': 'Transfer',
'value': transferred,
'inline': True
},
{
'name': 'Delete',
'value': deleted,
'inline': True
},
{
'name': 'Duration',
'value': f'{int(duration_minutes)} minutes {int(duration_seconds)} seconds',
},
{
'name': 'Date and Time',
'value': f'{date_ny} {time_ny}',
},
],
'color': 12868102
}
else:
with open(log_file_path, "r") as file:
error_output = file.read()
error_output = error_output[-2000:]
embed = {
'title': f'{hostname} rsync failed',
'description': f'```{error_output}```',
'color': 15158332,
'fields': [
{
'name': 'Error Code',
'value': result.returncode,
'inline': True
}
]
}
requests.post(webhook_url, json={'embeds': [embed]})