From 5ca6bca6be4a3a1c1be3dbb45cac130c19b6b6c8 Mon Sep 17 00:00:00 2001 From: Zbuddy13 Date: Fri, 6 Sep 2024 11:00:27 -0400 Subject: [PATCH] new functionality backup now stores metadata and data in json file. github push functionality is being worked on. there is time zone in the config and a time module --- backup.py | 8 ++++---- modules/config.py | 2 ++ modules/github.py | 11 +++++++++++ modules/schemas/postgres.py | 4 ++-- modules/time.py | 17 +++++++++++++++++ 5 files changed, 36 insertions(+), 6 deletions(-) create mode 100644 modules/github.py create mode 100644 modules/time.py diff --git a/backup.py b/backup.py index dd1cbaa..2d08ca2 100644 --- a/backup.py +++ b/backup.py @@ -31,8 +31,8 @@ def main(): # Loop throught tables and take snapshot for name in tables: f = open(str(name+".json"), "w") - tableContents = json.dumps(pg.dumpTable(dbOBJ, name), indent=4) - print("Contents to be written to file" + str(tableContents)) + combineTables = pg.dumpMetadata(dbOBJ, name) | pg.dumpTable(dbOBJ, name) + tableContents = json.dumps(combineTables, indent=4) f.write(tableContents) f.close() dbOBJ.close() @@ -46,8 +46,8 @@ def test(): print(row) if __name__ == "__main__": - #main() - test() + main() + #test() if post["backup_interval"] > 0: mainTimer = timer.initializeTimer() timer.addJob(mainTimer, main, post["backup_interval"]) diff --git a/modules/config.py b/modules/config.py index af58d55..60871d5 100644 --- a/modules/config.py +++ b/modules/config.py @@ -22,6 +22,8 @@ def importConfig(filename): conf["databases"][database]["backup_count"] = -1 if not conf["databases"][database].get("backup_location", None): conf["databases"][database]["backup_location"] = str(os.getcwd())+"/Backups" + if not conf["databases"][database].get("tz", None): + conf["databases"][database]["tz"] = 'America/New_York' else: print("databases Keyword is necessary to add a database") return conf diff --git a/modules/github.py b/modules/github.py new file mode 100644 index 0000000..5734711 --- /dev/null +++ b/modules/github.py @@ -0,0 +1,11 @@ +import os +from git import Repo +import modules.config as config + +# Gives the location of the YAML Configuration File +location = os.environ.get("config", "config.yaml") +# Set yaml config as conf +conf = config.importConfig(location) + +def setDir(location): + repo = Repo(location) \ No newline at end of file diff --git a/modules/schemas/postgres.py b/modules/schemas/postgres.py index 182acfa..06eb537 100644 --- a/modules/schemas/postgres.py +++ b/modules/schemas/postgres.py @@ -47,11 +47,11 @@ def removeUnwantedTables(tableList, unwantedList): def dumpTable(dbObject, tableName : str): with dbObject.cursor(row_factory=psycopg.rows.dict_row) as cur: cur.execute(f"Select * FROM \"{tableName}\"") - return {tableName: cur.fetchall()} + return {"Data": cur.fetchall()} # Dumps a single Postgres table def dumpMetadata(dbObject, tableName : str): with dbObject.cursor(row_factory=psycopg.rows.dict_row) as cur: cur.execute(f"SELECT column_name, data_type FROM information_schema.columns WHERE table_schema = 'public' AND table_name = \'{tableName}\';") - return cur.fetchall() + return {"Metadata": cur.fetchall()} diff --git a/modules/time.py b/modules/time.py new file mode 100644 index 0000000..e76a5bc --- /dev/null +++ b/modules/time.py @@ -0,0 +1,17 @@ +import time +from datetime import date +import os +import modules.config as config + +# Gives the location of the YAML Configuration File +location = os.environ.get("config", "config.yaml") +# Set yaml config as conf +conf = config.importConfig(location) + +os.environ['TZ'] = conf["databases"]["postgres"]["tz"] + +def getTime(): + now = time.localtime() + current_time = time.strftime("%H:%M:%S", now) + currentDateTime = str(date.today()) + " " + str(current_time) # Todays Date + return currentDateTime \ No newline at end of file