Skip to content
This repository has been archived by the owner on Feb 27, 2024. It is now read-only.

Commit

Permalink
feat: adds sample code & readme
Browse files Browse the repository at this point in the history
  • Loading branch information
Enngage committed Feb 28, 2020
1 parent 6d11c02 commit 3275b88
Show file tree
Hide file tree
Showing 5 changed files with 203 additions and 9 deletions.
139 changes: 131 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,143 @@ The purpose of this project is to backup & restore [Kentico Kontent](https://kon

Install package globally:

`npm i todo -g`
`npm i wip-backup-manager -g`

## Backup & restore with CLI
## Use via CLI

To backup a project use:
### Configuration

`kbm --projectId=xxx --action=backup`
| 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.zip'. |
| enableLog | Indicates if default logging is enabled (useful to indicate progress) |
| importLanguages | Indicates if languages should be imported |
| force | If enabled, project will we exported / restored even if there are data inconsistencies. This is enabled by default. |

To restore a project use:

`kbm --projectId=xxx --action=restore`
### Execution

## Backup & restore in code
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.zip`

To clean (delete) everything inside a project run:

`kbm --action=clean --apiKey=xxx --projectId=xxx`

### Use with config file

Create a `json` configuration file in the folder where you are attempting to run script. (e.g. `backup-config.json`)

```json
{
"projectId": "xxx",
"apiKey": "xxx",
"zipFilename": "backup",
"action": "backup",
"enableLog": true,
"importLanguages": false,
"force": true
}
```

To execute your action run:

`kbm --config=backup-config.json`

## Use via code

### Backup in code

```typescript
const run = async () => {
const exportService = new ExportService({
apiKey: 'sourceProjectApiKey',
projectId: 'sourceProjectId',
onExport: item => {
// called when any content is exported
console.log(`Exported: ${item.title} | ${item.type}`);
}
});

// data contains entire project content
const data = await exportService.exportAllAsync();

// you can also save backup in file with ZipService
const zipService = new ZipService({
filename: 'file',
enableLog: true
});

await zipService.createZipAsync(data);
};

run();
```

### Restore in code

```typescript
todo
const run = async () => {
const zipService = new ZipService({
filename: 'xxx',
enableLog: true
});

const importService = new ImportService({
onImport: item => {
// called when any content is imported
console.log(`Imported: ${item.title} | ${item.type}`);
},
projectId: 'targetProjectId',
apiKey: 'targetProjectId',
enableLog: true,
workflowIdForImportedItems: '00000000-0000-0000-0000-000000000000' // id that items are assigned
});

// read export data from zip
const data = await zipService.extractZipAsync();

// restore into target project
await importService.importFromSourceAsync(data);
};

run();
```

### Clean in code

```typescript
const run = async () => {
const zipService = new ZipService({
filename: 'xxx',
enableLog: true
});

const importService = new ImportService({
onImport: item => {
// called when any content is imported
console.log(`Imported: ${item.title} | ${item.type}`);
},
projectId: 'targetProjectId',
apiKey: 'targetProjectId',
enableLog: true,
workflowIdForImportedItems: '00000000-0000-0000-0000-000000000000' // id that items are assigned
});

// read export data from zip
const data = await zipService.extractZipAsync();

// restore into target project
await importService.importFromSourceAsync(data);
};

run();
```
2 changes: 1 addition & 1 deletion src/cli/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import yargs = require('yargs');

import { CleanService } from '../clean';
import { ICliFileConfig, fileHelper, CliAction } from '../core';
import { ExportService, IExportAllResult } from '../export';
import { ExportService } from '../export';
import { IImportSource, ImportService } from '../import';
import { ZipService } from '../zip';
import { ProjectContracts } from '@kentico/kontent-management';
Expand Down
16 changes: 16 additions & 0 deletions src/samples/clean-sample.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { CleanService } from 'src';

const run = async () => {
const cleanService = new CleanService({
onDelete: item => {
// called when any content is deleted
console.log(`Deleted: ${item.title} | ${item.type}`);
},
projectId: 'targetProjectId',
apiKey: 'targetProjectApiKey'
});

await cleanService.cleanAllAsync();
};

run();
27 changes: 27 additions & 0 deletions src/samples/export-sample.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { ZipService } from 'src/zip';

import { ExportService } from '../export';

const run = async () => {
const exportService = new ExportService({
apiKey: 'sourceProjectApiKey',
projectId: 'sourceProjectId',
onExport: item => {
// called when any content is exported
console.log(`Exported: ${item.title} | ${item.type}`);
}
});

// data contains entire project content
const data = await exportService.exportAllAsync();

// you can also save backup in file with ZipService
const zipService = new ZipService({
filename: 'file',
enableLog: true
});

await zipService.createZipAsync(data);
};

run();
28 changes: 28 additions & 0 deletions src/samples/restore-sample.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { ImportService } from 'src';
import { ZipService } from 'src/zip';

const run = async () => {
const zipService = new ZipService({
filename: 'xxx',
enableLog: true
});

const importService = new ImportService({
onImport: item => {
// called when any content is imported
console.log(`Imported: ${item.title} | ${item.type}`);
},
projectId: 'targetProjectId',
apiKey: 'targetProjectId',
enableLog: true,
workflowIdForImportedItems: '00000000-0000-0000-0000-000000000000' // id that items are assigned
});

// read export data from zip
const data = await zipService.extractZipAsync();

// restore into target project
await importService.importFromSourceAsync(data);
};

run();

0 comments on commit 3275b88

Please sign in to comment.