CSV backup work

This commit is contained in:
Jokob-sk
2023-09-07 08:09:25 +10:00
parent 8676b7ccde
commit 296891ca67
4 changed files with 278 additions and 1 deletions

View File

@@ -0,0 +1,13 @@
## Overview
Plugin generating CSV backups of your Devices database table, including the network mappings. Can be used for importing your setup via the Maintenance > Backup / Restore > CSV Import feature.
### Usage
- Go to settings and find TBC
#### Examples:
- TBC
### Known Limitations
- TBC

View File

@@ -0,0 +1,180 @@
{
"code_name": "csv_backup",
"unique_prefix": "CSVBCKP",
"enabled": true,
"data_source": "script",
"show_ui": false,
"localized": ["display_name", "description", "icon"],
"display_name": [
{
"language_code": "en_us",
"string": "CSV backup"
},
{
"language_code": "es_es",
"string": "N/A"
}
],
"icon": [
{
"language_code": "en_us",
"string": "<i class=\"fa-solid fa-save\"></i>"
}
],
"description": [
{
"language_code": "en_us",
"string": "A plugin to auto-generate devices.csv backups."
},
{
"language_code": "es_es",
"string": "N/A"
}
],
"params" : [{
"name" : "overwrite",
"type" : "setting",
"value" : "CSVBCKP_overwrite"
},
{
"name" : "location",
"type" : "setting",
"value" : "CSVBCKP_location"
}
],
"settings": [
{
"function": "RUN",
"type": "text.select",
"default_value":"disabled",
"options": ["disabled", "once", "always_after_scan"],
"localized": ["name", "description"],
"name" :[{
"language_code":"en_us",
"string" : "When to run"
},
{
"language_code":"es_es",
"string" : "Cuándo ejecuta"
}],
"description": [{
"language_code":"en_us",
"string" : "When the backup should be created. A daily SCHEDULE is a good option."
}]
},
{
"function": "CMD",
"type": "readonly",
"default_value": "python3 /home/pi/pialert/front/plugins/csv_backup/script.py overwrite={overwrite} location={location}",
"options": [],
"localized": ["name", "description"],
"name": [
{
"language_code": "en_us",
"string": "Command"
},
{
"language_code": "es_es",
"string": "Comando"
}
],
"description": [
{
"language_code": "en_us",
"string": "Command to run. This can not be changed"
},
{
"language_code": "es_es",
"string": "Comando a ejecutar. Esto no se puede cambiar"
}
]
},
{
"function": "RUN_SCHD",
"type": "text",
"default_value":"0 2 3 * *",
"options": [],
"localized": ["name", "description"],
"name" : [{
"language_code":"en_us",
"string" : "Schedule"
},
{
"language_code":"es_es",
"string" : "Schedule"
}],
"description": [{
"language_code":"en_us",
"string" : "Only enabled if you select <code>schedule</code> in the <a href=\"#CSVBCKP_RUN\"><code>CSVBCKP_RUN</code> setting</a>. Make sure you enter the schedule in the correct cron-like format (e.g. validate at <a href=\"https://crontab.guru/\" target=\"_blank\">crontab.guru</a>). For example entering <code>0 4 * * *</code> will run the scan after 4 am in the <a onclick=\"toggleAllSettings()\" href=\"#TIMEZONE\"><code>TIMEZONE</code> you set above</a>. Will be run NEXT time the time passes."
},
{
"language_code":"es_es",
"string" : "Solo está habilitado si selecciona <code>schedule</code> en la configuración <a href=\"#CSVBCKP_RUN\"><code>CSVBCKP_RUN</code></a>. Asegúrese de ingresar la programación en el formato similar a cron correcto (por ejemplo, valide en <a href=\"https://crontab.guru/\" target=\"_blank\">crontab.guru</a>). Por ejemplo, ingresar <code>0 4 * * *</code> ejecutará el escaneo después de las 4 a.m. en el <a onclick=\"toggleAllSettings()\" href=\"#TIMEZONE\"><code>TIMEZONE</ código> que configuró arriba</a>. Se ejecutará la PRÓXIMA vez que pase el tiempo."
}]
},
{
"function": "RUN_TIMEOUT",
"type": "integer",
"default_value": 30,
"options": [],
"localized": ["name", "description"],
"name": [
{
"language_code": "en_us",
"string": "Run timeout"
},
{
"language_code": "es_es",
"string": "Tiempo límite de ejecución"
}
],
"description": [
{
"language_code": "en_us",
"string": "Maximum time in seconds to wait for the script to finish. If this time is exceeded the script is aborted."
},
{
"language_code": "es_es",
"string": "Tiempo máximo en segundos para esperar a que finalice el script. Si se supera este tiempo, el script se cancela."
}
]
},
{
"function": "overwrite",
"type": "boolean",
"default_value":false,
"options": [],
"localized": ["name", "description"],
"name" : [{
"language_code":"en_us",
"string" : "Overwrite file"
}],
"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."
}]
},
{
"function": "location",
"type": "text",
"default_value":"/home/pi/pialert/config",
"options": [],
"localized": ["name", "description"],
"name" : [{
"language_code":"en_us",
"string" : "File location"
}],
"description": [{
"language_code":"en_us",
"string" : "Where the devices.csv file should be saved."
}]
}
],
"database_column_definitions":
[
]
}

View File

@@ -0,0 +1,84 @@
#!/usr/bin/env python
# test script by running python script.py devices=test,dummy
import os
import pathlib
import argparse
import sys
import hashlib
import csv
from io import StringIO
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
from const import logPath, pialertPath
CUR_PATH = str(pathlib.Path(__file__).parent.resolve())
LOG_FILE = os.path.join(CUR_PATH, 'script.log')
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")
values = parser.parse_args()
mylog('verbose', ['[CSVBCKP] In script'])
# plugin_objects = Plugin_Objects( RESULT_FILE )
# if values.devices:
# for fake_dev in values.devices.split('=')[1].split(','):
# fake_mac = string_to_mac_hash(fake_dev)
# 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)
# plugin_objects.write_result_file()
# # Execute your SQL query
# cursor.execute("SELECT * FROM Devices")
# # Get column names
# columns = [desc[0] for desc in cursor.description]
# # Initialize the CSV writer
# csv_writer = csv.writer(output, quoting=csv.QUOTE_MINIMAL)
# # Write the header row
# csv_writer.writerow(columns)
# # Fetch and write data rows
# for row in cursor.fetchall():
# csv_writer.writerow(row)
# # Close the database connection
# conn.close()
# # Prepare the CSV data for download
# csv_data = output.getvalue()
return 0
#===============================================================================
# BEGIN
#===============================================================================
if __name__ == '__main__':
main()

View File

@@ -581,7 +581,7 @@
{
"function": "version",
"type": "text",
"default_value":"",
"default_value":"v4",
"options": [],
"localized": ["name", "description"],
"name" : [{