Skip to content

Instantly share code, notes, and snippets.

@tuanchauict
Created October 3, 2020 01:55
Show Gist options
  • Save tuanchauict/c2926c92c0fa01086d6e0dde57c8b690 to your computer and use it in GitHub Desktop.
Save tuanchauict/c2926c92c0fa01086d6e0dde57c8b690 to your computer and use it in GitHub Desktop.
Back up your app debug data and restore for avoid loosing data while testing
#!/bin/bash -e
# List all backups
device_path="zzzBackup"
package=<package_of_the_app> # <--- replace this with your app
adb shell "run-as $package ls $device_path"
#!/bin/bash -e
# Backup data
package=<package_of_the_app> # <--- replace this with your app
device_path="zzzBackup" # under app dir.
backup_time=$(date +%F_%H-%M-%S)
backup_version=$(adb shell dumpsys package $package | grep versionName | sed -E "s/ versionName=//")
echo "Backup version:===$backup_version==="
separator="__version_"
backup_path="$device_path/$backup_time$separator$backup_version"
echo Backup to "$backup_path"
adb shell "run-as $package mkdir -p $backup_path"
adb shell "run-as $package cp -a shared_prefs $backup_path"
adb shell "run-as $package cp -a databases $backup_path"
# Add more folder if you want
#!/bin/bash -e
# Restore data
package=<package_of_the_app> # <--- replace this with your app
device_path="zzzBackup"
restore_path="$device_path/$1"
echo "Restore from $restore_path"
adb shell am force-stop $package
adb shell "run-as $package rm -rf shared_prefs" # Clean app's data
adb shell "run-as $package rm -rf databases" # Clean app's data
# Add more folder to clean if you want
adb shell "run-as $package cp -a $restore_path/shared_prefs ."
adb shell "run-as $package cp -a $restore_path/databases ."
# Add more folder to copy if you want
# adb shell am start "$package/YourMainActivity" # update this if you want to start the app
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment