Skip to content

Commit

Permalink
added backup location
Browse files Browse the repository at this point in the history
  • Loading branch information
Zbuddy13 committed Sep 5, 2024
1 parent a517b1f commit 8de3638
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 3 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,5 @@
.idea
*.pyc
config.yaml
/Backups
/BackupFiles
14 changes: 12 additions & 2 deletions backup.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@
# Set yaml config as conf
conf = config.importConfig(location)
post = conf["databases"]["postgres"]
# Set path for backup files to go to
savepath = post["backup_location"]
if not os.path.exists(savepath):
os.mkdir(savepath)
os.chdir(savepath)

def main():
# import config, print config, connect to db, get the list of tables, remove unwanted, dump each table
Expand All @@ -24,12 +29,17 @@ def main():
dbTableList = pg.getTableList(dbOBJ)
tables = pg.removeUnwantedTables(dbTableList, post["excluded_tables"])
# Loop throught tables and take snapshot
for x in tables:
print(pg.dumpTable(dbOBJ, x))
for name in tables:
f = open(str(name+".json"), "w")
tableContents = json.dumps(pg.dumpTable(dbOBJ, name), indent=2)
print("Contents to be written to file" + str(tableContents))
f.write(tableContents)
f.close()
dbOBJ.close()

if __name__ == "__main__":
main()
print(os.getcwd())
if post["backup_interval"] > 0:
mainTimer = timer.initializeTimer()
timer.addJob(mainTimer, main, post["backup_interval"])
Expand Down
3 changes: 2 additions & 1 deletion example.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,5 @@
# - table2
# driver: postgres/mysql (postgres Default)
# backup_interval: (in hours) 1 Default
# backup_count: 5 Default
# backup_count: 5 Default
# backup_location: /
3 changes: 3 additions & 0 deletions modules/config.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import os
import yaml
import modules.error as error

Expand All @@ -19,6 +20,8 @@ def importConfig(filename):
conf["databases"][database]["backup_interval"] = -1
if not conf["databases"][database].get("backup_count", None):
conf["databases"][database]["backup_count"] = -1
if not conf["databases"][database].get("backup_location", None):
conf["databases"][database]["backup_location"] = str(os.getcwd())+"/Backups"
else:
print("databases Keyword is necessary to add a database")
return conf
Expand Down

0 comments on commit 8de3638

Please sign in to comment.