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
|
||||
|
||||
- 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",
|
||||
"type": "text.select",
|
||||
"default_value":"disabled",
|
||||
"options": ["disabled", "once", "always_after_scan"],
|
||||
"options": ["disabled", "once", "schedule", "always_after_scan", "on_new_device"],
|
||||
"localized": ["name", "description"],
|
||||
"name" :[{
|
||||
"language_code":"en_us",
|
||||
@@ -61,7 +61,7 @@
|
||||
}],
|
||||
"description": [{
|
||||
"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",
|
||||
"type": "text",
|
||||
"default_value":"0 2 3 * *",
|
||||
"default_value":"0 2 * * 3",
|
||||
"options": [],
|
||||
"localized": ["name", "description"],
|
||||
"name" : [{
|
||||
@@ -153,7 +153,7 @@
|
||||
}],
|
||||
"description": [{
|
||||
"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": [{
|
||||
"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
|
||||
# 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 pathlib
|
||||
@@ -7,12 +8,13 @@ import argparse
|
||||
import sys
|
||||
import hashlib
|
||||
import csv
|
||||
import sqlite3
|
||||
from io import StringIO
|
||||
from datetime import datetime
|
||||
|
||||
sys.path.append("/home/pi/pialert/front/plugins")
|
||||
sys.path.append('/home/pi/pialert/pialert')
|
||||
|
||||
import database
|
||||
from plugin_helper import Plugin_Object, Plugin_Objects, decodeBase64
|
||||
from logger import mylog, append_line_to_file
|
||||
from helper import timeNowTZ
|
||||
@@ -26,53 +28,66 @@ RESULT_FILE = os.path.join(CUR_PATH, 'last_result.log')
|
||||
def main():
|
||||
|
||||
# the script expects a parameter in the format of devices=device1,device2,...
|
||||
parser = argparse.ArgumentParser(description='TBC')
|
||||
parser.add_argument('location', action="store", help="TBC")
|
||||
parser.add_argument('overwrite', action="store", help="TBC")
|
||||
parser = argparse.ArgumentParser(description='Export devices data to CSV')
|
||||
parser.add_argument('overwrite', action="store", help="Specify 'TRUE' to overwrite an existing file, or 'FALSE' to create a new file")
|
||||
parser.add_argument('location', action="store", help="The directory where the CSV file will be saved")
|
||||
values = parser.parse_args()
|
||||
|
||||
overwrite = values.overwrite.split('=')[1]
|
||||
|
||||
if (overwrite.upper() == "TRUE"):
|
||||
overwrite = True
|
||||
else:
|
||||
overwrite = False
|
||||
|
||||
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:
|
||||
# for fake_dev in values.devices.split('=')[1].split(','):
|
||||
# Execute your SQL query
|
||||
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(
|
||||
# primaryId=fake_dev, # MAC (Device Name)
|
||||
# secondaryId="0.0.0.0", # IP Address (always 0.0.0.0)
|
||||
# watched1=fake_dev, # Device Name
|
||||
# watched2="",
|
||||
# watched3="",
|
||||
# watched4="",
|
||||
# extra="",
|
||||
# foreignKey=fake_mac)
|
||||
if overwrite:
|
||||
filename = 'devices.csv'
|
||||
else:
|
||||
timestamp = datetime.now().strftime('%Y%m%d%H%M%S')
|
||||
filename = f'devices_{timestamp}.csv'
|
||||
|
||||
# plugin_objects.write_result_file()
|
||||
fullPath = os.path.join(values.location.split('=')[1], filename)
|
||||
|
||||
# # Execute your SQL query
|
||||
# cursor.execute("SELECT * FROM Devices")
|
||||
mylog('verbose', ['[CSVBCKP] Writing file ', fullPath])
|
||||
|
||||
# # Get column names
|
||||
# columns = [desc[0] for desc in cursor.description]
|
||||
# Create a CSV file in the specified location
|
||||
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
|
||||
# csv_writer = csv.writer(output, quoting=csv.QUOTE_MINIMAL)
|
||||
# Wrap the header values in double quotes and write the header row
|
||||
csv_writer.writerow([ '"' + col + '"' for col in columns])
|
||||
|
||||
# # Write the header row
|
||||
# csv_writer.writerow(columns)
|
||||
# Fetch and write data rows
|
||||
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
|
||||
# for row in cursor.fetchall():
|
||||
# csv_writer.writerow(row)
|
||||
# Close the database connection
|
||||
conn.close()
|
||||
|
||||
# # Close the database connection
|
||||
# conn.close()
|
||||
# Open the CSV file for reading
|
||||
with open(fullPath, 'r') as file:
|
||||
data = file.read()
|
||||
|
||||
# # Prepare the CSV data for download
|
||||
# csv_data = output.getvalue()
|
||||
# Replace all occurrences of """ with "
|
||||
data = data.replace('"""', '"')
|
||||
|
||||
# Open the CSV file for writing
|
||||
with open(fullPath, 'w') as file:
|
||||
file.write(data)
|
||||
|
||||
return 0
|
||||
|
||||
|
||||
@@ -19,8 +19,6 @@ def timeNowTZ():
|
||||
return datetime.datetime.now().replace(microsecond=0)
|
||||
|
||||
|
||||
# conf.LOG_LEVEL = get_setting_value("LOG_LEVEL")
|
||||
|
||||
#-------------------------------------------------------------------------------
|
||||
debugLevels = [
|
||||
('none', 0), ('minimal', 1), ('verbose', 2), ('debug', 3)
|
||||
@@ -50,22 +48,8 @@ def file_print (*args):
|
||||
result += str(arg)
|
||||
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)
|
||||
|
||||
# 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
|
||||
def append_to_file(file_path, data):
|
||||
|
||||
Reference in New Issue
Block a user