Skip to content

Instantly share code, notes, and snippets.

@dongyuwei
Last active February 16, 2018 07:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dongyuwei/a0d62f926fad9de7476346c49422713b to your computer and use it in GitHub Desktop.
Save dongyuwei/a0d62f926fad9de7476346c49422713b to your computer and use it in GitHub Desktop.
This is a simple ruby script to backup react-native AsyncStorage in iOS Simulator. So we can backup and restore the whole redux store in AsyncStorage for development.
#!/usr/bin/env ruby
# backup workbench.app's AsyncStorage filesystem, so we can restore the whole redux store for development.
require "open3"
Open3.popen3("ps ax | grep workbench.app | awk '{print $5}' ") { |stdin, stdout, stderr|
process_name = stdout.readlines()[0]
base_dir = process_name.split('/data/')[0]
# Ref: node_modules/react-native/React/Modules/RCTAsyncLocalStorage.m
async_storage_dir = `find #{base_dir} -name "RCTAsyncLocalStorage_V1"`
timestamp = `date +"%Y%m%d_%H%M_%S"`
backup_dir = "~/workbench_async_storage_backup/#{timestamp}"
`mkdir -p #{backup_dir}`
`cp -Rfp #{async_storage_dir.strip()} #{backup_dir}`
puts "workbench.app's AsyncStorage have been backuped to #{backup_dir}"
puts "You can restore it to #{async_storage_dir}"
}
@dongyuwei
Copy link
Author

This script only works for iOS.
Please replace workbench.app with your target react-native app in iOS Simulator.

On iOS, AsyncStorage is backed by native code that stores small values in a serialized dictionary and larger values in separate files.
On Android, AsyncStorage will use either RocksDB or SQLite based on what is available.

Ref: https://facebook.github.io/react-native/docs/asyncstorage.html

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment