Backup and Restore SQLite database (.db) using C# .NET
Links
GitHub Repository — https://github.com/joemoceri/database-toolkit
Intro
We’ll be using SQLite version 3.37.2 and .NET 6+. See the GitHub repository below for the latest with comments and more.
Make sure to download SQLite and the bundle of command-line tools. Store these files in a location on your computer and add them to your path. I’ve placed mine at C:\sqlite, yours should look similar the screenshot below.

We’re going to be using the sqlite3 command-line utility to back up and restore.
Backup
Let’s start with the C# .NET method
When using the localDatabasePath we need to make sure to use double \ instead of one and enclose them in double-quotes when passing to the sqlite-backup.bat file. We’re using the .NET Process class to execute the .bat file.
The final backup command will look like this (note the double slash on the parameter for the .backup command). Both parameters are the input to the method with a few adjustments handling the \ when inside sqlite3 and enclosing both in double-quotes to handle paths.
sqlite3 "C:\path\to\your\database\database.db" ".backup \"C:\\database.backup.db\""
We’re using the dot command .backup that comes with sqlite3. What we’re doing here is connecting to the database located at database.db, then issuing a .backup command telling it to store the backup at database.backup.db. This by default uses main when choosing which database to back up, so since we’re connecting to database.db, that’s what main becomes. You can see this if you open a command prompt and enter sqlite3 with database.db as your parameter, once in use the dot command .databases to list all the databases and the database you connected with will be listed as ‘main’.
Given a database file named test.db in the directory below, you’ll see test.db listed as main.

Afterward you’ll see your backup saved at the path you specified in localDatabasePath. We can use this backup to restore our database next.
Restore
In order to restore a database, we can start with the following C# .NET method
This method is very similar to backup, we’re just using a different .bat file, and handling the same problems.
The final restore command will look like this
sqlite3 "C:\path\to\your\database\database.db" ".restore \"C:\\database.backup.db\""
This command is very similar to backup, except this time we’re using the dot command .restore instead of .backup. Using the same logic as above this will connect to database.db, which will then list database.db as main, and when we restore we default to using main, which will use the database.backup.db file and overwrite it. One easy way to see this is to create a backup, modify some data with the original, then restore the backup.
Afterward, you’ll see the modified changes are gone and the original data restored.