mirror of
https://github.com/jokob-sk/NetAlertX.git
synced 2025-12-07 09:36:05 -08:00
CSV backup work
This commit is contained in:
0
front/php/server/traceroute.php
Normal file → Executable file
0
front/php/server/traceroute.php
Normal file → Executable file
@@ -4,10 +4,5 @@ Plugin generating CSV backups of your Devices database table, including the netw
|
|||||||
|
|
||||||
### Usage
|
### Usage
|
||||||
|
|
||||||
- Go to settings and find TBC
|
- If the devices.csv file can be overwritten or the date and time timestamp added to the name. This is toggled with the `CSVBCKP_overwrite` setting.
|
||||||
|
|
||||||
#### Examples:
|
|
||||||
- TBC
|
|
||||||
|
|
||||||
### Known Limitations
|
|
||||||
- TBC
|
|
||||||
|
|||||||
@@ -49,7 +49,7 @@
|
|||||||
"function": "RUN",
|
"function": "RUN",
|
||||||
"type": "text.select",
|
"type": "text.select",
|
||||||
"default_value":"disabled",
|
"default_value":"disabled",
|
||||||
"options": ["disabled", "once", "always_after_scan"],
|
"options": ["disabled", "once", "schedule", "always_after_scan", "on_new_device"],
|
||||||
"localized": ["name", "description"],
|
"localized": ["name", "description"],
|
||||||
"name" :[{
|
"name" :[{
|
||||||
"language_code":"en_us",
|
"language_code":"en_us",
|
||||||
@@ -61,7 +61,7 @@
|
|||||||
}],
|
}],
|
||||||
"description": [{
|
"description": [{
|
||||||
"language_code":"en_us",
|
"language_code":"en_us",
|
||||||
"string" : "When the backup should be created. A daily SCHEDULE is a good option."
|
"string" : "When the backup should be created. A daily or weekly <code>SCHEDULE</code> is a good option."
|
||||||
}]
|
}]
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
@@ -94,7 +94,7 @@
|
|||||||
{
|
{
|
||||||
"function": "RUN_SCHD",
|
"function": "RUN_SCHD",
|
||||||
"type": "text",
|
"type": "text",
|
||||||
"default_value":"0 2 3 * *",
|
"default_value":"0 2 * * 3",
|
||||||
"options": [],
|
"options": [],
|
||||||
"localized": ["name", "description"],
|
"localized": ["name", "description"],
|
||||||
"name" : [{
|
"name" : [{
|
||||||
@@ -153,7 +153,7 @@
|
|||||||
}],
|
}],
|
||||||
"description": [{
|
"description": [{
|
||||||
"language_code":"en_us",
|
"language_code":"en_us",
|
||||||
"string" : "If the devices.csv file should be always overwritten. If disabled, the date and time is added to the name."
|
"string" : "If the <code>devices.csv</code> file should be always overwritten. If disabled, the date and time is added to the name."
|
||||||
}]
|
}]
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
@@ -168,7 +168,7 @@
|
|||||||
}],
|
}],
|
||||||
"description": [{
|
"description": [{
|
||||||
"language_code":"en_us",
|
"language_code":"en_us",
|
||||||
"string" : "Where the devices.csv file should be saved."
|
"string" : "Where the <code>devices.csv</code> file should be saved. For example <code>/home/pi/pialert/config</code>."
|
||||||
}]
|
}]
|
||||||
}
|
}
|
||||||
],
|
],
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
#!/usr/bin/env python
|
#!/usr/bin/env python
|
||||||
# test script by running python script.py devices=test,dummy
|
# test script by running:
|
||||||
|
# /home/pi/pialert/front/plugins/csv_backup/script.py overwrite=False location=/home/pi/pialert/config
|
||||||
|
|
||||||
import os
|
import os
|
||||||
import pathlib
|
import pathlib
|
||||||
@@ -7,12 +8,13 @@ import argparse
|
|||||||
import sys
|
import sys
|
||||||
import hashlib
|
import hashlib
|
||||||
import csv
|
import csv
|
||||||
|
import sqlite3
|
||||||
from io import StringIO
|
from io import StringIO
|
||||||
|
from datetime import datetime
|
||||||
|
|
||||||
sys.path.append("/home/pi/pialert/front/plugins")
|
sys.path.append("/home/pi/pialert/front/plugins")
|
||||||
sys.path.append('/home/pi/pialert/pialert')
|
sys.path.append('/home/pi/pialert/pialert')
|
||||||
|
|
||||||
import database
|
|
||||||
from plugin_helper import Plugin_Object, Plugin_Objects, decodeBase64
|
from plugin_helper import Plugin_Object, Plugin_Objects, decodeBase64
|
||||||
from logger import mylog, append_line_to_file
|
from logger import mylog, append_line_to_file
|
||||||
from helper import timeNowTZ
|
from helper import timeNowTZ
|
||||||
@@ -26,53 +28,66 @@ RESULT_FILE = os.path.join(CUR_PATH, 'last_result.log')
|
|||||||
def main():
|
def main():
|
||||||
|
|
||||||
# the script expects a parameter in the format of devices=device1,device2,...
|
# the script expects a parameter in the format of devices=device1,device2,...
|
||||||
parser = argparse.ArgumentParser(description='TBC')
|
parser = argparse.ArgumentParser(description='Export devices data to CSV')
|
||||||
parser.add_argument('location', action="store", help="TBC")
|
parser.add_argument('overwrite', action="store", help="Specify 'TRUE' to overwrite an existing file, or 'FALSE' to create a new file")
|
||||||
parser.add_argument('overwrite', action="store", help="TBC")
|
parser.add_argument('location', action="store", help="The directory where the CSV file will be saved")
|
||||||
values = parser.parse_args()
|
values = parser.parse_args()
|
||||||
|
|
||||||
|
overwrite = values.overwrite.split('=')[1]
|
||||||
|
|
||||||
|
if (overwrite.upper() == "TRUE"):
|
||||||
|
overwrite = True
|
||||||
|
else:
|
||||||
|
overwrite = False
|
||||||
|
|
||||||
mylog('verbose', ['[CSVBCKP] In script'])
|
mylog('verbose', ['[CSVBCKP] In script'])
|
||||||
|
|
||||||
# plugin_objects = Plugin_Objects( RESULT_FILE )
|
# Connect to the PiAlert SQLite database
|
||||||
|
conn = sqlite3.connect('/home/pi/pialert/db/pialert.db')
|
||||||
|
cursor = conn.cursor()
|
||||||
|
|
||||||
# if values.devices:
|
# Execute your SQL query
|
||||||
# for fake_dev in values.devices.split('=')[1].split(','):
|
cursor.execute("SELECT * FROM Devices")
|
||||||
|
|
||||||
# fake_mac = string_to_mac_hash(fake_dev)
|
# Get column names
|
||||||
|
columns = [desc[0] for desc in cursor.description]
|
||||||
|
|
||||||
# plugin_objects.add_object(
|
if overwrite:
|
||||||
# primaryId=fake_dev, # MAC (Device Name)
|
filename = 'devices.csv'
|
||||||
# secondaryId="0.0.0.0", # IP Address (always 0.0.0.0)
|
else:
|
||||||
# watched1=fake_dev, # Device Name
|
timestamp = datetime.now().strftime('%Y%m%d%H%M%S')
|
||||||
# watched2="",
|
filename = f'devices_{timestamp}.csv'
|
||||||
# watched3="",
|
|
||||||
# watched4="",
|
|
||||||
# extra="",
|
|
||||||
# foreignKey=fake_mac)
|
|
||||||
|
|
||||||
# plugin_objects.write_result_file()
|
fullPath = os.path.join(values.location.split('=')[1], filename)
|
||||||
|
|
||||||
# # Execute your SQL query
|
mylog('verbose', ['[CSVBCKP] Writing file ', fullPath])
|
||||||
# cursor.execute("SELECT * FROM Devices")
|
|
||||||
|
|
||||||
# # Get column names
|
# Create a CSV file in the specified location
|
||||||
# columns = [desc[0] for desc in cursor.description]
|
with open(fullPath, 'w', newline='') as csvfile:
|
||||||
|
# Initialize the CSV writer
|
||||||
|
csv_writer = csv.writer(csvfile, delimiter=',', quoting=csv.QUOTE_MINIMAL)
|
||||||
|
|
||||||
# # Initialize the CSV writer
|
# Wrap the header values in double quotes and write the header row
|
||||||
# csv_writer = csv.writer(output, quoting=csv.QUOTE_MINIMAL)
|
csv_writer.writerow([ '"' + col + '"' for col in columns])
|
||||||
|
|
||||||
# # Write the header row
|
# Fetch and write data rows
|
||||||
# csv_writer.writerow(columns)
|
for row in cursor.fetchall():
|
||||||
|
# Wrap each value in double quotes and write the row
|
||||||
|
csv_writer.writerow(['"' + str(value) + '"' for value in row])
|
||||||
|
|
||||||
# # Fetch and write data rows
|
# Close the database connection
|
||||||
# for row in cursor.fetchall():
|
conn.close()
|
||||||
# csv_writer.writerow(row)
|
|
||||||
|
|
||||||
# # Close the database connection
|
# Open the CSV file for reading
|
||||||
# conn.close()
|
with open(fullPath, 'r') as file:
|
||||||
|
data = file.read()
|
||||||
|
|
||||||
# # Prepare the CSV data for download
|
# Replace all occurrences of """ with "
|
||||||
# csv_data = output.getvalue()
|
data = data.replace('"""', '"')
|
||||||
|
|
||||||
|
# Open the CSV file for writing
|
||||||
|
with open(fullPath, 'w') as file:
|
||||||
|
file.write(data)
|
||||||
|
|
||||||
return 0
|
return 0
|
||||||
|
|
||||||
|
|||||||
@@ -19,8 +19,6 @@ def timeNowTZ():
|
|||||||
return datetime.datetime.now().replace(microsecond=0)
|
return datetime.datetime.now().replace(microsecond=0)
|
||||||
|
|
||||||
|
|
||||||
# conf.LOG_LEVEL = get_setting_value("LOG_LEVEL")
|
|
||||||
|
|
||||||
#-------------------------------------------------------------------------------
|
#-------------------------------------------------------------------------------
|
||||||
debugLevels = [
|
debugLevels = [
|
||||||
('none', 0), ('minimal', 1), ('verbose', 2), ('debug', 3)
|
('none', 0), ('minimal', 1), ('verbose', 2), ('debug', 3)
|
||||||
@@ -50,22 +48,8 @@ def file_print (*args):
|
|||||||
result += str(arg)
|
result += str(arg)
|
||||||
print(result)
|
print(result)
|
||||||
|
|
||||||
# try:
|
|
||||||
# # # Open the file
|
|
||||||
# # file = open(logPath + "/pialert.log", "a")
|
|
||||||
|
|
||||||
# # # Write to the file
|
|
||||||
# # file.write(result + '\n')
|
|
||||||
|
|
||||||
# # # Close the file
|
|
||||||
# # file.close()
|
|
||||||
|
|
||||||
append_to_file_with_timeout(logPath + "/pialert.log", result + '\n', 5)
|
append_to_file_with_timeout(logPath + "/pialert.log", result + '\n', 5)
|
||||||
|
|
||||||
# except Exception as e:
|
|
||||||
# # Handle the exception, e.g., log it or print an error message
|
|
||||||
# print(f"Error opening or writing to the file: {e}")
|
|
||||||
|
|
||||||
#-------------------------------------------------------------------------------
|
#-------------------------------------------------------------------------------
|
||||||
# Function to append to the file
|
# Function to append to the file
|
||||||
def append_to_file(file_path, data):
|
def append_to_file(file_path, data):
|
||||||
|
|||||||
Reference in New Issue
Block a user