Skip to content

Instantly share code, notes, and snippets.

@stefanoverna
Last active March 13, 2023 05:22
Show Gist options
  • Save stefanoverna/e1389d3f7802792dff9aac770f27f0dd to your computer and use it in GitHub Desktop.
Save stefanoverna/e1389d3f7802792dff9aac770f27f0dd to your computer and use it in GitHub Desktop.
Simple backup of DatoCMS content
// 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())
)
})
})
})
}
// 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