Page 1 of 1

Configuration Autosave and Versioning/Archiving

Posted: Fri Dec 30, 2011 9:06 pm
by derelict
Hi All

Today i had the need to finally implement something that autosaves my current running eventghost configuration and also copies it away in case i need it later.

Well... it's done... and i decided to "release" it to the public in order to give something back to the community!!!

i hope that someone has a use or need for it ;-) it's no big thing though !

Features:
------------------------

- Autosaves the configuration (if needed/the config has changed) every 5 minutes (or to the schedule YOU define) and copies that configuration into a folder called _archive in the same directory where your eventghost xml file lives (the folder is automatically created if needed!), prefixed with the current year, month,day,hour,minute and second

- Autosaves the configuration ("forced") every 12 hours (or to the schedule YOU define) and copies that configuration to the _archive folder (same "rules" apply like above)

- After every save (either forced or automatic) the _archive folder will be examined for "expired" files... and all files older than 15 days will be deleted automatically. This value can be changed in the python script in the EG Archive Rentention Macro.

- The needed ScheduleGhost Triggers are already included in the "One Time Schedules Setup" Macro.... just run each entry manually (F5) and an according schedule ghost entry will be created for you!

Requirement:
---------------------------
- ScheduleGhost Plugin

Please be aware that this is a "Works for Me" Release... make your checks and try to help yourselfs in case you have problems... i also take no responsibilty for any damage caused by my solution ;-)

Code: Select all

<?xml version="1.0" encoding="UTF-8" ?>
<EventGhost Version="1540">
    <Folder Name="EG Autosave/Archive/Retention" Expanded="True">
        <Macro Name="EG Autosave" Expanded="True">
            <Event Name="EGConfigManagement.AutoSave" />
            <Action Name="Save if changed">
                EventGhost.PythonScript(u'print "---------------------------------"\nprint "Checking if eg config has changed"\nprint "---------------------------------"\n\nif eg.document.isDirty:\n    print "... config has changed"\n    print "... Saving"\n    eg.TriggerEvent("Save", prefix = "EGConfigManagement")\n')
            </Action>
        </Macro>
        <Macro Name="EG Save" Expanded="True">
            <Event Name="EGConfigManagement.Save" />
            <Action Name="Save and archive">
                EventGhost.PythonScript(u'import shutil, os, datetime, time\n\nnow = datetime.datetime.now()\n\nfiledate = now.strftime("%Y-%m-%d___%H-%M-%S")\nconfigfile = eg.document.filePath\narchivepath = os.path.dirname(configfile) + "\\\\_archive\\\\"\nsavefile = archivepath + r"\\\\" + filedate + "__." + os.path.basename(configfile)\n\nprint "---------------------------------"\nprint "Saving current Configuration"\nprint "---------------------------------"\n\neg.document.Save()\n\nif not os.path.isdir(archivepath):\n    print "... Creating Missing Archive Folder"\n    os.makedirs(archivepath)\n\nprint "... Copying File to archive location"\nshutil.copy2(configfile, savefile)\n\neg.TriggerEvent("ArchiveRetention", prefix = "EGConfigManagement")\n\n\n\n    ')
            </Action>
        </Macro>
        <Macro Name="EG Archive Retention" Expanded="True">
            <Event Name="EGConfigManagement.ArchiveRetention" />
            <Action Name="archive retention">
                EventGhost.PythonScript(u'import os, time\n\n#Change ME:\negarchiveretentiondays = 15\n\negarchiveretentioninseconds = egarchiveretentiondays * 86400\n\n# For Testing:\n#egarchiveretentioninseconds = 5\n\n\nprint "----------------------------------------------------------------"\nprint "Performing EG Config Archive Rentention of " + str(egarchiveretentiondays) + " days"\nprint "----------------------------------------------------------------"\n\nconfigfile = eg.document.filePath\narchivepath = os.path.dirname(configfile) + "\\\\_archive\\\\"\n\nnow = time.time()\n\nfor f in os.listdir(archivepath):\n\n    if os.stat(os.path.join(archivepath, f)).st_mtime < now - egarchiveretentioninseconds:\n        if os.path.isfile(os.path.join(archivepath, f)):\n            print "... Removing: " + os.path.join(archivepath, f)\n            os.remove(os.path.join(archivepath, f))\n            \n            \n   ')
            </Action>
        </Macro>
        <Macro Name="One Time Schedules Setup" Expanded="True">
            <Action>
                SchedulGhost.AddSchedule(u"[1, u'EG_Auto_Save_Check', 5, [u'00:00:00', u'00:00:00', '2011-01-01', 5, 1], '', u'EGConfigManagement', u'AutoSave', '', '']")
            </Action>
            <Action>
                SchedulGhost.AddSchedule(u"[1, u'EG_Auto_Save', 5, [u'00:00:00', u'00:00:00', u'2011-01-01', 12, 2], '', u'EGConfigManagement', u'Save', '', '']")
            </Action>
        </Macro>
    </Folder>
</EventGhost>
Have Fun with it... and let me know if it worked out well for you guys!

Marcel