Back up Data with the Command Line Tool
Overview
This article introduces how to back up data from Kintone to another Kintone domain using cli-kintone .
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
-
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 help page.
-
Create an app in domain B from the template.
-
Change the app name to "Project Management Backup".
-
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.
-
Follow the steps in the Generating API Tokens 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
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
|
|
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.
|
|
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.
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
.
|
|
STEP 2: Import Records to the Backup App
To import the exported data to the backup app, use the record import
command.
-
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.
-
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.
-
Export data using the same steps as Export Records from the Master App .
-
Edit the exported CSV file and change the Record_number in the first row to master_record_number.
-
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.
|
|
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.
|
|
For example, to run job every day at midnight, the Crontab entry would look like as follows.
|
|