Backing up and Restoring Data in Kintone

Kintone
10 min readJun 27, 2024

--

Although permissions capabilities in Kintone are strong, accidentally deleting data in Kintone is still possible. When deleting records, spaces and apps, users feel stressed by the loss of important information. Once records are deleted, they cannot be restored without a backup. That is why it is important to schedule the creation of backup stores of Kintone records.

This article covers restoration of deleted records, fields and attachments. Refer to these guides if an app or a space has been deleted.

The tool used by this tutorial is cli-kintone for macos. cli-kintone is a program used in the Mac’s Linux shell Terminal.

NOTE: When records are deleted, the record edit history and other record metadata cannot be restored. When restoring an app or space, the metadata is restored.

Agenda

  • Installing cli-kintone
  • Exporting Data with cli-kintone
  • Exporting Data on a Schedule with crontab
  • Exporting Attachment field Backup Data with cli-kintone
  • Restoring All Records
  • Restoring Some Records (Manual Restore)
  • Restoring a Deleted Field
  • Restoring a Deleted Attachment Field

Installing cli-kintone

Download the cli-kintone package from cli-kintone GitHub page.

Double click the downloaded file to extract the folder.

Extracting the downloaded cli-package will create the folder where backups are generated.

Open the folder, and right click the cli-kintone exe file. Click “Copy” to copy the exe file’s location (also known as the “path”).

In the Terminal, type the following command, and paste the file’s location between the single quotes (replacing the <exe-file-location> tag).

alias cli-kintone='<exe-file-location'

Now, cli-kintone can be called by typing the following command into the console.

cli-kintone
cli-kintone has been installed.

Exporting Data with cli-kintone

To export data, use the cli-kintone record export command. Provide the App ID, API Token of the app, the link to the subdomain (example, “https://mysubdomain.kintone.com”), and the folder path/location to save the data.

To save the backup data in a folder, “Copy” the folder in Finder. This will get the path/location of the folder.

Paste this to replace the <backup-folder-path> tag below.

The folder location contains the name to give the backup file (example, “backuplog.csv”).

For the following command, replace the tags (<>) with the subdomain, API Token, the App Id, and the backup folder path/location to save the file.

cli-kintone record export \ 
--base-url https://<mysubdomain>.kintone.com \
--api-token <api-token> \
--app <id> \
> <backup-folder-path>/mybackup.csv
Example of running the command in the terminal.

The folder location provided in the command will store the newly saved file of Kintone data.

The backup file of Kintone records was saved to the folder.

Similarly, csv data can be imported into the Kintone app by using cli-kintone record import. This will be explained in more detail later when discussing restoring Attachment field files.

Exporting Data on a Schedule with Crontab

To execute the cli-kintone export command automatically every week, crontab in the Terminal will be used.

In the terminal, run the following command to add nano as the editor for crontab.

export EDITOR=nano

Run the following commands to create a shell script to store the cli-kintone export function.

touch backuplogscript.sh

Edit the backup log script with the following command.

nano backuplogscript.sh

In Finder, “Copy” the folder that the backup data will be saved in. This will be the path/location for both the backup csv and the attachments backup, called <backup-folder-path> in the example.

In this example, the backup data will be saved in “cli-kintone-macos”

In the editor, paste the cli-kintone export command. Add the attachments-dir flag to export attachments and record data. Replace the <backup-folder-path> with the path/location.

Each log will be named with the date created.

current_date=$(date +"%Y-%m-%d")
cli-kintone record export \
--base-url https://<subdomain>.kintone.com \
--api-token <api-token> \
--app <id> \
> <backup-folder-path>/backuplog_$current_date.csv \
--attachments-dir <backup-folder-path>
The schedule script is created with nano text editor.

Save by pressing CTRL+O, exit the nano text editor by pressing CTRL+X.

Get the backup script’s location/path, by clicking “Copy” on the backup script in the Finder.

Paste the backup script’s location/path into the command below. This enhances permissions for the script.

chmod +x <backup-script-path>

It should look something like this example:

chmod +x /Users/pathtoscript/backupscript.sh

Now run the command to edit crontabs jobs.

crontab -e

In the editor, add the following line to the file. For example, this runs the command every Sunday at 2 AM.

0 2 * * 0 specifies the time schedule:

  • 0: minute (0th minute)
  • 2: hour (2 AM)
  • *: day of the month (any day)
  • *: month (any month)
  • 0: day of the week (0 represents Sunday)
0 2 * * 0 <backup-script-path>

It should look something like this example:

0 2 * * 0 /path/to/your/script.sh 
The line was added to the crontab file.

Save by pressing CTRL+O, exit the nano text editor by pressing CTRL+X.

The job is added to the scheduler.

To verify the cron job has been set up correctly, list your current cron jobs by running this command.

Accept the permissions alert.

Run the command to verify the script has been scheduled.

crontab -l
The shell script is listed in the scheduled jobs.

The dated backup data has been created successfully.

As time passes, your data backup will contain weekly logs of Kintone data from the app.

Exporting Attachment field backup data with cli-kintone

Records can store documents and files by using the Attachment fields. This data can also be backed up and restored if accidentally deleted.

Use the same record export command, but with the — — attachments-dir flag. Provide the folder location for where to store the files from Kintone Attachment fields.

cli-kintone record export \ 
--base-url https://<subdomain>.kintone.com \
--api-token <api-token> \
--app <id> \
> <backup-folder-path>/mybackup.csv \
--attachments-dir <backup-folder-path>
The command will export the Kintone record data to the existing folder with the name provided (example, “log10.csv”).

The files in the Attachment fields will be saved to separate folders for each record (example, “Attachment-13011” are the files from Kintone record number 13011.

Now that the Kintone data is backed up, it can be used to restore deleted record data.

Restoring All Records

This section will cover restoring all records and attachments when all records are deleted. The backup data created by cli-kintone record export command will be used.

Get the location/path of the backup csv by “copying” the file in Finder.

Right clicking the backup csv and clicking “Copy” get’s the file’s location/path

Use the following cli-kintone record import command. Replace the tags (<>) with the subdomain, the API Token, the App Id, and the file location/path of the backup csv. Replace the <backup-csv-path> tag by “Pasting” the backup csv’s path/location.

cli-kintone record import --base-url https://<subdomain>.kintone.com \
--api-token <api-token> \
--app <id> --file-path <backup-csv-path> \
--update-key Record_number

If the Attachment data must also be restored, add the attachment-dir flag. Replace the path/location of the Attachment backup.

In the Finder, right-click and “Copy” the folder that contains the Attachment backup data (in this example, “cli-kintone-macos” folder contains all the Attachment backup data).

Get the path/location of the Attachment backup folder.

Paste the path/location to replace the <attachments-backup-path> tag.

cli-kintone record import --base-url https://<subdomain>.kintone.com \
--api-token <api-token> \
--app <id> --file-path <backup-csv-path> \
--attachments-dir <attachments-backup-path> \
--update-key Record_number

It should look something like this example:

cli-kintone record import --base-url https://mysubdomain.kintone.com \
--api-token XUYgiuyXIUBiubXoiniyb \
--app 16 --file-path /Users/MyUser/Downloads/cli-kintone-macos/backuplog.csv \
--attachments-dir /Users/MyUser/Downloads/cli-kintone-macos/ \
--update-key Record_number

Restoring Some Records (Manual Restore)

In the case that some records are deleted, the records to restore must be identified in the backup data file. The records to restore must be imported back into Kintone via manual import or cli-kintone import.

Identify the deleted records by exporting all of the remaining data from the Kintone app. Make sure to export the Record number.

Open the backup file of records. Paste the backup record data beside the exported Kintone data.

Paste the backup record data beside the exported Kintone data.

To identify which records were deleted, highlight the Record number column from the exported data, and the Record Number column from the backup data.

Use Highlight Cell Rules > Duplicate Values.

The unhighlighted Record numbers are the records that were deleted.

Copy and paste these records into a new Excel sheet and import the file into Kintone.

Restoring Attachments Manually

There are cases where some records and not all records have been deleted, and need to be restored. In this case, the deleted records need to be identified and restored manually. First, the record data should be restored with the cli-kintone import function. Then, each record must have the file added back to the Attachment field.

When restoring attachments manually, it is helpful to rename the Record number column, and upload it into a new field called “Old Record Number.”

This way, the old record number can help with identifying and manually restoring attachments, which are named after the original Record number.

Create a field in the app in Kintone to store the original Record number.

Import the record data (with the old Record number) into the Kintone app.

Now to manually restore attachments, navigate to the backup folder of attachments.

Based on the folder name (the same as the old Record number), the following attachment test.png will be attached to the new record with the original record number (example, 18011).

In Kintone, filter for the “Old Record Number” (example, 18011).

Add the attachment to the record and save to restore.

The record data has been successfully restored.

Restoring a Deleted Field

If a field in Kintone is deleted, the field must be manually added back to the app. After the field is restored, the data in the field that was deleted can be restored using cli-kintone.

The data can also be restored by native import of the backup csv file into the app, with the key-to-bulk update field selected on the Record number.

The deleted field (example, “Last Name”) must be added back to the app, with the same field code. Refer to the backup csv and check the column names if the field code is not known, similar to the manual restore technique.

The cli-kintone record import command can be used.

cli-kintone record import --base-url https://<subdomain>.kintone.com \
--api-token <api-token> \
--app <id> --file-path <backup-csv-path> \
--update-key Record_number

The missing data will be restored to the existing records.

Restoring a Deleted Attachment Field

Like the previous step, if the Attachment field has been deleted, add back the Attachment field to the Kintone app. Make sure the new Attachment field code is the same as the original Attachment field.

Use the following cli-kintone command to restore the records with attachments. Provide the folders where the record data and attachment backup data are located.

cli-kintone record import --base-url https://<subdomain>.kintone.com \
--api-token <api-token> \
--app <id> --file-path <backup-csv-path> \
--attachments-dir <attachments-backup-path> \
--update-key Record_number

Attachment data has been restored.

Conclusion

This concludes the article about backup and restoring data in Kintone. It’s important to have systems in place to prevent information loss. For example, take steps to restrict delete permissions for all users except the Administrator. Pre-emptive steps are necessary for proper protection of your data.

Subscribe to Kintone Developer Program for more information on customization and enhancement of apps.

--

--

Kintone
Kintone

Written by Kintone

Love your data again with custom, easy-to-build business apps for your team. We talk about #CitizenDevelopers #DigitalTransformation and #CompanyCulture

No responses yet