-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.py
258 lines (213 loc) · 9.37 KB
/
main.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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
import os
import sys
import getopt
import subprocess
from multiprocessing.dummy import Pool as ThreadPool
import sqlite3
from itertools import repeat
from vars import *
# Create Log dir
if not os.path.exists(BACKUPDIR_LOG):
os.makedirs(BACKUPDIR_LOG)
# Update the server status to down in the database
def mark_host_db_down(serverip):
conn = sqlite3.connect(sqlite_file_path, timeout=10)
c = conn.cursor()
c.execute("""UPDATE serversmanage_servers SET backupstatus = 'ERROR' WHERE ip = ? """, (serverip, ))
conn.commit()
conn.close()
# Get a list of up and down servers
def check_alive_hosts(ssh_cmd):
hosts_up_list = []
hosts_down_list = []
hosts_ping_up_list = []
hosts_ping_down_list = []
conn = sqlite3.connect(sqlite_file_path, timeout=10)
c = conn.cursor()
c.execute("select ip from serversmanage_servers where serverstatus='Enabled';")
ip_rows = c.fetchall()
ip_list = []
for ip in ip_rows:
ip_list.append(", ".join(ip))
for server_ip in ip_list:
ping_chk = os.system("ping -c 5 " + server_ip)
if ping_chk == 0:
hosts_ping_up_list.append(server_ip)
else:
hosts_ping_down_list.append(server_ip)
hosts_down_list.append(server_ip)
mark_host_db_down(server_ip)
for server in hosts_ping_up_list:
# GET SSH Port from database
c.execute("select sshport from serversmanage_servers where ip='%s';" % server)
ssh_rows = c.fetchall()
ssh_port = str(ssh_rows[0]).replace("(", "").replace(",)", "")
# GET Username from database
c.execute("select username from serversmanage_servers where ip='%s';" % server)
username_rows = c.fetchall()
username = str(username_rows[0]).replace("(", "").replace(",)", "").replace("'", "")
# Check SSH Connection
ssh = subprocess.Popen(
["ssh", '-p' '%s' % ssh_port, '-o', 'UserKnownHostsFile=/root/.ssh/known_hosts', '-o',
'StrictHostKeyChecking=no', '-o', 'BatchMode=yes', '%s@%s' % (username, server), ssh_cmd],
shell=False,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE)
ssh_result = ssh.stdout.readlines()
if not ssh_result:
error = ssh.stderr.readlines()
print(sys.stderr, "ERROR: %s" % error)
hosts_down_list.append(server.rstrip())
mark_host_db_down(server)
else:
hosts_up_list.append(server.rstrip())
conn.close()
return hosts_up_list, hosts_down_list
# Core function for Rsync process
def rsync_start(server_ip, rsync_stdout):
# GET SSH Port and Hostname from database
conn = sqlite3.connect(sqlite_file_path, timeout=10)
c = conn.cursor()
c.execute("select sshport from serversmanage_servers where ip='%s';" % server_ip)
ssh_port_rows = c.fetchall()
c.execute("select hostname from serversmanage_servers where ip='%s';" % server_ip)
hostname_rows = c.fetchall()
# GET Username from database
c.execute("select username from serversmanage_servers where ip='%s';" % server_ip)
username_rows = c.fetchall()
username = str(username_rows[0]).replace("(", "").replace(",)", "").replace("'", "")
c.execute("select backuppaths from serversmanage_servers where ip='%s';" % server_ip)
# files_to_bkp_rows = List
# Data for files to backup are written like :
# [('/home,/etc/csf/csf.conf,/etc/php.ini,/etc/my.cnf',)]
# We need to iterate through these values and split them using , character
files_to_bkp_rows = c.fetchall()
# Final LIST to use, This list will be used for threading purposes in rsync functions
files_to_bkp = []
for item_in_list in files_to_bkp_rows:
# Iterating through files_to_bkp_rows will return a tuple
# AFTER LOOP : item_in_list = tuple
# Iterate through item_in_list tuple
for item_in_tuple in item_in_list:
# # AFTER LOOP : item_in_tuple = List
# Remove the blank values from item_in_tuple using filter
# files_to_bkp_pre type AFTER filter is object
# split is used to get separate values based on ,
files_to_bkp_pre = filter(None, item_in_tuple.split(','))
# Iterate through object
for i in files_to_bkp_pre:
files_to_bkp.append(i)
conn.close()
ssh_port = str(ssh_port_rows[0]).replace("(", "").replace(",)", "")
server_hostname = str(hostname_rows[0]).replace("(", "").replace(",)", "").replace("'", "")
BKPDIR_HOSTNAME = BACKUPDIR + server_hostname
def rsync_threaded(files_to_bkp):
print("Start backup for : %s On %s" % (files_to_bkp, server_hostname))
if rsync_stdout == 1:
start_backup = os.system("rsync -axSqR --delete --exclude-from=%s -e 'ssh -p %s' %s@%s:%s %s"
% (RSYNC_EXCLUDE, ssh_port, username, server_ip, files_to_bkp, BKPDIR_HOSTNAME) + "/")
print("Finished backup for : %s On %s" % (files_to_bkp, server_hostname))
else:
start_backup = os.system("rsync -axSqR --delete --exclude-from=%s -e 'ssh -p %s' %s@%s:%s %s"
% (RSYNC_EXCLUDE, ssh_port, username, server_ip, files_to_bkp, BKPDIR_HOSTNAME) + "/ ")
print("Background process is running for : %s On %s" % (files_to_bkp, server_hostname))
# Set backup status in the database
conn = sqlite3.connect(sqlite_file_path, timeout=10)
c = conn.cursor()
c.execute("""UPDATE serversmanage_servers SET lastbackup = CURRENT_TIMESTAMP WHERE ip = ? """, (server_ip, ))
c.execute("""UPDATE serversmanage_servers SET backupstatus = 'Good' WHERE ip = ? """, (server_ip, ))
conn.commit()
conn.close()
# Make the Pool of workers
pool = ThreadPool(number_of_threads)
results = pool.map(rsync_threaded, files_to_bkp)
pool.close()
pool.join()
# Monitor the backup status whether it's Ok or errors found an send mail notification
def bkp_monitor():
import datetime
from smtplib import SMTP_SSL
from email.mime.text import MIMEText
# GET a list of servers with backup errors
conn = sqlite3.connect(sqlite_file_path, timeout=10)
c = conn.cursor()
c.execute("select ip, hostname, backupstatus from serversmanage_servers where backupstatus='ERROR';")
all_rows = c.fetchall()
try:
os.remove(email_file)
except FileNotFoundError:
pass
with open(email_file, "a") as myfile:
myfile.write("Backup Status for: " + datetime.datetime.now().strftime("%B %d, %Y - %I:%M%p") + "\n\n")
# IF backup errors found, include it's information in the msg.
if all_rows:
for ip, hostname, backupstatus in all_rows:
with open(email_file, "a") as myfile:
myfile.write("IP: %s" % ip + "\n")
myfile.write("Hostname: %s" % hostname + "\n")
myfile.write("Backup Status: %s" % backupstatus + "\n============================\n")
else:
# IF all is awesome, return success msg
with open(email_file, "a") as myfile:
myfile.write(success_message_content)
conn.close()
if os.path.isfile(email_file):
content = open(email_file).read()
try:
msg = MIMEText(content, text_subtype)
msg['Subject'] = subject
msg['From'] = "RsyncHero %s" % sender
msg['To'] = destination[0]
conn = SMTP_SSL(SMTPserver)
conn.set_debuglevel(False)
conn.login(USERNAME, PASSWORD)
conn.sendmail(sender, destination, msg.as_string())
conn.close()
except EOFError:
print("mail failed")
os.remove(email_file)
def backup_all_servers():
# Get a list of up and down servers
hosts_up, hosts_down = check_alive_hosts(ssh_test_cmd)
# Prepare the list for starmap two arguments.
hosts_up_two_args = []
# Add the argument to hosts list and append to separate list.
# Output should be something like [('a', 1), ('b', 1), ('c', 1)]
for i in zip(hosts_up, repeat(rsync_stdout)):
hosts_up_two_args.append(i)
# Make the Pool of workers
pool = ThreadPool(number_of_threads)
results = pool.starmap(rsync_start, hosts_up_two_args)
pool.close()
pool.join()
def backup_one_server(server_ip):
# This function need the parameter as a list
# Prepare the list for starmap two arguments.
hosts_up = []
hosts_up_two_args = []
hosts_up.append(server_ip)
# Add the argument to hosts list and append to separate list.
# Output should be something like [('a', 1), ('b', 1), ('c', 1)]
# Always run backup one server in background ( append 0 )
for i in zip(hosts_up, repeat(0)):
hosts_up_two_args.append(i)
# Make the Pool of workers
pool = ThreadPool(number_of_threads)
results = pool.starmap(rsync_start, hosts_up_two_args)
pool.close()
pool.join()
try:
opts, args = getopt.getopt(sys.argv[1:], "hs:as:o:", ["help", "all", "ip="])
except getopt.GetoptError as err:
print("Please enter the correct opt")
sys.exit(2)
for opt, arg in opts:
if opt in ("-h", "--help"):
print("Usage : python3 main.py --ACTION")
print("Available actions are : --all, --ip")
elif opt in ("-a", "--all"):
backup_all_servers()
bkp_monitor()
if opt in ("-o", "--ip"):
value = arg
backup_one_server(value)