This gist provides functions to import and export data from an IndexedDB database as JSON. It's based on Justin Emery's indexeddb-export-import
package, but applies some adjustments that reflect better on the current browser landscape (i.e. better developer ergonomics but no support for Internet Explorer).
For each of the provided functionalities, you need a connected IDBDatabase
instance.
import { idb } from 'some-database'
import { exportToJson } from 'idb-backup-and-restore.js'
exportToJson(idb)
.then(result => {
console.log('Exported JSON string:', result)
})
.catch(error => {
console.error('Something went wrong during export:', error)
})
import { idb } from 'some-database'
import { serializedData } from 'some-serialized-data'
import { importFromJson } from 'idb-backup-and-restore.js'
importFromJson(idb, serializedData)
.then(() => {
console.log('Successfully imported data')
})
.catch(error => {
console.error('Something went wrong during import:', error)
})
Depending on your use case, it can be reasonable to clear a database before importing serialized data:
import { idb } from 'some-database'
import { serializedData } from 'some-serialized-data'
import { importFromJson, clearDatabase } from 'idb-backup-and-restore.js'
clearDatabase(idb)
.then(() => importFromJson(idb, serializedData))
.then(() => {
console.log('Successfully cleared database and imported data')
})
.catch(error => {
console.error('Could not clear & import database:', error)
})
It seems like it, at least the error message is usually linked to that problem.
For these amounts of data you should probably back up your whole browser's user data. For Google Chrome, those are located at
%userdata%\AppData\Local\Google\Chrome\User Data
(Windows) respectively~/Library/Application Support/Google/Chrome
(macOS). Other browsers use very similar paths.You should be able to back up that whole folder and restore it in the same place on a new machine. I have definitely done this successfully before, but, again, you better verifiy it to work with another PC or in a VM. (If I remember correctly, there was also the restriction that the new device needed to have the same user/password credentials to be able to read the backed up data on Windows — but I'm not sure on that. That might only affect stored passwords.)