Backup and restore Android’s settings quickly
Few posts ago (Programmatically change android settings) I mentioned a clever way of changing Android’s setting from a command line with the use of one of the system’s binary. This allowed for manipulation of phone’s configuration without even touching the screen. But as mentioned not only setting values is possible, but also reading them and if you read, you can backup.
Settings binary provides one more useful option, listing all values in particular group. To display a list of configuration values from a particular namespace, use the following:
adb shell settings list 'namespace' (one of the following: system, secure, global)
Here is a quick example:
$ adb shell settings list system accelerometer_rotation=1 alarm_alert=content://media/internal/audio/media/11 alarm_alert_set=1 dim_screen=1 dtmf_tone=1 dtmf_tone_type=0 ...
This looks really promising now. In order to differentiate particular categories, we can dump them into separate files (or use a prefix in a single file solution).
#! bin/bash mkdir -p ./backup adb shell settings list system > ./backup/settings_system adb shell settings list secure > ./backup/settings_secure adb shell settings list global > ./backup/settings_global
And that’s it. All available settings values have been backed up. It’s important to remember that not all device settings are being dumped. App data or WiFi configuration are only a few examples that will NOT be stored after performing this operation.
One we’ve got settings in a nice and simple format we can restore them onto a device with ‘put’ command or we could verify phone configuration with previous backups:
diff ./path/tp/old_backup ./path/to/new_backup
And here is a simple script to set (put) configurations from files back on the device:
#!/bin/bash for i in ./backup/*; do CAT=$(echo "$i" | cut -d "_" -f2 | tr -d " ") echo "Category: $CAT" if [ "$CAT" == "" ]; then continue fi for line in $(cat $i) do CHECK=$(echo "$line" | grep "=" | grep -v "0x" | grep -v "googlequicksearchbox" | grep -v "adb" | wc -l | tr -d " " | tr -d "\t\n") CHECK_END=$(echo "$line" | grep "=$" | wc -l | tr -d " " | tr -d "\t\n") if [ "$CHECK_END" -eq 1 ]; then echo "Skipping [!]: $line" continue fi KEY=$(echo "$line" | cut -d "=" -f1) VAL=$(echo "$line" | cut -d "=" -f2-) KEY_VALID=$(echo "$KEY" | tr -d " ") VAL_VALID=$(echo "$VAL" | tr -d " ") if [[ "$KEY_VALID" == "" || "$VAL_VALID" == "" || "$CHECK" -eq 0 ]]; then echo "Skipping [!]: $line" continue fi echo "Setting: $KEY, with value: $VAL" adb shell settings put $CAT $KEY $VAL done done
Always be careful when setting key=values pairs in settings as useful as it seems, it might as well force you into performing a factory reset, so:
- use the same device for restore as the one used for backup
- it is not recommended to perform restore even on the same device a backup was created if a firmware update was performed