Dave67350 commited on
Commit
60abeb3
·
verified ·
1 Parent(s): 3082568

Create nis2_backup_recovery_log.py

Browse files
Files changed (1) hide show
  1. tools/nis2_backup_recovery_log.py +123 -0
tools/nis2_backup_recovery_log.py ADDED
@@ -0,0 +1,123 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # tools/nis2_backup_recovery_log.py
2
+
3
+ import datetime
4
+ import re
5
+ from fpdf import FPDF
6
+ from langdetect import detect
7
+ import gradio as gr
8
+ from tools.common import prepend_metadata_questions
9
+
10
+ # === PDF Export ===
11
+ def export_text_to_pdf(text, metadata=None, output_path=None, language="en"):
12
+ if output_path is None:
13
+ timestamp = datetime.datetime.now().strftime("%Y%m%d_%H%M%S")
14
+ output_path = f"backup_recovery_log_{timestamp}.pdf"
15
+
16
+ pdf = FPDF()
17
+ pdf.add_page()
18
+ pdf.set_auto_page_break(auto=True, margin=15)
19
+
20
+ # Title
21
+ pdf.set_font("Arial", 'B', 16)
22
+ pdf.set_text_color(0, 51, 102)
23
+ title = "Backup & Recovery Log" if language == "en" else "Journal de Sauvegarde et de Restauration"
24
+ pdf.cell(0, 15, title, ln=True, align='C')
25
+ pdf.ln(10)
26
+
27
+ # Metadata
28
+ if metadata:
29
+ pdf.set_font("Arial", '', 12)
30
+ pdf.set_text_color(90, 90, 90)
31
+ pdf.multi_cell(0, 10, f"Organization: {metadata.get('organization_name', 'N/A')}")
32
+ pdf.multi_cell(0, 10, f"Completed by: {metadata.get('user_name', 'N/A')} ({metadata.get('user_role', 'N/A')})")
33
+ pdf.multi_cell(0, 10, f"Timestamp: {metadata.get('timestamp', 'N/A')}")
34
+ pdf.ln(5)
35
+
36
+ # Body
37
+ pdf.set_font("Arial", '', 12)
38
+ pdf.set_text_color(0, 0, 0)
39
+ for line in text.strip().split('\n'):
40
+ if line.startswith("## "):
41
+ section = line.replace("## ", "").strip()
42
+ pdf.set_font("Arial", 'B', 13)
43
+ pdf.set_text_color(30, 30, 120)
44
+ pdf.ln(8)
45
+ pdf.cell(0, 10, section, ln=True)
46
+ pdf.set_font("Arial", '', 12)
47
+ pdf.set_text_color(0, 0, 0)
48
+ elif line.startswith("- **"):
49
+ match = re.match(r"- \*\*(.+?)\*\*: (.+)", line)
50
+ if match:
51
+ label, value = match.groups()
52
+ pdf.set_font("Arial", 'B', 12)
53
+ pdf.cell(0, 10, f"{label}:", ln=True)
54
+ pdf.set_font("Arial", '', 12)
55
+ pdf.multi_cell(0, 10, value)
56
+ else:
57
+ pdf.multi_cell(0, 10, line)
58
+
59
+ pdf.output(output_path)
60
+ return output_path
61
+
62
+ # === Questions ===
63
+ QUESTIONS = prepend_metadata_questions([
64
+ ("backup_frequency", "How often are backups performed?"),
65
+ ("backup_type", "What types of data are backed up (e.g. full, incremental)?"),
66
+ ("storage_location", "Where are backups stored (on-prem, cloud, etc.)?"),
67
+ ("retention_policy", "What is the data retention policy?"),
68
+ ("recovery_test_frequency", "How often are recovery procedures tested?"),
69
+ ("last_successful_backup", "When was the last successful backup?"),
70
+ ("last_tested_recovery", "When was the last recovery test performed?"),
71
+ ("responsible_person", "Who is responsible for the backup and recovery processes?")
72
+ ])
73
+
74
+ def get_questions():
75
+ return QUESTIONS
76
+
77
+ # === Execution ===
78
+ def run_tool():
79
+ state = {"step": 0, "answers": {}}
80
+
81
+ def step_by_step_agent(user_input, state):
82
+ step = state["step"]
83
+ answers = state["answers"]
84
+ if step > 0:
85
+ key, _ = QUESTIONS[step - 1]
86
+ answers[key] = user_input
87
+ if step < len(QUESTIONS):
88
+ next_q = QUESTIONS[step][1]
89
+ state["step"] += 1
90
+ return next_q, state, None
91
+
92
+ content = "\n".join([f"- **{label}**: {answers.get(key, '')}" for key, label in QUESTIONS])
93
+ lang = detect(content)
94
+ metadata = {
95
+ "user_name": answers.get("user_name", "N/A"),
96
+ "user_role": answers.get("user_role", "N/A"),
97
+ "organization_name": answers.get("organization_name", "N/A"),
98
+ "timestamp": datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")
99
+ }
100
+ pdf_path = export_text_to_pdf(content, metadata=metadata, language=lang)
101
+ return "✅ Backup & recovery log complete. Download below.", {"done": True}, pdf_path
102
+
103
+ with gr.Blocks(title="Backup & Recovery Log Tool") as demo:
104
+ chatbot = gr.Chatbot(label="💾 Backup Assistant", value=[{"role": "assistant", "content": QUESTIONS[0][1]}], type="messages")
105
+ msg = gr.Textbox(label="Your answer")
106
+ state_var = gr.State(state)
107
+ file_output = gr.File(label="Download PDF")
108
+ reset_btn = gr.Button("🔁 Restart")
109
+
110
+ def chat_logic(msg_in, state_in):
111
+ reply, updated_state, file = step_by_step_agent(msg_in, state_in)
112
+ messages = [{"role": "user", "content": msg_in}]
113
+ if reply:
114
+ messages.append({"role": "assistant", "content": reply})
115
+ return messages, updated_state, file
116
+
117
+ def reset():
118
+ return [{"role": "assistant", "content": QUESTIONS[0][1]}], {"step": 0, "answers": {}}, None
119
+
120
+ msg.submit(chat_logic, [msg, state_var], [chatbot, state_var, file_output])
121
+ reset_btn.click(reset, outputs=[chatbot, state_var, file_output])
122
+
123
+ demo.launch(show_api=False)