4

I am trying to create a script to make an edit to the window's registry. As a fall back, I want to create a back up of the registry and save it in the working directory (or some other directory, but that is for later). Is there a way to use the power of python to backup the registry first?

So far the only way I have found to do this might be a call out to reg.exe, but I was looking for something more native to python itself.

Thanks!

0

3 Answers 3

2

The registry is a deeply Windows-centric construct, though I have not done any research on the subject, I would bet that there is no "native" way for backing up the registry in Python. I think you already have your answer and creating a process in Python and letting it run Reg Export is the best way to accomplish what you want.

However, if for some reason you don't want to run Reg.exe or invoke any external processes, I recommend that you write and save every registry entry before you edit it into a .reg file like this:

[HKEY_LOCAL_MACHINE\SOFTWARE\Adobe\Adobe Acrobat]
[HKEY_LOCAL_MACHINE\SOFTWARE\Adobe\Adobe Acrobat\9.0]
[HKEY_LOCAL_MACHINE\SOFTWARE\Adobe\Adobe Acrobat\9.0\Installer]
"AppInit_DLLs"="acaptuser64.dll"

This approach will ensure that you don't rely on any external utility and is the nearest thing to a "native" registry backup in Python.

Sign up to request clarification or add additional context in comments.

Comments

1

The recommended way to do this is to create a "restore point", which will make a backup to which you can restore the registry. I don't what the API to do this is, but I'm pretty sure it exists.

You can also do it manually, of course, but that is a different issue.

Comments

1

You can use the winreg module's SaveKey function if the program is UAC elevated:

import winreg, win32security, win32api # use _winreg for older versions of Python
with winreg.OpenKey(winreg.HKEY_CLASSES_ROOT, '') as handle: # Replace with the desired key
    win32security.AdjustTokenPrivileges(win32security.OpenProcessToken(win32api.GetCurrentProcess(), 40), 0, [(win32security.LookupPrivilegeValue(None, 'SeBackupPrivilege'), 2)]) # Basically, adjusts permissions for the interpreter to allow registry backups
    winreg.SaveKey(handle, 'C:\\REGBACKUP') # Replace with the desired file path

You can then load it for use with the winreg library:

import winreg
with winreg.OpenKey(winreg.HKEY_CLASSES_ROOT, '', 'C:\\REGBACKUP') as handle:
    ...

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.