Is there a way to backup/export & import Strapi data
Asked Answered
D

5

16

So I am at the place where I lost all of my Strapi data after moving from local to remote host and deploying my Strapi through herokuapp.com

I am using Strapi in my personal NEXT.JS project.

Luckily my Strapi database wasn't so huge and all content-types were kept so I was able to recreate the database quite quick. Also this was just a personal project.

I am wondering though, if I decide to push Strapi to use in in more professional context and in real project – how do I move from local development to deployment without loosing all data?

Is there a way to export everything before deployment and then import it into the deployed CMS or how does this work?

Also – what if I want to do it the other way around? I will keep working on my app using my Strapi on Heroku but at some point I will want to replicate the CMS locally. Where is the data stored and how do it get access to it?

Dormouse answered 23/5, 2021 at 21:38 Comment(5)
Have you solved this issue yet? I am also struggling with itBiforate
I'm using sqlite so I copy my ./tmp folder. As for recreating the admin folder $ npm build. If you have a non-flat database it should have it's own import/export tools.Slashing
I came across this topic today, and realizing some are pointing out this library (personally have not tried yet)Tomahawk
@TommyLeong to be honest what I did and how I managed to solve this issue is by setting up a mongo instance deployed on heroku and added all data manually again. Now this way the data lives in actual mongodb and is not stored locally… It was definitely a hassle but I was lucky to be working mainly on the front end with minimal dataset (in most cases just some placeholder data) so reproducing that didn’t take that much timeDormouse
Thanks for sharing your approach. Yea, I think in your case you're lucky because of minimal dataset. Probably not very suitable in the use of Production environmentTomahawk
M
5

You can do it via the CLI now, new from version 4.6. Strapi supports export, import, and transfer.

To create a tar of your data:

npm run strapi export --file my-strapi-export

To import data into your project:

npm run strapi import -f export_file.tar.gz

There are more options in the docs: https://docs.strapi.io/developer-docs/latest/developer-resources/data-management.html#export-data-using-the-cli-tool

With the new data export & import system, you can backup & restore your Strapi project data without acting manually and directly into the database.

More info: https://strapi.io/blog/announcing-strapi-v4.6

Montalvo answered 15/2, 2023 at 12:4 Comment(0)
O
1

From docs: "Strapi does not currently provide any tools for migrating or deploying your data changes between different environments (ie. from development to production). With the exception being the Content-Manager settings"

And there is no export/import content for now.

To export your data for example from the local environment to the production you have to handle:

  • content-types - Strapi store this stuff at files so version control will help
  • database data - you have to make database backup manually and then import data at the production server
  • static files - if you use Srapi to handle the static files you probably will have to copy them manually and import them to the production server or use version control for it (bad option). They are stored at app/public/uploads

I didn't tried this myself but it looks like a pretty tough task.

Conclusion: if it's OK for you to migrate only your content types, just put a git on your Strapi folder

Oxbow answered 15/3, 2022 at 23:0 Comment(0)
M
0

Keep in mind Heroku is in the middle of shutting down its free tier, so using another provider like railway.app or render.com might be a good idea.

Anyway: As Eugene already mentioned in his answer there are 3 types of data that might have to transmitted (content types, the actual database, and files).

After your first deployment to Heroku you should end up with all content types being there, but with an empty database and no files.

Following this guide you will create an own database while setting up your project where you can now either export and import your database from your local development environment (which you would have to do manually) or put in new data by hand. Sometimes this is even better since development environments tend to include a lot of "Lorem Ipsum" content for testing purposes.

Future deployments should not reset your database though but keep your data from that environment.

Finally there are the files which I would recommend to store on Cloudinary since it's free, and Strapi offers an easy-to-use plugin for it. Just create a free account on Cloudinary, install the plugin in your Strapi project, and set your ENV variables for your production environment within Heroku.

Midterm answered 15/9, 2022 at 23:57 Comment(0)
S
0

I found this

Apparently they recently did a plugin tutorial that had to do with this issue. There is a plugin called strapi-plugin-import-export-content on git hub that might be able to help your issue.

git hub link

Slowmoving answered 23/9, 2022 at 22:24 Comment(0)
F
0

I use this script

  1. Create AWS S3 bucket with IAM policy

set this ENV variables

/.env

BACKUP_AWS_ACCESS_KEY_ID="XXXX"
BACKUP_AWS_SECRET_ACCESS_KEY="XXXX"
BACKUP_AWS_REGION="us-east-1"
BACKUP_AWS_S3_BUCKET="XXXX-strapi-backup"
  1. Create this .js script and run it as cronjob

/backup_uploader.js

const AWS = require('aws-sdk');
const { execSync } = require('child_process');
const fs = require('fs');

// CONFIGS
const EXPORT_FILE_NAME = 'my-strapi-export';
const S3_BUCKET_NAME = process.env.BACKUP_AWS_S3_BUCKET;

// AWS CONFIG
AWS.config.update({
    accessKeyId: process.env.BACKUP_AWS_ACCESS_KEY_ID,
    secretAccessKey: process.env.BACKUP_AWS_SECRET_ACCESS_KEY,
    region: process.env.BACKUP_AWS_REGION
});

const s3 = new AWS.S3();

const uploadFileToS3 = () => {
    const fileContent = fs.readFileSync(EXPORT_FILE_NAME);

    // S3 FILENAME
    const currentDate = new Date().toISOString().split('T')[0];
    const s3Key = `${currentDate}.txt`;

    const params = {
        Bucket: S3_BUCKET_NAME,
        Key: s3Key,
        Body: fileContent
    };

    s3.upload(params, function(err, data) {
        if (err) {
            throw err;
        }
        console.log(`File uploaded successfully. ${data.Location}`);
    });
};


// TIMEOUT
const TIMEOUT_MS = 60 * 60 * 1000; // 1h
const timeout = setTimeout(() => {
  console.error('Process stopped after 1h.');
  process.exit(1); // Esce con un errore
}, TIMEOUT_MS);

// RUN STRAPI EXPORT
console.log("Exporting data using Strapi...");
execSync(`yarn strapi export --no-encrypt --no-compress --file ${EXPORT_FILE_NAME}`, { stdio: 'inherit' });

// UPLOAD TO S3
console.log("Uploading exported file to S3...");
uploadFileToS3();
Flunky answered 28/11, 2023 at 10:52 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.