android adb, retrieve database using run-as

adb shell and piping binary data

If you're unaware, running "adb shell" creates a pseudoterminal. This means that all LF characters are rewritten as CRLFs. If you're piping binary data back to your local machine, this will corrupt stdout.

For example, this results in a corrupted PNG file:
adb shell screencap -p > test.png

I was looking to fix this in adb, then noticed it was already done last year. There's an unadvertised exec service that bypasses the creation of the pseudoterminal. Invoke as follows:

adb exec-out screencap -p > test.png

Change was made by +Jeff Sharkey 

https://android.googlesource.com/platform/system/core/+/5d9d434efadf1c535c7fea634d5306e18c68ef1f
5d9d434efadf1c535c7fea634d5306e18c68ef1f - platform/system/core - Git at Google
Add "exec" service: shell commands with no pty. To facilitate device scripts that want to read/write binary data from the host side, this change introduces a new "exec" service that behaves like "shell" but without creating a pty, which would otherwise mangle binary data.
android.googlesource.com
156 plus ones




android adb, retrieve database using run-as

One a non-rooted android device, I can navigate to the data folder containing the database using the run-as command with my package name. Most files types I am content with just viewing, but with the database I would like to pull if from the android device.

Is there a download copy or move command from this part of adb shell? I would like to download the database file and view its content using a database browser.

One answer here involves turning entire application package into a compressed archive, but there is no further answer on how to extract that archive once this is done and moved to the machine, leaving me very sidetracked when there might be a more direct solution to begin with

thank you

share edit flag
 
 

6 Answers

up vote 92 down vote accepted
adb shell "run-as package.name chmod 666 /data/data/package.name/databases/file"
adb pull /data/data/package.name/databases/file .
adb shell "run-as package.name chmod 600 /data/data/package.name/databases/file"

UPDATE

the command for Android 5.0+ to save /data/data/package.name/databases/file would be:

adb exec-out run-as package.name cat databases/file > file
share edit flag
 
2
 
Or, just use run-as to copy the file to a spot on external storage, and pull the database file from there, rather than messing around with file permissions. –  CommonsWare  Jul 4 '14 at 11:02 
2
 
The run-as has no permission to write to the sdcard - there is something new added with the latest update... :( –  slott  Jan 13 '15 at 11:09
2
 
When I run adb shell "run-as package.name chmod 666 /data/data/package.name/databases/file" adb ends me error -->run-as: exec failed for chmod666 Error:Permission denied . I'm in Nexus 7 without root –  Shudy Jan 13 '15 at 12:02
 
Write access to external sd card is no longer possible with adb. –  slott  Jan 13 '15 at 13:40
1
 
When transferring my database using "adb exec-out run-as package.name cat databases/file > file", I'm getting extra line-breaks (CR LF), making the file unreadable by sqliteman. Looks like the file is being copied as text rather than binary. Is there any way to fix it? –  Gyum Fox  Sep 23 '15 at 13:36
 

The accepted answer doesn't work anymore for me (blocked by Android?)

So instead I did this:

> adb shell
shell $ run-as com.example.package
shell $ chmod 666 databases/file
shell $ exit                                               ## exit out of 'run-as'
shell $ cp /data/data/package.name/databases/file /sdcard/
shell $ run-as com.example.package
shell $ chmod 600 databases/file
> adb pull /sdcard/file .
share edit flag
 
1
 
This should be the new correct answer OR @Alex P. you should add this answer for newer Android versions. Also in my case I had to use 777 and apply it to the database-directory first/as-well before I could access the db file. –  muetzenflo  Mar 31 '15 at 9:10
1
 
My device is rooted, but I found I still need to run su to get the run-as command working. –  ThomasW Apr 9 '15 at 8:26
1
 
Hero!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! –  Bear  Aug 10 '15 at 13:30
 
Thanks working on 5.1! –  Muhammad Babar  Aug 14 '15 at 6:31
 
What if you don't have SD card? –  Gyum Fox  Sep 23 '15 at 12:19
 

I've published a simple shell script for dumping databases:

https://github.com/Pixplicity/humpty-dumpty-android

It performs two distinct methods described here:

  1. First, it tries to make the file accessible for other users, and attempting to pull it from the device.
  2. If that fails, it streams the contents of the file over the terminal to the local machine. It performs an additional trick to remove \r characters that some devices output to the shell.

From here you can use a variety of CLI or GUI SQLite applications, such as sqlite3 or sqlitebrowser, to browse the contents of the database.

share edit flag
 

Here's a solution that works on a device running Android 5.1. The following example is for Windows.

You need sed ([https://www.gnu.org/software/sed/manual/sed.html] or sed.exe on windows, e.g. from cygwin.) ( On Unix, it'll just be there ;) ). To remove bad '\r' characters, at least on windows.

Now just run the following command:

adb exec-out "run-as com.yourcompany.yourapp /data/data/com.yourcompany.yourapp/databases/YourDatabaseName" | c:\cygwin\bin\sed.exe 's/\x0D\x0A/\x0A/'>YourDatabaseName.db

The sed command strips out trailing /r characters.

Of course you should replace "com.yourcompany.yourapp" with the package name of the app and "YourDatabaseName" with the name of the database in the app.

share edit flag
 

For app's debug version, it's very convenient to use command adb exec-out run-as xxx.yyy.zzz cat somefile > somefile to extract a single file. But you have to do multiple times for multiple files. Here is a simple script I use to extract the directory.

#!/bin/bash
P=
F=
D=

function usage()
{
    echo "$(basename $0) [-f file] [-d directory] -p package"
    exit 1
}

while getopts ":p:f:d:" opt
do
    case $opt in
        p)
            P=$OPTARG
            echo package is $OPTARG
            ;;
        f)
            F=$OPTARG
            echo file is $OPTARG
            ;;
        d)
            D=$OPTARG
            echo directory is $OPTARG
            ;;
        \?)
            echo Unknown option -$OPTARG
            usage
            ;;
        \:)
            echo Required argument not found -$OPTARG
            usage
            ;;
    esac
done

[ x$P == x ] && {
    echo "package can not be empty"
    usage
    exit 1
}

[[ x$F == x  &&  x$D == x ]] && {
    echo "file or directory can not be empty"
    usage
    exit 1
}

function file_type()
{
    # use printf to avoid carriage return
    __t=$(adb shell run-as $P "sh -c \"[ -f $1 ] && printf f || printf d\"")
    echo $__t
}

function list_and_pull()
{
    t=$(file_type $1)
    if [ $t == d ]; then
        for f in $(adb shell run-as $P ls $1)
        do
            # the carriage return output from adb shell should
            # be removed
            mkdir -p $(echo -e $1 |sed $'s/\r//')
            list_and_pull $(echo -e $1/$f |sed $'s/\r//')
        done
    else
        echo pull file $1
        [ ! -e $(dirname $1) ] && mkdir -p $(dirname $1)
        $(adb exec-out run-as $P cat $1 > $1)
    fi
}

[ ! -z $D ] && list_and_pull $D
[ ! -z $F ] && list_and_pull $F

Hope it would be helpful. This script is also available at gist.

Typical usage is

$ ./exec_out.sh -p com.example.myapplication -d databases

then it will extract all files under your apps databases directory, which is /data/data/com.example.myapplication/databases, into current directory.

share edit flag
 

why dont you just view the database within the adb shell ?

in adb on most phones or on emulator call sqlite3

now run standard sql commands right on the prompt.

share edit flag
 
1
 
I like graphical representations of the table data, which requires me to load a file in. Maybe someone could script an sqlite browser to connect to android devices via adb though –  CQM  Mar 3 '14 at 17:47 
2
 
sqlite3 is not available on a regular device - only emulators and dev devices. –  slott  Jan 13 '15 at 13:40
 
©️2020 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页