Jump to content

How-To: Backing up Evernote using PowerShell


jefito

Recommended Posts

  • Level 5*

For Valentine's day, I'm offering up a simple Windows PowerShell script that you can use to backup your Evenote notes. It uses the Evernote auxiliary program, ENScript.exe, to export Evernote notebooks to respective Evenote format (.ENEX) files.

The script is very dumb. You'll need to change the script to specify a backup directory location where the backups will land. You may need to change the script to specify the location of ENScript.exe.

The script will create a new directory in that location with a name that's based on the current date, and write the .ENEX files in that directory. There will be a .ENEX file for each notebook (which is what you want, because notebook name is not stored in a .ENEX file, so if you need to restore you can get the source notebook from the .ENEX filename).

Note: I am not a PowerShell expert, and while I've tested the script, it may not be particularly robust in the face of errors. In normal use it appears to work, but use it at your own risk. This script comes with a no-money-back guarantee. Oh, and feel free to modify it for your own use, or offer changes / suggestions / comments. I may or may not respond. You'll notice I have a list of Todos in the script. I may or may not deliver on those either.

You may need to change your execution policy to let this script operate properly. The command I used was: "Set-ExecutionPolicy RemoteSigned". In addition, you may want to make a Windows shortcut for the script. You'll need to search the web for that, but it can be done.

Take the following code and save it as a PowerShell script file Ii.e., with an extension ".PS1") somewhere that PowerShell can find it. I called mine "ENBackup.PS1". Creative, no?

# Simple Evernote backup PowerShell Script
#
# Backs up all notebooks in your account as .enex files, located in a designated
# directory. The backup directory name is generated by a base name with the
# current date (mm-dd-yy) appended.
#
# Todo:
# * Specify backup directory base via command-line
# * Backup single notebook specified via command-line
# * Restore notebook

# You may need to modify this according to the location of ENScript.exe
$ENscript = "C:\Program Files (x86)\Evernote\Evernote\ENScript.exe"

# Modify backup directory base as desired
$BackupLocation = "J:\Backups\Evernote\"

# Write out notebook names to a temporary file, one line at a time
$tempfile = "notebooks.txt"
& $ENscript listNotebooks >$tempfile
$reader = [System.IO.File]::OpenText($tempfile)

# Create a backup directory for this date
# Get current date (mm-dd-yy) and append it to the backup directory base
$date = get-date -uformat "%m-%d-%y"
$BackupDir = $BackupLocation + "Backup " + $date
# Make the actual backup directory
if ( ! (Test-Path $BackupDir) )
{
     md $BackupDir
}

# Process notebook names
try
{
    for( ; ; )
     {
          # fetch a notebook name
        $name = $reader.ReadLine()
        if ($name -eq $null) { break }
         
          # write out the backup file for this notebook: <notebook name>.enex
          "Backing up " + $name + "..."
          $BackupFileName = $BackupDir + "\" + $name + ".enex"
          & $ENscript exportNotes /q "notebook:$name" /f $BackupFileName
    }
     # Show your work
     dir $BackupDir
}
finally
{
    $reader.Close()
}
# delete the temp file
remove-item $tempfile

 

Have fun & Happy Valentine's Day for all of the Evernoters!!!

2/24/14 - Edit: Fixed ENScript exportNotes query. It was missing the 'notebook:' specifier. Also fixed problems with notebooks containing spaced in their names.

7/30/19: Edit: Script changed on to reflect more realistic location of ENScript.exe

  • Like 6
Link to comment
  • 2 weeks later...
  • Level 5*

For those of you who prefer Cherry Cokes, cars with fins, or that rich 8-bit sound, you can always go retro with a DOS batch file:
 
 

@echo offset enbackup=c:\Archive\Evernote\SimpleBackupset notelist=Notebooks.txtecho Backing up Evernote notebooks to %enbackupif not exist %enbackup% md %enbackup%del /q %enbackup%\*.enexenscript listNotebooks >%notelist%for /f "delims="  %a in ( Notebooks.txt) do (echo %a... & enscript exportnotes /q notebook:"%a" /f "%enbackup%\%a.enex")dir %enbackup%set enbackup=set notelist=

Edit: Warning! The above .bat file doesn't work under the standard CMD command shell. I tested this using the more capable TCC command shell, where it all works well. I'll try to update this for CMD when feasible.

Link to comment
  • Evernote Expert
For those unfamiliar with "codes" and "scripts" like me :(, you can use this alternative method.

 

Choose your Evernote Database folder, which can be seen from Tools-> Options -> Open Database folder.

 

Map the directory path to an automated, backup Software, and periodically run the software and back up your database to an external hard disk. :)
  • Like 2
Link to comment
  • Level 5*

 

For those unfamiliar with "codes" and "scripts" like me :(, you can use this alternative method.
 
Choose your Evernote Database folder, which can be seen from Tools-> Options -> Open Database folder.
 
Map the directory path to an automated, backup Software, and periodically run the software and back up your database to an external hard disk. :)

 

These methods were created expressly to help out people who don't want to monkey around with .exb files. The .enex file format is text, and XML-based, so may be more portable than binary .exb files, and more readily accessible to writing programs / scripts to process them. Aside from that, these methods will work even if Evernote is currently active, which, as far as I can tell doesn't work with copying the .exb file.

  • Like 1
Link to comment
  • 1 year later...

Doesn't work for any notebook that has a name containing any accentuated characters on my computer.

The ENScript listNotebooks commands is the problem, it replaces every accentuated character with a pair of characters!

Link to comment
  • 4 weeks later...
On 2/14/2014 at 8:54 PM, jefito said:

For Valentine's day, I'm offering up a simple Windows PowerShell script

Hi @jefito and thanks for your script. Could you please try and address JMD63's issue ? I have exactly the same problem.

When I try to backup my notebooks with your script, all those who have accentuated characters (ex : "é") are simply NOT saved, which is obviously a big problem.

The problem seems to come when fetching a notebook's name

$name = $reader.ReadLine()

  , because when that name is displayed

"Backing up " + $name + "..."

, it's already displayed incorrectly in the console. And ultimately the corresponding file is not saved to my hard drive. So if I have 12 notebooks but 2 of them have accents, I will end up with only 10 saved files.

Thanks in advance for your reply.

Link to comment
  • Level 5*
7 minutes ago, Jose de la Mancha said:

Hi @jefito and thanks for your script. Could you please try and address JMD63's issue ? I have exactly the same problem.

Yeah, sorry. I can replicate the problem, but haven't been able to fix it. Names with accents seem to come fine in the console *like the output of the ListNotebooks command, but when  redirected into a file,  they come out weird. Not really sure what's going on. Any PowerShell experts out there?

Link to comment

@jefitoFYI names with spaces aren't saved either apparently, which would be even a bigger issue. Have you noticed that ?

Your script doesn't report that as an error, which means that I didn't notice it at first.

I have posted the issues (accents, spaces) on a PowerShell board, and I will report back on any improvements.

PS : PinkElephant, manually renaming all the notebooks (by replacing all the accents and spaces) just to allow local backups is not a solution, and barely a workaround. Not very acceptable to me, sorry.

Link to comment

All : after a long discussion on two PowerShell boards (here's the shortest one, the other one is in french :

), everything seems to point indeed at the ENscript.exe windows executable. It looks like it has trouble handling accents and spaces in notebook names. People on these boards say they can't do anything, and it needs to be fixed on the ENscript.exe side.

@gbarryI have submitted a support ticket on Evernote's Twitter support channel here :

 Could you please make sure that somebody looks into it, and that somebody keeps me/us informed ? Thanks in advance.

Link to comment
  • Level 5*
Quote
 

@evernotehelps there seems to be a bug in the ENscript.exe Windows app. When used to access notebooks, it disregards all notebook names with accents or spaces. The issue has already been discussed here with no solution : https://discussion.evernote.com/topic/53536-how-to-backing-up-evernote-using-powershell/  . Please keep me informed. Thanks.

It is not true that ENScript disregards notebook names with embedded spaces or accented characters. The problem is that the ENScript arguments need to be quoted properly when passed on the command line. For example, see the following screen cap: In particular, the /q search argument needs to be quoted correctly to account for embedded spaces in any of the search terms. In this case, we want to export the notebook "Test Notebook", and in order to do this, we need to add quotes around its name,, and to do that correctly, they need to be escaped using a '\' character. Hence the search term" /q "notebook:\"Test Notebook\"". Also note that the notebook name with accents is handled just fine as is: /q notebook:Àççêñt.

image.png.4257e2b885746275a193fb30bdc9e4c3.png

 

As far as I can tell, there are two problems with my script. First, the escaping of names with embedded spaces is not correct. I haven't had time to work that out yet. Second, there's some kind of problem in the reading of the accented notebook name that I obtained by using the listNotebooks command and sending that into a text file. The name comes out garbled, and when used in the exportNotes command, doesn't match its notebook, so nothing is exported. I don't know the answer to that at this time.

 

 

Link to comment

I'd like to report that I've FINALLY managed to make it work, and backup ALL OF MY CURRENTLY 18 EVERNOTE NOTEBOOKS.

But to achieve this, I had to make big compromises on the script side. The loop part just wouldn't work because of the accents / special chars thing. Even seeking help from other boards, it was impossible to make it work. Don't know why.

So I just typed in the notebook names one by one, assigning them to various variables. But even THAT didn't work !

So ultimately I had to shorten 6 of the notebook names in the ENscript calls, using the "*". And then miraculously, it worked ! I have no idea why, because some of the names I had to shorten weren't even long or with special characters ("Appartements", "Brouillons"...). Anyway, all my 18 notebooks are now saved, instead of just 12. So I wanted to share the final script, so that you all know that there is at least one way to achieve the intended purpose, even with accents and special chars :

$ENscript = "C:\Program Files (x86)\Evernote\Evernote\ENScript.exe"
$BackupLocation = "D:\Backups\Evernote\"
$date = get-date -uformat "%Y-%m-%d"

$BackupDir = $BackupLocation + "Backup " + $date
if ( ! (Test-Path $BackupDir) ) { md $BackupDir }

$B01 = $BackupDir + "\Appartements.enex"
$B02 = $BackupDir + "\Brouillons.enex"
$B03 = $BackupDir + "\Carnet principal.enex"
$B04 = $BackupDir + "\Cuisine.enex"
$B05 = $BackupDir + "\Danse.enex"
$B06 = $BackupDir + "\Danse - Apprentissage.enex"
$B07 = $BackupDir + "\Danse - Compétitions.enex"
$B08 = $BackupDir + "\Danse - Polémiques.enex"
$B09 = $BackupDir + "\Decisions.enex"
$B10 = $BackupDir + "\Déménagement.enex"
$B11 = $BackupDir + "\DIVERS.enex"
$B12 = $BackupDir + "\Facebook.enex"
$B13 = $BackupDir + "\Hi-Fi & Hi-Tech.enex"
$B14 = $BackupDir + "\Informatique.enex"
$B15 = $BackupDir + "\Modèles de notes.enex"
$B16 = $BackupDir + "\Perso.enex"
$B17 = $BackupDir + "\Santé.enex"
$B18 = $BackupDir + "\Voyages.enex"

& $ENscript exportNotes /q "notebook: App*" /f $B01
& $ENscript exportNotes /q "notebook: Bro*" /f $B02
& $ENscript exportNotes /q "notebook: Car*" /f $B03
& $ENscript exportNotes /q "notebook: Cuisine" /f $B04
& $ENscript exportNotes /q "notebook: Danse" /f $B05
& $ENscript exportNotes /q "notebook: Danse - App*" /f $B06
& $ENscript exportNotes /q "notebook: Danse - Compétitions" /f $B07
& $ENscript exportNotes /q "notebook: Danse - Pol*" /f $B08
& $ENscript exportNotes /q "notebook: Dec*" /f $B09
& $ENscript exportNotes /q "notebook: Déménagement" /f $B10
& $ENscript exportNotes /q "notebook: DIVERS" /f $B11
& $ENscript exportNotes /q "notebook: Facebook" /f $B12
& $ENscript exportNotes /q "notebook: Hi-Fi & Hi-Tech" /f $B13
& $ENscript exportNotes /q "notebook: Informatique" /f $B14
& $ENscript exportNotes /q "notebook: Mod*" /f $B15
& $ENscript exportNotes /q "notebook: Perso" /f $B16
& $ENscript exportNotes /q "notebook: Santé" /f $B17
& $ENscript exportNotes /q "notebook: Voyages" /f $B18

Please note that variables 01, 02, 03, 06, 08 and 15 were the 6 where I absolutely had to put asterisks. All the other ones worked fine without them.

Link to comment
  • Level 5*

I'd check your results to make sure that you're getting what you think you're getting. I think you are using incorrect ENScript parameters, at least in the sample you've shown.

For example, there should be no space character between the "notebook:"  and the notebook name (e.g. "Voyages"). This is incorrect Evernote search language syntax. I'm guessing that the "notebook:" bit is being ignored, and your query is just a text search for "Voyages". Also, the notebook: term does not accept wildcards ('*'), and I think you're just getting a text search for, e.g. "App*".  I'd guess that the .enex files that you generate using wildcards are in general a lot larger you'd expect.

You should check out the Evernote search language reference: https://dev.evernote.com/doc/articles/search_grammar.php

Link to comment

@jefitoyou were right with both remarks. Which made me modify the script and end up with this :

$ENscript = "C:\Program Files (x86)\Evernote\Evernote\ENScript.exe"
$BackupLocation = "D:\Backups\Evernote\"
$date = get-date -uformat "%Y-%m-%d"

$BackupDir = $BackupLocation + "Backup " + $date
if ( ! (Test-Path $BackupDir) ) { md $BackupDir }

$B01 = $BackupDir + "\Appartements.enex"
$B02 = $BackupDir + "\Brouillons.enex"
$B03 = $BackupDir + "\Carnet principal.enex"
$B04 = $BackupDir + "\Cuisine.enex"
$B05 = $BackupDir + "\Danse.enex"
$B06 = $BackupDir + "\Danse - Apprentissage.enex"
$B07 = $BackupDir + "\Danse - Compétitions.enex"
$B08 = $BackupDir + "\Danse - Polémiques.enex"
$B09 = $BackupDir + "\Decisions.enex"
$B10 = $BackupDir + "\Déménagement.enex"
$B11 = $BackupDir + "\DIVERS.enex"
$B12 = $BackupDir + "\Facebook.enex"
$B13 = $BackupDir + "\Hi-Fi & Hi-Tech.enex"
$B14 = $BackupDir + "\Informatique.enex"
$B15 = $BackupDir + "\Modèles de notes.enex"
$B16 = $BackupDir + "\Perso.enex"
$B17 = $BackupDir + "\Santé.enex"
$B18 = $BackupDir + "\Voyages.enex"

& $ENscript exportNotes /q "notebook:Appartements" /f $B01
& $ENscript exportNotes /q "notebook:Brouillons" /f $B02
& $ENscript exportNotes /q "notebook:Carnet principal" /f $B03
& $ENscript exportNotes /q "notebook:Cuisine" /f $B04
& $ENscript exportNotes /q "notebook:Danse" /f $B05
& $ENscript exportNotes /q "notebook:Danse - Apprentissage" /f $B06
& $ENscript exportNotes /q "notebook:Danse - Compétitions" /f $B07
& $ENscript exportNotes /q "notebook:Danse - Polémiques" /f $B08
& $ENscript exportNotes /q "notebook:Decisions" /f $B09
& $ENscript exportNotes /q "notebook:Déménagement" /f $B10
& $ENscript exportNotes /q "notebook:DIVERS" /f $B11
& $ENscript exportNotes /q "notebook:Facebook" /f $B12
& $ENscript exportNotes /q "notebook:Hi-Fi & Hi-Tech" /f $B13
& $ENscript exportNotes /q "notebook:Informatique" /f $B14
& $ENscript exportNotes /q "notebook:Modèles de notes" /f $B15
& $ENscript exportNotes /q "notebook:Perso" /f $B16
& $ENscript exportNotes /q "notebook:Santé" /f $B17
& $ENscript exportNotes /q "notebook:Voyages" /f $B18

All the info is now saved (I have checked opening some .enex files with a text editor).

Unfortunately, only 15 notebooks out of 18 are saved. 03, 13, 15 are still not saved, and I don't know why. This is driving me crazy !

Link to comment

About notebook 03, if I change its name to "Carnetprincipal" it's saved. So the space seems to be a problem.

It would also explain why notebooks 13 and 15 aren't saved.

Which raises two questions :

1. Is there a way to solve it without having to change the notebook names ?
2. Why isn't it a problem in notebooks 06, 07, 08 that have spaces too ???

EDIT - notebooks 06, 07, 08 are saved incorrectly too. Probably because of the spaces.

So we've solved the accents problem (using a workaround), but not the spaces problem. Oh boy.

Link to comment
  • Level 5*
3 hours ago, Jose de la Mancha said:

About notebook 03, if I change its name to "Carnetprincipal" it's saved. So the space seems to be a problem.

Yes, that's the first problem I show in my little CMD session above. ENScript can deal with notebook names that have embedded spaces; you just need to get the inner quotes around the notebook name escaped properly in the notebook search query. From the command-line, e.g. "notebook:\"My Notebook\"", but I haven't figured out how to produce that in PowerShell yet.

But let's face it -- if you're going to do everything by hand, it might just be easier to make a good old .bat file and run with that. My PowerShell script was designed to query your note database, get the notebook names and back each one up. Unfortunately is has a couple of flaws that I haven't had time to sort out yet.

Link to comment

You are right @jefito. Why bother so much after all ? I have made this bat script :
 

chcp 1252 > nul

set $madate=%date:~-4%-%date:~3,2%-%date:~0,2%
md "D:\Backups\Evernote\Backup "%$madate%

"C:\Program Files (x86)\Evernote\ENScript.exe" exportNotes /q "notebook:Appartements" /f "D:\Backups\Evernote\Backup "%$madate%"\Appartements.enex"
"C:\Program Files (x86)\Evernote\ENScript.exe" exportNotes /q "notebook:Brouillons" /f "D:\Backups\Evernote\Backup "%$madate%"\Brouillons.enex"
"C:\Program Files (x86)\Evernote\ENScript.exe" exportNotes /q "notebook:\"Carnet principal\"" /f "D:\Backups\Evernote\Backup "%$madate%"\Carnet principal.enex"
"C:\Program Files (x86)\Evernote\ENScript.exe" exportNotes /q "notebook:Cuisine" /f "D:\Backups\Evernote\Backup "%$madate%"\Cuisine.enex"
"C:\Program Files (x86)\Evernote\ENScript.exe" exportNotes /q "notebook:Danse" /f "D:\Backups\Evernote\Backup "%$madate%"\Danse.enex"
"C:\Program Files (x86)\Evernote\ENScript.exe" exportNotes /q "notebook:\"Danse - Apprentissage\"" /f "D:\Backups\Evernote\Backup "%$madate%"\Danse - Apprentissage.enex"
"C:\Program Files (x86)\Evernote\ENScript.exe" exportNotes /q "notebook:\"Danse - Compétitions\"" /f "D:\Backups\Evernote\Backup "%$madate%"\Danse - Compétitions.enex"
"C:\Program Files (x86)\Evernote\ENScript.exe" exportNotes /q "notebook:\"Danse - Polémiques\"" /f "D:\Backups\Evernote\Backup "%$madate%"\Danse - Polémiques.enex"
"C:\Program Files (x86)\Evernote\ENScript.exe" exportNotes /q "notebook:Decisions" /f "D:\Backups\Evernote\Backup "%$madate%"\Decisions.enex"
"C:\Program Files (x86)\Evernote\ENScript.exe" exportNotes /q "notebook:Déménagement" /f "D:\Backups\Evernote\Backup "%$madate%"\Déménagement.enex"
"C:\Program Files (x86)\Evernote\ENScript.exe" exportNotes /q "notebook:DIVERS" /f "D:\Backups\Evernote\Backup "%$madate%"\DIVERS.enex"
"C:\Program Files (x86)\Evernote\ENScript.exe" exportNotes /q "notebook:Facebook" /f "D:\Backups\Evernote\Backup "%$madate%"\Facebook.enex"
"C:\Program Files (x86)\Evernote\ENScript.exe" exportNotes /q "notebook:\"Hi-Fi ^& Hi-Tech\"" /f "D:\Backups\Evernote\Backup "%$madate%"\Hi-Fi & Hi-Tech.enex"
"C:\Program Files (x86)\Evernote\ENScript.exe" exportNotes /q "notebook:Informatique" /f "D:\Backups\Evernote\Backup "%$madate%"\Informatique.enex"
"C:\Program Files (x86)\Evernote\ENScript.exe" exportNotes /q "notebook:\"Modèles de notes\"" /f "D:\Backups\Evernote\Backup "%$madate%"\Modèles de notes.enex"
"C:\Program Files (x86)\Evernote\ENScript.exe" exportNotes /q "notebook:Perso" /f "D:\Backups\Evernote\Backup "%$madate%"\Perso.enex"
"C:\Program Files (x86)\Evernote\ENScript.exe" exportNotes /q "notebook:Santé" /f "D:\Backups\Evernote\Backup "%$madate%"\Santé.enex"
"C:\Program Files (x86)\Evernote\ENScript.exe" exportNotes /q "notebook:Voyages" /f "D:\Backups\Evernote\Backup "%$madate%"\Voyages.enex"

... and it just WORKS !!!
I had of course to escape properly some notebook names. Two examples :

  • Carnet principal ---> "notebook:\"Carnet principal\""
  • Hi-Fi & Hi-Tech ---> "notebook:\"Hi-Fi ^& Hi-Tech\"" (please note the ^ before the &)

Problem solved for me I guess ? 🤨🤩🤪 (I'm not even sure - I can't believe that I've actually succeeded after all this time, lol)

Link to comment

As a bonus, I have :

  • Added the bat script as a task in the Task Scheduler, to be executed once a week
  • Added a line at the end of the bat script, to have it automatically delete all Backup folders older than 30 days :
forfiles -p "D:\Backups\Evernote" -m Backup* -d 30 -c "cmd /c IF @isdir == TRUE rd /S /Q @path"

 

Link to comment
  • 6 months later...
On 2/14/2014 at 9:54 PM, jefito said:

& $ENscript exportNotes /q "notebook:$name" /f $BackupFileName

 

For supporting space characters in the notebook names, I changed that line to:

"notebook:""$name""" | & $ENscript exportNotes /f "$BackupFileName"

Works for me. That was the only way I got the quoting right in Powershell...

Markku

Link to comment
  • 4 months later...
14 hours ago, dhanushx012 said:

Doesn't work for any notebook that has a name containing any accentuated characters on my computer.

Correct, even in 2020 the non-ASCII character handling seems so strange in Windows:

PS C:\Users\Markku> & 'C:\Program Files (x86)\Evernote\Evernote\ENScript.exe' listNotebooks
Inbox
...
Test-åäö
PS C:\Users\Markku> & 'C:\Program Files (x86)\Evernote\Evernote\ENScript.exe' listNotebooks >tempfile
PS C:\Users\Markku> type .\tempfile
Inbox
...
Test-åäö
PS C:\Users\Markku>

Someone with more PowerShell skills could try to improve that aspect of the code if possible.

Markku

Link to comment
  • 1 month later...

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...