Back up Data with the Command Line Tool

Contents

Overview

This article introduces how to back up data from Kintone to another Kintone domain using cli-kintone (External link) .

The commands used in this article are explained in the Export Data with the Command Line Tool and Import Data with the Command Line Tool articles. It is recommended to read those article before this current article.

Initial Setup

This section introduces the required Kintone Apps and settings.

The example depicted in this article uses two domains: domain A and domain B. Data in domain A will be backed up to domain B.

Prepare the Sample App

This app is in domain A. Use the Project Management App created in the Download and Upload Attachments with the Command Line Tool article. In the below sections, this will be called the master app.

Prepare the Backup App

  1. Create a template from the master app by following the steps in the Creating a Template from an App and Registering the Template in Kintone (External link) help page.

  2. Create an app in domain B from the template.

  3. Change the app name to "Project Management Backup".

  4. Add a field that will store the master app's record number so that records can be synchronized between the two apps.

    Field Type Field Name Notes
    Number Record number of the master app On the field setting screen, configure the following settings
    • set the field code to master_record_number
    • check Prohibit duplicate values.
  5. Follow the steps in the Generating API Tokens (External link) article to generate API tokens for both apps. Make sure to check the following options, and take note of the API tokens, as it is needed in the later steps.

    • View records
    • Add records
    • Edit records
    • Delete records
information

To back up the following fields, check the Manage App option.

  • Created by
  • Updated by
  • Created datetime
  • Updated datetime

Initial Back up of Records

This section explains how to back up the records and attachments from the master app for the first time.

STEP 1: Export Records from the Master App

To export records and attachments, use the record export command. For example, the following command exports the data to "backup.csv". In addition, the attachments will be downloaded to the directory specified in the --attachments-dir option.

Example

1
cli-kintone.exe record export --app 125 --base-url https://example.kintone.com --api-token CNk3wfOufWtASDc6miMBTEa2SEm3ZokIcJPOSKJD > backup.csv --attachments-dir ./attachments

For more details, refer to the Export Data with the Command Line Tool and Download and Upload Attachments with the Command Line Tool articles.

The "attachments" folder and the backup.csv will be created in the same directory as the cli-kintone executable.

1
2
3
works # location of the cli-kintone executable
└─── attachments
└─── backup.csv

To export certain fields, use the --fields option and specify the field codes. For more details, refer to the Export Data with the Command Line Tool article.

information

To create a working directory for each app, it is recommended to update the environment variable PATH with the cli-kintone executable so that it can be used from any directory.
On Windows, use the following command to add cli-kintone to the environment variable PATH.

1
> setx PATH "%PATH%;<directory of the cli-kintone executable>"

STEP 2: Import Records to the Backup App

To import the exported data to the backup app, use the record import command.

  1. Manually edit the "backup.csv" file and change the Record_number in the first row to master_record_number. This column will be used to output the Record number values of the master app to the Master app record number field in the backup app.

  2. Use the following command to add records to the backup app.

    1
    
    cli-kintone.exe record import --app 200 --base-url https://backup.kintone.com --api-token CAk2sfOufGtFSDc2hfMBTEv14Em3ZokIcJGKFIEO --file-path ./backup.csv --attachments-dir ./attachments

Sync Record Data Manually

This section explains how to sync data after doing the backup steps above.
Use the record import command and specify the --update-key option as master_record_number.

  1. Export data using the same steps as Export Records from the Master App .

  2. Edit the exported CSV file and change the Record_number in the first row to master_record_number.

  3. Use the following command to import data to the backup app.

    1
    
    cli-kintone.exe record import --app 200 --base-url https://backup.kintone.com --api-token CAk2sfOufGtFSDc2hfMBTEv14Em3ZokIcJGKFIEO --file-path ./backup.csv --attachments-dir ./attachments --update-key "master_record_number"

Sync Record Data Automatically

To do this backup process automatically and regularly, create a shell script with the sync commands and automatically execute the scripts using cron or Launchd.

Below is an example of a shell script that syncs record data from the master app to the backup app. To edit the CSV file and change the field code, use the sed command.

1
2
3
4
5
6
#!/bin/bash
ATTACHMENTS_FILE_PATH=./attachments
CSV_FILE_PATH=./backup.csv

cli-kintone record export --app 125 --base-url https://example.kintone.com --api-token CNk3wfOufWtASDc6miMBTEa2SEm3ZokIcJPOSKJD --attachments-dir $ATTACHMENTS_FILE_PATH | sed "1 s/Record_number/master_record_number/" > $CSV_FILE_PATH
cli-kintone record import --app 200 --base-url https://backup.kintone.com --api-token CAk2sfOufGtFSDc2hfMBTEv14Em3ZokIcJGKFIEO --file-path $CSV_FILE_PATH --attachments-dir $ATTCHIMENTS_FILE_PATH -- update-key "Record_number"

Set up cron

Cron is a time-based job scheduler. If the OS is RHEL8, Ubuntu v20 or later, use the following commands to configure cron.

1
2
crontab -e # Open the crontab configuration file
systemctl start cron # start the cron

For example, to run job every day at midnight, the Crontab entry would look like as follows.

1
0 0 * * * ./scheduler.sh