EG Backup script

Do you have questions about writing plugins or scripts in Python? Meet the coders here.
Post Reply
User avatar
kgschlosser
Site Admin
Posts: 5146
Joined: Fri Jun 05, 2015 5:43 am
Location: Rocky Mountains, Colorado USA

EG Backup script

Post by kgschlosser » Wed Dec 04, 2019 11:57 pm

For those of you that like to make backups.. This is a handy bit of code that will backup EG all plugins, all plugin data as well. it will make a zip of all of it and save the zip to the desktop.

Code: Select all

<?xml version="1.0" encoding="UTF-8" ?>
<EventGhost Version="0.5.0-rc6">
    <Action Name="eg_backup_script" XML_Guid="{1BEC413B-A87F-4556-8BC0-76E93BE81B07}">
        EventGhost.PythonScript(u'\nimport os\nimport shutil\nimport subprocess\nimport threading\n\ndef do():\n    print \'This may take a while to run.\'\n    print \'Please be patient and do not close EG\'\n    print \'or any command prompt windows that open\'\n\n    def check_net():\n        command = \'PowerShell -ExecutionPolicy ByPass -Command "[environment]::Version"\'\n        proc = subprocess.Popen(command, stdout=subprocess.PIPE, stderr=subprocess.PIPE)\n        out, err = proc.communicate()\n\n        out = list(line for line in out.split(\'\\r\\n\') if line.strip())\n        err = list(line for line in err.split(\'\\r\\n\') if line.strip())\n\n        if err:\n            print \'\\n\'.join(err)\n            return None\n\n        return int(out[2].split(\' \', 1)[0])\n\n    print \'Checking .NET version attached to PowerShell\'\n    net_ver = check_net()\n    if net_ver is None:\n        print \'exiting backup\'\n        eg.globals.backup_thread = None\n        return\n    \n    if net_ver != 4:\n        ans = raw_input(\'PowerShell is using .NET version {0},\\nwe need it to be running version 4\\n\\nWould you like to upgrade it to .NET version 4? (y, n)\'.format(net_ver))\n        if ans.lower() == \'n\':\n            eg.globals.backup_thread = None\n            print \'exiting backup\'\n            return\n            \n        power_shell_cfg =[         \n            \'&lt;?xml version="1.0"?&gt;\',\n            \'&lt;configuration&gt;\',\n            \'    &lt;startup useLegacyV2RuntimeActivationPolicy="true"&gt;\',\n            \'        &lt;supportedRuntime version="v4.0.30319"/&gt;\',\n            \'        &lt;supportedRuntime version="v2.0.50727"/&gt;\',\n            \'    &lt;/startup&gt;\',\n            \'&lt;/configuration&gt;\'\n        ]\n        \n        power_shell_cfg = \'\\r\\n\'.join(power_shell_cfg)\n\n        ps_path = r\'C:\\Windows\\System32\\WindowsPowerShell\\v1.0\\PowerShell.Exe.Config\'\n        \n        if os.path.exists(ps_path):\n            os.rename(ps_path, ps_path + \'.backup\')    \n        \n        with open(ps_path, \'w\') as f:\n            f.write(power_shell_cfg) \n        \n        print \'Verifying that .NET version change was sucessfull\'\n        net_ver = check_net()\n        if net_ver is None:\n            print \'exiting backup\'\n            eg.globals.backup_thread = None\n            return\n    \n        if net_ver != 4:\n            print \'ERROR: unable to upgrade to .net version 4\'\n            print \'exiting backup\'\n            eg.globals.backup_thread = None\n            return\n        \n        print \'PowerShell .NET version change sucessful\'\n\n    else:\n        print \'PowerShell .NET version is 4\'\n\n    print \'Copying files\'\n    \n    eg.config.Save()\n\n    output_path = os.path.expandvars(r\'%userprofile%\\desktop\\eventghost_backup\')\n\n    if os.path.exists(output_path):\n        print \'temp file path already exists\', output_path\n        print \'exiting backup\'\n        eg.globals.backup_thread = None\n        return\n\n    os.mkdir(output_path)\n\n    app_dst = os.path.join(output_path, \'application\')\n    appdata_dst = os.path.join(output_path, \'appdata\')\n    programdata_dst = os.path.join(output_path, \'programdata\')\n\n    app_src = eg.mainDir\n    appdata_src = eg.configDir\n    programdata_src = os.path.abspath(os.path.join(eg.localPluginDir, \'..\'))\n\n    print app_src, \' --&gt; \', app_dst\n    shutil.copytree(app_src, app_dst)\n    print app_src, \' --&gt; \', app_dst, \'COMPLETE\'\n\n    print appdata_src, \' --&gt; \', appdata_dst\n    shutil.copytree(appdata_src, appdata_dst)\n    print appdata_src, \' --&gt; \', appdata_dst, \'COMPLETE\'\n\n\n    print programdata_src, \' --&gt; \', programdata_dst\n    shutil.copytree(programdata_src, programdata_dst)\n    print programdata_src, \' --&gt; \', programdata_dst, \'COMPLETE\'\n\n\n    if eg.config.autoloadFilePath:\n        dst = os.path.join(output_path, os.path.split(eg.config.autoloadFilePath)[-1])\n        print eg.config.autoloadFilePath, \' --&gt; \', dst\n        shutil.copyfile(eg.config.autoloadFilePath, dst)\n        print eg.config.autoloadFilePath, \' --&gt; \', dst, \'COMPLETE\'\n\n\n    print \'Compressing files\'\n\n    zip_file = os.path.abspath(\n        os.path.join(output_path, \'..\')\n    )\n\n    zip_file = os.path.join(zip_file, \'eg_backup.zip\')\n    \n    if os.path.exists(zip_file):\n        ans = raw_input(\'a backup file already exists ({0})\\n\\nWould you like to overwright the file (y/n)?\'.format(zip_file))\n        if ans.lower() == \'n\':\n            shutil.rmtree(output_path)\n            print \'exiting backup\'\n            eg.globals.backup_thread = None\n            return\n        \n        os.remove(zip_file)\n\n    print output_path, \' --&gt; \', zip_file\n\n    command = (\n        \'PowerShell \'\n        \'-ExecutionPolicy ByPass \'\n        \'-Command "\'\n        \'Add-Type -Path C:\\\\Windows\\\\Microsoft.NET\\\\assembly\\\\GAC_MSIL\\\\System.IO.Compression\\\\v4.0_4.0.0.0__b77a5c561934e089\\\\System.IO.Compression.dll;\\r\\n\'\n        \'Add-Type -Path C:\\\\Windows\\\\Microsoft.NET\\\\assembly\\\\GAC_MSIL\\\\System.IO.Compression.FileSystem\\\\v4.0_4.0.0.0__b77a5c561934e089\\\\System.IO.Compression.FileSystem.dll;\\r\\n\'\n        \'Add-Type -Path C:\\\\Windows\\\\Microsoft.NET\\\\assembly\\\\GAC_MSIL\\\\System.IO.Compression.ZipFile\\\\v4.0_4.0.0.0__b77a5c561934e089\\\\System.IO.Compression.ZipFile.dll;\\r\\n\'\n        "[System.IO.Compression.ZipFile]::CreateFromDirectory(\'{0}\', \'{1}\', [System.IO.Compression.CompressionLevel]::Optimal, $false);".format(output_path, zip_file)\n    )\n\n    proc = subprocess.Popen(command, stdout=subprocess.PIPE, stderr=subprocess.PIPE)\n    out, err = proc.communicate()\n\n    out = list(line for line in out.split(\'\\r\\n\') if line.strip())\n    err = list(line for line in err.split(\'\\r\\n\') if line.strip())\n\n    if err:\n        print \'\\n\'.join(err)\n        shutil.rmtree(output_path)\n        print output_path, \' --&gt; \', zip_file, \'FAILED\'\n        print \'FAILED\'\n\n    else:\n        print \'\\n\'.join(out)\n        shutil.rmtree(output_path)\n        print \'backup is located\', zip_file    \n        print \'FINISHED\'\n    \n    eg.globals.backup_thread = None\n\neg.globals.backup_thread = getattr(eg.globals, \'backup_thread\', None)\n\nif eg.globals.backup_thread is not None:\n    print \'backup already running\'\n    print \'exiting\'\n    eg.Exit()\n\neg.globals.backup_thread = threading.Thread(target=do)\neg.globals.backup_thread.start()\n\n')
    </Action>
</EventGhost>
If you like the work I have been doing then feel free to Image

loveleejohn
Experienced User
Posts: 133
Joined: Thu Dec 10, 2015 12:09 am

Re: EG Backup script

Post by loveleejohn » Wed Jan 29, 2020 9:20 pm

Thanks for this KG! Does it do the tree as well?

User avatar
kgschlosser
Site Admin
Posts: 5146
Joined: Fri Jun 05, 2015 5:43 am
Location: Rocky Mountains, Colorado USA

Re: EG Backup script

Post by kgschlosser » Thu Jan 30, 2020 1:34 am

I do not remember...
If you like the work I have been doing then feel free to Image

Post Reply