Last active
March 13, 2023 05:22
-
-
Save stefanoverna/e1389d3f7802792dff9aac770f27f0dd to your computer and use it in GitHub Desktop.
Simple backup of DatoCMS content
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// First option: | |
// This will dump every content of a DatoCMS space in the "backup" directory | |
// To be used with the `dato dump` command (see https://docs.datocms.com/other/basic-usage.html) | |
module.exports = (dato, root, i18n) => { | |
i18n.availableLocales.forEach(locale => { | |
root.directory(`backup/${locale}`, localeDir => { | |
dato.itemTypes.forEach(itemType => { | |
localeDir.createDataFile( | |
`${itemType.apiKey}.json`, | |
`json`, | |
dato.itemsOfType(itemType).map(item => item.toMap()) | |
) | |
}) | |
}) | |
}) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Second option | |
// Just use our API client to fetch all the records and save them in a JSON file | |
// To run it, just | |
// node dump.js | |
const SiteClient = require('datocms-client').SiteClient; | |
const fs = require('fs'); | |
const client = new SiteClient('YOUR_API_TOKEN'); | |
client.items.all() | |
.then(response => { | |
fs.writeFileSync('dump.json', JSON.stringify(response, null, 2)); | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment