Friday, August 10, 2012

Backup your KeePass 2 database before saving it

KeePass 2 has a trigger system which you can use to backup your database before saving it. Here's how. Open KeePass window, select Tools -> Triggers... Click Add... Type in the name (like "Backup database on save"). On Events tab, click Add... and choose Saving database file from the dropdown menu. You can skip Conditions tab. On Actions tab click Add... again. Leave Execute command line / URL selected.
File/URL: %comspec%

Arguments: /c mkdir "C:\Users\username\Backup"

Wait for exit: Yes
This trigger will try to create a backup folder. You can specify whatever location you want for this. Click Add...again.
File/URL: %comspec%

Arguments: /c copy "{DB_PATH}" "C:\Users\username\Backup\{DB_BASENAME}.kdbx_{DT_SIMPLE}"

Wait for exit: Yes
This trigger will copy your database file to the backup folder with a timestamp appended. Now to avoid having thousands of database copies in a long time we can specify a trigger which will delete oldest copies. The one below will leave 10 backup files and delete the rest. You can change it to whatever number you like (orange number). So, on Actions tab click Add... again.
File/URL: %comspec%

Arguments: /c for /f "skip=10 tokens=*" %X in ('dir "C:\Users\username\Backup\{DB_BASENAME}.*" /b /o:-d') do del "C:\Users\username\Backup\%X"
Thanks to KeePass website. You can find more useful examples there.

Labels: , , , ,

comments powered by Disqus