Kontent Backup Manager
The purpose of this project is to backup & restore Kentico Kontent projects. This project uses CM API to both get & restore data.
Installation
Install package globally:
npm i @kentico/kontent-backup-manager -g
Use via CLI
Configuration
Config | Value |
---|
projectId | Id of Kentico Kontent project (required) |
apiKey | Content management Api key (required) |
action | Action. Possible values are: restore & backup & clean (required) |
zipFilename | Name of zip used for export / restoring data. (e.g. 'kontent-backup'). |
enableLog | Indicates if default logging is enabled (useful to indicate progress) |
force | If enabled, project will we exported / restored even if there are data inconsistencies. Enabled by default. |
baseUrl | Custom base URL for Management API calls. |
enablePublish | Indicates if language variants published on the source project are also published on target. Enabled by default |
exportFilter | Can be used to export only selected data types. Expects CSV of types. For example contentType,language will cause backup manager to export only content types & language data. List of data types can be found below. |
Data types
- taxonomy
- contentType
- contentTypeSnippet
- contentItem
- languageVariant
- language
- assetFolder
- binaryFile
- workflowSteps (only export)
Execution
We recommend restoring backups to clean (empty) projects. Restoration process may make changes to target project such as changing language codenames to match source project.
To backup a project run:
kbm --action=backup --apiKey=xxx --projectId=xxx
To restore a project run:
kbm --action=restore --apiKey=xxx --projectId=xxx --zipFilename=backupFile
To clean (delete) everything inside a project run:
kbm --action=clean --apiKey=xxx --projectId=xxx
To get some help you can use:
kbm --help
Use with config file
Create a json
configuration file in the folder where you are attempting to run script. (e.g. backup-config.json
)
{
"projectId": "xxx",
"apiKey": "xxx",
"zipFilename": "backup",
"action": "backup",
"enableLog": true,
"force": true,
"baseUrl": null,
"exportFilter: null
}
To execute your action run:
kbm --config=backup-config.json
Use via code
Backup in code
import { ExportService, ImportService, ZipService } from "@kentico/kontent-backup-manager";
import { FileService } from '@kentico/kontent-backup-manager/dist/cjs/lib/node';
const run = async () => {
const exportService = new ExportService({
apiKey: 'sourceProjectApiKey',
projectId: 'sourceProjectId',
exportFilter: undefined,
onExport: item => {
console.log(`Exported: ${item.title} | ${item.type}`);
}
});
const data = await exportService.exportAllAsync();
const zipService = new ZipService({
context: 'node.js',
enableLog: true
});
const zipData = await zipService.createZipAsync(data);
const fileService = new FileService({
enableLog: true,
});
await fileService.writeFileAsync('backup', zipData);
};
run();
Restore in code
import { ExportService, ImportService, ZipService } from "@kentico/kontent-backup-manager";
import { FileService } from '@kentico/kontent-backup-manager/dist/cjs/lib/node';
const run = async () => {
const fileService = new FileService({
enableLog: true,
});
const zipFile = await fileService.loadFileAsync('backup');
const zipService = new ZipService({
context: 'node.js',
enableLog: true
});
const importService = new ImportService({
onImport: item => {
console.log(`Imported: ${item.title} | ${item.type}`);
},
canImport: {
asset: (item) => true,
contentType: (item) => {
if (item.codename === 'article') {
return true;
}
return false;
},
assetFolder: item => true,
contentItem: item => true,
contentTypeSnippet: item => true,
language: item => true,
languageVariant: item => true,
taxonomy: item => true,
},
enablePublish: true,
projectId: 'targetProjectId',
apiKey: 'targetProjectId',
enableLog: true,
fixLanguages: true,
workflowIdForImportedItems: '00000000-0000-0000-0000-000000000000'
});
const importData = await zipService.extractZipAsync(zipFile);
await importService.importFromSourceAsync(importData);
};
run();
Clean in code
const run = async () => {
const zipService = new ZipService({
filename: 'xxx',
enableLog: true,
context: 'node.js'
});
const importService = new ImportService({
onDelete: item => {
console.log(`Deleted: ${item.title} | ${item.type}`);
},
fixLanguages: true,
projectId: 'targetProjectId',
apiKey: 'targetProjectId',
enableLog: true
});
const data = await zipService.extractZipAsync();
await importService.importFromSourceAsync(data);
};
run();