How to backup Sanity Studio CMS content using the CLI.

4 replies
Last updated: Oct 2, 2023
Hi! Is there any way to backup the Sanity Studio CMS content uploaded?
AI Update

Yes, absolutely! Sanity provides several ways to backup your content, with the most common being the CLI export command.

Using the CLI to Export Your Dataset

The easiest way to backup your content is using the Sanity CLI:

sanity dataset export production

This creates a production.tar.gz file containing all your documents and assets. You can also specify a custom filename:

sanity dataset export production ./backups/backup-2024-01-15.tar.gz

What's Included in the Export

The export includes:

  • All your documents (posts, pages, etc.)
  • All assets (images, PDFs, files)
  • Hidden documents used by Studio and plugins

It does not include your schema definition (that lives in your Studio code) or access control settings.

Exporting Without Assets

If you only need the document data (not images/files), you can export to NDJSON format:

sanity dataset export production backup.ndjson --no-assets

Restoring from a Backup

To restore or import a backup:

sanity dataset import production.tar.gz production

This merges the backup content with your existing dataset. Always test imports on a development dataset first!

Automated Backup Strategies

Since Sanity doesn't store backups for you, consider setting up automated backups using:

  1. GitHub Actions - You can use this guide for automated Studio backups with GitHub Actions to schedule regular exports
  2. Scheduled scripts - Run the export command via cron jobs or CI/CD pipelines
  3. Cloud storage - Store exports in AWS S3, Google Cloud Storage, or similar services

Enterprise Backup Features

If you're on an Enterprise plan, Sanity offers managed backup features with:

  • Automatic backup scheduling
  • Retention policies
  • Server-side dataset cloning (Cloud Clone)
  • No need to download data locally

Best Practice Tip

Always export your dataset before running content migrations:

# Backup first
sanity dataset export production backup-before-migration.tar.gz

# Run your migration
sanity migration run your-migration

# Restore if needed
sanity dataset import backup-before-migration.tar.gz production

The CLI export/import workflow is reliable and gives you complete control over your content backups!

Can be done via the cli mate...
https://www.sanity.io/docs/dataset#fd38ca03b011
sanity dataset export production
Ok, so I can export the dataset including images? And other question, usually what do you use to store the export? Digitalocean?
Yep that'll give you your images too. Exporting in this manner will save the data to your local machine, you'd have to get a little creative if you wanted it to backup to a server.
ok! I understand! Thank you.

Sanity – Build the way you think, not the way your CMS thinks

Sanity is the developer-first content operating system that gives you complete control. Schema-as-code, GROQ queries, and real-time APIs mean no more workarounds or waiting for deployments. Free to start, scale as you grow.

Was this answer helpful?