Loop in a bash script with adb command [duplicate] - android

This question already has answers here:
Loop to find an app from a file and pull it with adb shell
(2 answers)
Closed 4 years ago.
I'm beginning with bash ans I'm trying to loop in a shell script to pull different apk's from my android device.
I've got a file with apk's paths
apkpaths.txt
/data/app/com.naver.linewebtoon-1/base.apk
/data/app/com.game5mobile.lineandwater-1/base.apk
I read this file and try to applicate this script but only the first apk is pulled but three times. How can I create a single file app.apk for each apk I pull with the adb command line ?
#!/bin/bash
filename="$1"
while IFS=: true; do
line=''
read -r line
if [ -z "$line" ]; then
break
fi
END=3
for i in $(seq 1 $END);
do adb shell cat $line > app$i.apk;
done
#echo "$line"
done < "$filename"
Thank you

#!/bin/bash
fileNum=1
while IFS='' read -r line || [[ -n "$line" ]]; do
adb shell cat $line > app$fileNum.apk
let "fileNum++"
done < "$1"

Related

shell script to get file from android sdcard with adb shell [duplicate]

This question already has answers here:
Copy a batch of files from Android to PC
(2 answers)
Closed 4 years ago.
#!/bin/bash
DIR_PATH="/sdcard/log/xxxxdir"
function useAdbReadLastFileFromDir(){
fileName=`adb shell ls $1|tail -1`
if [ -n $fileName ]
then
echo "fileName:"
echo "adb pull $PATH/$fileName"
fileContent=`adb pull $PATH/$fileName`
echo "fileContent:"$fileContent
else
echo "file not found exception"
fi
}
useAdbReadLastFileFromDir $DIR_PATH
android6.0 yotaphone/sunsang note5 can't pull file from sdcard, error message said:
file or dir not exist
but androi8.0 oppo findx/mi mix2 does.
help~!
adb pull takes 2 mandatory parameters: REMOTE_FILENAME and LOCAL_FILENAME
If you want to adb pull a text file to stdout - use adb shell cat FILENAME instead

adb shell one-liner not passing output properly [duplicate]

This question already has an answer here:
Send data back to the script which started the activity via adb shell am start
(1 answer)
Closed 4 years ago.
I am trying to write a script that will pull any photos or videos I've taken today. Here is what the code looks like:
for i in $(adb shell ls -l /sdcard/DCIM/Camera/ | grep $(date +%Y-%m-%d) | awk '{ print $7 }' ) ; do adb pull /sdcard/DCIM/Camera/$i ~/Photos ; done
And here is the error I get when I run it:
' does not existsdcard/DCIM/Camera/IMG_20160507_012827.jpg
It properly grabs the name the file(s) that need to be pulled, but for some reason it doesn't pass that information to "adb pull" properly.
Do I need to do something else to "sanitize" the output of one command into the input of the other?
why don't you just try find to list down all the files which are modified in last 24 hours and copy them to desired directory ?
find ~/desired/source -mtime -1 -type f -print0 | xargs -0 cp -t ~/destination/picture

capture android device screenshot with adb & perl [duplicate]

This question already has answers here:
Read binary stdout data like screencap data from adb shell?
(19 answers)
Closed 5 years ago.
I tried the one liner solution to capture screenshot via adb and the file is created successfully on PC but it is unreadable:
C:\Program Files\Android\android-sdk\platform-tools>adb shell screencap -p | perl -pe 's/\x0D\x0A/\x0A/g' > c:\users\utilisateur\desktop\android_screenshot1.png
The file seem to be correct but I think the Magic is not correct:
EDIT: The same issue happened with Git Bash for Windows.
adb exec-out screencap -p > screen.png
will save it on your machine directly
#Passella answer worked for me with a little tweaking:
adb shell screencap -p "/mnt/sdcard/output.png" && adb pull "/mnt/sdcard/output.png" "C:\output.png" && adb shell rm "/mnt/sdcard/output.png"
I had to quote the paths and replace | with &&.
Also, if you are using a Genymotion simulator, it's better to use its own adb:
C:\"Program Files"\Genymobile\Genymotion\tools\adb shell screencap -p "/mnt/sdcard/output.png" && C:\"Program Files"\Genymobile\Genymotion\tools\adb pull "/mnt/sdcard/output.png" "C:\output.png" && C:\"Program Files"\Genymobile\Genymotion\tools\adb shell rm "/mnt/sdcard/output.png"
Try this:
adb shell screencap -p /mnt/sdcard/output.png | adb pull /mnt/sdcard/output.png C:\output.png | adb shell rm /mnt/sdcard/output.png

Edit an app SharedPreferences [duplicate]

Attempting to pull a single file using
adb pull /data/data/com.corp.appName/files/myFile.txt myFile.txt
fails with
failed to copy '/data/data/com.corp.appName/files/myFile.txt myFile.txt' to 'myFile.txt': Permission denied
despite that USB debugging is enabled on the device.
We can go around the problem through the archaic route
adb shell
run-as com.corp.appName
cat files/myFile.txt > myFile.txt
but this is unwieldy for more than one file.
How can I pull the directory /data/data/com.corp.appName/files to my MacBook?
Doing this either directly or through a transit in `/storage/sdcard0/myDir (from where I can continue with Android File Transfer) is fine.
Additional Comment
It may be that just running
adb backup -f myFiles com.corp.appName
will generate the files I am looking for. In that case I am looking for a way to untar/unzip the resulting backup!
adb backup will write an Android-specific archive:
adb backup -f myAndroidBackup.ab com.corp.appName
This archive can be converted to tar format using:
dd if=myAndroidBackup.ab bs=4K iflag=skip_bytes skip=24 | openssl zlib -d > myAndroidBackup.tar
Reference:
http://nelenkov.blogspot.ca/2012/06/unpacking-android-backups.html
Search for "Update" at that link.
Alternatively, use Android backup extractor to extract files from the Android backup (.ab) file.
I had the same problem but solved it running following:
$ adb shell
$ run-as {app-package-name}
$ cd /data/data/{app-package-name}
$ chmod 777 {file}
$ cp {file} /mnt/sdcard/
After this you can run
$ adb pull /mnt/sdcard/{file}
Here is what worked for me:
adb -d shell "run-as com.example.test cat /data/data/com.example.test/databases/data.db" > data.db
I'm printing the database directly into local file.
On MacOSX, by combining the answers from Calaf and Ollie Ford, the following worked for me.
On the command line (be sure adb is in your path, mine was at ~/Library/Android/sdk/platform-tools/adb) and with your android device plugged in and in USB debugging mode, run:
adb backup -f backup com.mypackage.myapp
Your android device will ask you for permission to backup your data. Select "BACKUP MY DATA"
Wait a few moments.
The file backup will appear in the directory where you ran adb.
Now run:
dd if=backup bs=1 skip=24 | python -c "import zlib,sys;sys.stdout.write(zlib.decompress(sys.stdin.read()))" > backup.tar
Now you'll you have a backup.tar file you can untar like this:
tar xvf backup.tar
And see all the files stored by your application.
Newer versions of Android Studio include the Device File Explorer which I've found to be a handy GUI method of downloading files from my development Nexus 7.
You Must make sure you have enabled USB Debugging on the device
Click View > Tool Windows > Device File Explorer or click the Device File Explorer button in the tool window bar to open the Device File Explorer.
Select a device from the drop down list.
Interact with the device content in the file explorer window. Right-click on a file or directory to create a new file or directory, save the selected file or directory to your machine, upload, delete, or synchronize. Double-click a file to open it in Android Studio.
Android Studio saves files you open this way in a temporary directory outside of your project. If you make modifications to a file you opened using the Device File Explorer, and would like to save your changes back to the device, you must manually upload the modified version of the file to the device.
Full Documentation
You may use this shell script below. It is able to pull files from app cache as well, not like the adb backup tool:
#!/bin/sh
if [ -z "$1" ]; then
echo "Sorry script requires an argument for the file you want to pull."
exit 1
fi
adb shell "run-as com.corp.appName cat '/data/data/com.corp.appNamepp/$1' > '/sdcard/$1'"
adb pull "/sdcard/$1"
adb shell "rm '/sdcard/$1'"
Then you can use it like this:
./pull.sh files/myFile.txt
./pull.sh cache/someCachedData.txt
If you are using a Mac machine and a Samsung phone, this is what you have to do (since run-as doesn't work on Samsung and zlib doesn't work on Mac)
Take a backup of your app's data directory
adb backup -f /Users/username/Desktop/data.ab com.example
You will be asked for a password to encrypt in your Phone, don't enter any. Just tap on "Back up my data". See How to take BackUp?
Once successfully backed up, you will see data.ab file in your Desktop. Now we need to convert this to tar format.
Use Android Backup Extractor for this. Download | SourceCode
Download it and you will see abe.jar file. Add this to your PATH variable.
Execute this to generate the tar file: java -jar abe.jar unpack /Users/username/Desktop/data.ab /Users/username/Desktop/data.tar
Extract the data.tar file to access all the files
After setting the right permissions by adding the following code:
File myFile = ...;
myFile.setReadable(true, false); // readable, not only for the owner
adb pull works as desired.
see File.setReadable()
This answer is based on my experience with other answers, and comments in the answers. My hope is I can help someone in a similar situation.
I am doing this on OSX via terminal.
Previously Vinicius Avellar's answer worked great for me. I was only ever most of the time needing the database from the device from a debug application.
Today I had a use case where I needed multiple private files. I ended up with two solutions that worked good for this case.
Use the accepted answer along with Someone Somewhere's OSX specific comments. Create a backup and use the 3rd party solution,
sourceforge.net/projects/adbextractor/files/?source=navbar to unpack
into a tar. I'll write more about my experience with this solution at the bottom of this answer. Scroll down if
this is what you are looking for.
A faster solution which I settled with. I created a script for pulling multiple files similar to Tamas' answer. I am able to do it
this way because my app is a debug app and I have access to run-as on
my device. If you don't have access to run-as this method won't work
for you on OSX.
Here is my script for pulling multiple private files that I'll share with you, the reader, who is also investigating this awesome question ;) :
#!/bin/bash
#
# Strict mode: http://redsymbol.net/articles/unofficial-bash-strict-mode/
set -euo pipefail
IFS=$'\n\t'
#
# Usage: script -f fileToPull -p packageName
#
# This script is for pulling private files from an Android device
# using run-as. Note: not all devices have run-as access, and
# application must be a debug version for run-as to work.
#
# If run-as is deactivated on your device use one of the
# alternative methods here:
# http://stackoverflow.com/questions/15558353/how-can-one-pull-the-private-data-of-ones-own-android-app
#
# If you have encrypted backup files use:
# sourceforge.net/projects/adbextractor/files/?source=navbar
# From comments in the accepted answer in the above SO question
#
# If your files aren't encrypted use the accepted answer
# ( see comments and other answers for OSX compatibility )
#
# This script is open to expansions to allow selecting
# device used. Currently first selected device from
# adb shell will be used.
#Check we have one connected device
adb devices -l | grep -e 'device\b' > /dev/null
if [ $? -gt 0 ]; then
echo "No device connected to adb."
exit 1
fi
# Set filename or directory to pull from device
# Set package name we will run as
while getopts f:p: opt; do
case $opt in
f)
fileToPull=$OPTARG
;;
p)
packageName=$OPTARG
;;
esac
done;
# Block file arg from being blank
if [ -z "$fileToPull" ]; then
echo "Please specify file or folder to pull with -f argument"
exit 1
fi
# Block package name arg from being blank
if [ -z "$packageName" ]; then
echo "Please specify package name to run as when pulling file"
exit 1
fi
# Check package exists
adb shell pm list packages | grep "$packageName" > /dev/null
if [ $? -gt 0 ]; then
echo "Package name $packageName does not exist on device"
exit 1
fi
# Check file exists and has permission with run-as
fileCheck=`adb shell "run-as $packageName ls $fileToPull"`
if [[ $fileCheck =~ "Permission denied" ]] || [[ $fileCheck =~ "No such file or directory" ]]; then
echo "Error: $fileCheck"
echo "With file -> $fileToPull"
exit 1
fi
# Function to pull private file
#
# param 1 = package name
# param 2 = file to pull
# param 3 = output file
function pull_private_file () {
mkdir -p `dirname $3`
echo -e "\033[0;35m***" >&2
echo -e "\033[0;36m Coping file $2 -> $3" >&2
echo -e "\033[0;35m***\033[0m" >&2
adb shell "run-as $1 cat $2" > $3
}
# Check if a file is a directory
#
# param 1 = directory to check
function is_file_dir() {
adb shell "if [ -d \"$1\" ]; then echo TRUE; fi"
}
# Check if a file is a symbolic link
#
# param 1 = directory to check
function is_file_symlink() {
adb shell "if [ -L \"$1\" ]; then echo TRUE; fi"
}
# recursively pull files from device connected to adb
#
# param 1 = package name
# param 2 = file to pull
# param 3 = output file
function recurse_pull_private_files() {
is_dir=`is_file_dir "$2"`
is_symlink=`is_file_symlink "$2"`
if [ -n "$is_dir" ]; then
files=`adb shell "run-as $1 ls \"$2\""`
# Handle the case where directory is a symbolic link
if [ -n "$is_symlink" ]; then
correctPath=`adb shell "run-as $1 ls -l \"$2\"" | sed 's/.*-> //' | tr -d '\r'`
files=`adb shell "run-as $1 ls \"$correctPath\""`
fi
for i in $files; do
# Android adds nasty carriage return that screws with bash vars
# This removes it. Otherwise weird behavior happens
fileName=`echo "$i" | tr -d '\r'`
nextFile="$2/$fileName"
nextOutput="$3/$fileName"
recurse_pull_private_files "$1" "$nextFile" "$nextOutput"
done
else
pull_private_file "$1" "$2" "$3"
fi
}
recurse_pull_private_files "$packageName" "$fileToPull" "`basename "$fileToPull"`"
Gist:
https://gist.github.com/davethomas11/6c88f92c6221ffe6bc26de7335107dd4
Back to method 1, decrypting a backup using Android Backup Extractor
Here are the steps I took on my Mac, and issues I came across:
First I queued up a backup ( and set a password to encrypt my backup, my device required it ):
adb backup -f myAndroidBackup.ab com.corp.appName
Second I downloaded just abe.jar from here: https://sourceforge.net/projects/adbextractor/files/abe.jar/download
Next I ran:
java -jar ./abe.jar unpack myAndroidBackup.ab myAndroidBackup.tar
At this point I got an error message. Because my archive is encrypted, java gave me an error that I needed to install some security policy libraries.
So I went to http://www.oracle.com/technetwork/java/javase/downloads/jce8-download-2133166.html and downloaded the security policy jars I needed. Now in my case the install instructions told me the wrong location to put the jar files. It says that the proper location is <java-home>/lib/security. I put them there first and still got the error message. So I investigated and on my Mac with Java 1.8 the correct place to put them was: <java-home>/jre/lib/security. I made sure to backup the original policy jars, and put them there. Vola I was able to enter a password with abe.jar and decrypt to a tar file.
Lastly I just ran ( after running previous command again )
tar xvf myAndroidBackup.tar
Now it is important to note that if you can just run-as and cat, it is much much faster. One, you only get the files you want and not the entire application. Two, the more files ( + encryption for me ) makes it slower to transfer. So knowing to do this way is important if you don't have run-as on OSX, but the script should be first goto for a debug application.
Mind you I just wrote it today and tested it a few times, so please notify me of any bugs!
Similar to Tamas's answer, here is a one-liner for Mac OS X to fetch all of the files for app with your.app.id from your device and save them to (in this case) ~/Desktop/your.app.id:
(
id=your.app.id &&
dest=~/Desktop &&
adb shell "run-as $id cp -r /data/data/$id /sdcard" &&
adb -d pull "/sdcard/$id" "$dest" &&
if [ -n "$id" ]; then adb shell "rm -rf /sdcard/$id"; fi
)
Exclude the -d to pull from emulator
Doesn't stomp your session variables
You can paste the whole block into Terminal.app (or remove newlines if desired)
Starting form Dave Thomas script I've been able to write my own solution to overcome 2 problems:
my backup was containing only the manifest file
binary files got with Dave Thomas where unreadable
This is my script, that copies app data to sdcard and then pull it
#Check we have one connected device
adb devices -l | grep -e 'device\b' > /dev/null
if [ $? -gt 0 ]; then
echo "No device connected to adb."
exit 1
fi
# Set filename or directory to pull from device
# Set package name we will run as
while getopts f:p: opt; do
case $opt in
f)
fileToPull=$OPTARG
;;
p)
packageName=$OPTARG
;;
esac
done;
# Block package name arg from being blank
if [ -z "$packageName" ]; then
echo "Please specify package name to run as when pulling file"
exit 1
fi
# Check package exists
adb shell pm list packages | grep "$packageName" > /dev/null
if [ $? -gt 0 ]; then
echo "Package name $packageName does not exist on device"
exit 1
fi
adb shell "run-as $packageName cp -r /data/data/$packageName/ /sdcard/$packageName"
adb pull /sdcard/$packageName
adb shell rm -rf /sdcard/$packageName
Backed up Game data with apk. Nougat Oneplus 2.
**adb backup "-apk com.nekki.shadowfight" -f "c:\myapk\samsung2.ab"**
Does that mean that one could chmod the directory from world:--x to world:r-x long enough to be able to fetch the files?
Yes, exactly. Weirdly enough, you also need the file to have the x bit set. (at least on Android 2.3)
chmod 755 all the way down worked to copy a file (but you should revert permissions afterwards, if you plan to continue using the device).
you can do:
adb pull /storage/emulated/0/Android/data//

How can one pull the (private) data of one's own Android app?

Attempting to pull a single file using
adb pull /data/data/com.corp.appName/files/myFile.txt myFile.txt
fails with
failed to copy '/data/data/com.corp.appName/files/myFile.txt myFile.txt' to 'myFile.txt': Permission denied
despite that USB debugging is enabled on the device.
We can go around the problem through the archaic route
adb shell
run-as com.corp.appName
cat files/myFile.txt > myFile.txt
but this is unwieldy for more than one file.
How can I pull the directory /data/data/com.corp.appName/files to my MacBook?
Doing this either directly or through a transit in `/storage/sdcard0/myDir (from where I can continue with Android File Transfer) is fine.
Additional Comment
It may be that just running
adb backup -f myFiles com.corp.appName
will generate the files I am looking for. In that case I am looking for a way to untar/unzip the resulting backup!
adb backup will write an Android-specific archive:
adb backup -f myAndroidBackup.ab com.corp.appName
This archive can be converted to tar format using:
dd if=myAndroidBackup.ab bs=4K iflag=skip_bytes skip=24 | openssl zlib -d > myAndroidBackup.tar
Reference:
http://nelenkov.blogspot.ca/2012/06/unpacking-android-backups.html
Search for "Update" at that link.
Alternatively, use Android backup extractor to extract files from the Android backup (.ab) file.
I had the same problem but solved it running following:
$ adb shell
$ run-as {app-package-name}
$ cd /data/data/{app-package-name}
$ chmod 777 {file}
$ cp {file} /mnt/sdcard/
After this you can run
$ adb pull /mnt/sdcard/{file}
Here is what worked for me:
adb -d shell "run-as com.example.test cat /data/data/com.example.test/databases/data.db" > data.db
I'm printing the database directly into local file.
On MacOSX, by combining the answers from Calaf and Ollie Ford, the following worked for me.
On the command line (be sure adb is in your path, mine was at ~/Library/Android/sdk/platform-tools/adb) and with your android device plugged in and in USB debugging mode, run:
adb backup -f backup com.mypackage.myapp
Your android device will ask you for permission to backup your data. Select "BACKUP MY DATA"
Wait a few moments.
The file backup will appear in the directory where you ran adb.
Now run:
dd if=backup bs=1 skip=24 | python -c "import zlib,sys;sys.stdout.write(zlib.decompress(sys.stdin.read()))" > backup.tar
Now you'll you have a backup.tar file you can untar like this:
tar xvf backup.tar
And see all the files stored by your application.
Newer versions of Android Studio include the Device File Explorer which I've found to be a handy GUI method of downloading files from my development Nexus 7.
You Must make sure you have enabled USB Debugging on the device
Click View > Tool Windows > Device File Explorer or click the Device File Explorer button in the tool window bar to open the Device File Explorer.
Select a device from the drop down list.
Interact with the device content in the file explorer window. Right-click on a file or directory to create a new file or directory, save the selected file or directory to your machine, upload, delete, or synchronize. Double-click a file to open it in Android Studio.
Android Studio saves files you open this way in a temporary directory outside of your project. If you make modifications to a file you opened using the Device File Explorer, and would like to save your changes back to the device, you must manually upload the modified version of the file to the device.
Full Documentation
You may use this shell script below. It is able to pull files from app cache as well, not like the adb backup tool:
#!/bin/sh
if [ -z "$1" ]; then
echo "Sorry script requires an argument for the file you want to pull."
exit 1
fi
adb shell "run-as com.corp.appName cat '/data/data/com.corp.appNamepp/$1' > '/sdcard/$1'"
adb pull "/sdcard/$1"
adb shell "rm '/sdcard/$1'"
Then you can use it like this:
./pull.sh files/myFile.txt
./pull.sh cache/someCachedData.txt
If you are using a Mac machine and a Samsung phone, this is what you have to do (since run-as doesn't work on Samsung and zlib doesn't work on Mac)
Take a backup of your app's data directory
adb backup -f /Users/username/Desktop/data.ab com.example
You will be asked for a password to encrypt in your Phone, don't enter any. Just tap on "Back up my data". See How to take BackUp?
Once successfully backed up, you will see data.ab file in your Desktop. Now we need to convert this to tar format.
Use Android Backup Extractor for this. Download | SourceCode
Download it and you will see abe.jar file. Add this to your PATH variable.
Execute this to generate the tar file: java -jar abe.jar unpack /Users/username/Desktop/data.ab /Users/username/Desktop/data.tar
Extract the data.tar file to access all the files
After setting the right permissions by adding the following code:
File myFile = ...;
myFile.setReadable(true, false); // readable, not only for the owner
adb pull works as desired.
see File.setReadable()
This answer is based on my experience with other answers, and comments in the answers. My hope is I can help someone in a similar situation.
I am doing this on OSX via terminal.
Previously Vinicius Avellar's answer worked great for me. I was only ever most of the time needing the database from the device from a debug application.
Today I had a use case where I needed multiple private files. I ended up with two solutions that worked good for this case.
Use the accepted answer along with Someone Somewhere's OSX specific comments. Create a backup and use the 3rd party solution,
sourceforge.net/projects/adbextractor/files/?source=navbar to unpack
into a tar. I'll write more about my experience with this solution at the bottom of this answer. Scroll down if
this is what you are looking for.
A faster solution which I settled with. I created a script for pulling multiple files similar to Tamas' answer. I am able to do it
this way because my app is a debug app and I have access to run-as on
my device. If you don't have access to run-as this method won't work
for you on OSX.
Here is my script for pulling multiple private files that I'll share with you, the reader, who is also investigating this awesome question ;) :
#!/bin/bash
#
# Strict mode: http://redsymbol.net/articles/unofficial-bash-strict-mode/
set -euo pipefail
IFS=$'\n\t'
#
# Usage: script -f fileToPull -p packageName
#
# This script is for pulling private files from an Android device
# using run-as. Note: not all devices have run-as access, and
# application must be a debug version for run-as to work.
#
# If run-as is deactivated on your device use one of the
# alternative methods here:
# http://stackoverflow.com/questions/15558353/how-can-one-pull-the-private-data-of-ones-own-android-app
#
# If you have encrypted backup files use:
# sourceforge.net/projects/adbextractor/files/?source=navbar
# From comments in the accepted answer in the above SO question
#
# If your files aren't encrypted use the accepted answer
# ( see comments and other answers for OSX compatibility )
#
# This script is open to expansions to allow selecting
# device used. Currently first selected device from
# adb shell will be used.
#Check we have one connected device
adb devices -l | grep -e 'device\b' > /dev/null
if [ $? -gt 0 ]; then
echo "No device connected to adb."
exit 1
fi
# Set filename or directory to pull from device
# Set package name we will run as
while getopts f:p: opt; do
case $opt in
f)
fileToPull=$OPTARG
;;
p)
packageName=$OPTARG
;;
esac
done;
# Block file arg from being blank
if [ -z "$fileToPull" ]; then
echo "Please specify file or folder to pull with -f argument"
exit 1
fi
# Block package name arg from being blank
if [ -z "$packageName" ]; then
echo "Please specify package name to run as when pulling file"
exit 1
fi
# Check package exists
adb shell pm list packages | grep "$packageName" > /dev/null
if [ $? -gt 0 ]; then
echo "Package name $packageName does not exist on device"
exit 1
fi
# Check file exists and has permission with run-as
fileCheck=`adb shell "run-as $packageName ls $fileToPull"`
if [[ $fileCheck =~ "Permission denied" ]] || [[ $fileCheck =~ "No such file or directory" ]]; then
echo "Error: $fileCheck"
echo "With file -> $fileToPull"
exit 1
fi
# Function to pull private file
#
# param 1 = package name
# param 2 = file to pull
# param 3 = output file
function pull_private_file () {
mkdir -p `dirname $3`
echo -e "\033[0;35m***" >&2
echo -e "\033[0;36m Coping file $2 -> $3" >&2
echo -e "\033[0;35m***\033[0m" >&2
adb shell "run-as $1 cat $2" > $3
}
# Check if a file is a directory
#
# param 1 = directory to check
function is_file_dir() {
adb shell "if [ -d \"$1\" ]; then echo TRUE; fi"
}
# Check if a file is a symbolic link
#
# param 1 = directory to check
function is_file_symlink() {
adb shell "if [ -L \"$1\" ]; then echo TRUE; fi"
}
# recursively pull files from device connected to adb
#
# param 1 = package name
# param 2 = file to pull
# param 3 = output file
function recurse_pull_private_files() {
is_dir=`is_file_dir "$2"`
is_symlink=`is_file_symlink "$2"`
if [ -n "$is_dir" ]; then
files=`adb shell "run-as $1 ls \"$2\""`
# Handle the case where directory is a symbolic link
if [ -n "$is_symlink" ]; then
correctPath=`adb shell "run-as $1 ls -l \"$2\"" | sed 's/.*-> //' | tr -d '\r'`
files=`adb shell "run-as $1 ls \"$correctPath\""`
fi
for i in $files; do
# Android adds nasty carriage return that screws with bash vars
# This removes it. Otherwise weird behavior happens
fileName=`echo "$i" | tr -d '\r'`
nextFile="$2/$fileName"
nextOutput="$3/$fileName"
recurse_pull_private_files "$1" "$nextFile" "$nextOutput"
done
else
pull_private_file "$1" "$2" "$3"
fi
}
recurse_pull_private_files "$packageName" "$fileToPull" "`basename "$fileToPull"`"
Gist:
https://gist.github.com/davethomas11/6c88f92c6221ffe6bc26de7335107dd4
Back to method 1, decrypting a backup using Android Backup Extractor
Here are the steps I took on my Mac, and issues I came across:
First I queued up a backup ( and set a password to encrypt my backup, my device required it ):
adb backup -f myAndroidBackup.ab com.corp.appName
Second I downloaded just abe.jar from here: https://sourceforge.net/projects/adbextractor/files/abe.jar/download
Next I ran:
java -jar ./abe.jar unpack myAndroidBackup.ab myAndroidBackup.tar
At this point I got an error message. Because my archive is encrypted, java gave me an error that I needed to install some security policy libraries.
So I went to http://www.oracle.com/technetwork/java/javase/downloads/jce8-download-2133166.html and downloaded the security policy jars I needed. Now in my case the install instructions told me the wrong location to put the jar files. It says that the proper location is <java-home>/lib/security. I put them there first and still got the error message. So I investigated and on my Mac with Java 1.8 the correct place to put them was: <java-home>/jre/lib/security. I made sure to backup the original policy jars, and put them there. Vola I was able to enter a password with abe.jar and decrypt to a tar file.
Lastly I just ran ( after running previous command again )
tar xvf myAndroidBackup.tar
Now it is important to note that if you can just run-as and cat, it is much much faster. One, you only get the files you want and not the entire application. Two, the more files ( + encryption for me ) makes it slower to transfer. So knowing to do this way is important if you don't have run-as on OSX, but the script should be first goto for a debug application.
Mind you I just wrote it today and tested it a few times, so please notify me of any bugs!
Similar to Tamas's answer, here is a one-liner for Mac OS X to fetch all of the files for app with your.app.id from your device and save them to (in this case) ~/Desktop/your.app.id:
(
id=your.app.id &&
dest=~/Desktop &&
adb shell "run-as $id cp -r /data/data/$id /sdcard" &&
adb -d pull "/sdcard/$id" "$dest" &&
if [ -n "$id" ]; then adb shell "rm -rf /sdcard/$id"; fi
)
Exclude the -d to pull from emulator
Doesn't stomp your session variables
You can paste the whole block into Terminal.app (or remove newlines if desired)
Starting form Dave Thomas script I've been able to write my own solution to overcome 2 problems:
my backup was containing only the manifest file
binary files got with Dave Thomas where unreadable
This is my script, that copies app data to sdcard and then pull it
#Check we have one connected device
adb devices -l | grep -e 'device\b' > /dev/null
if [ $? -gt 0 ]; then
echo "No device connected to adb."
exit 1
fi
# Set filename or directory to pull from device
# Set package name we will run as
while getopts f:p: opt; do
case $opt in
f)
fileToPull=$OPTARG
;;
p)
packageName=$OPTARG
;;
esac
done;
# Block package name arg from being blank
if [ -z "$packageName" ]; then
echo "Please specify package name to run as when pulling file"
exit 1
fi
# Check package exists
adb shell pm list packages | grep "$packageName" > /dev/null
if [ $? -gt 0 ]; then
echo "Package name $packageName does not exist on device"
exit 1
fi
adb shell "run-as $packageName cp -r /data/data/$packageName/ /sdcard/$packageName"
adb pull /sdcard/$packageName
adb shell rm -rf /sdcard/$packageName
Backed up Game data with apk. Nougat Oneplus 2.
**adb backup "-apk com.nekki.shadowfight" -f "c:\myapk\samsung2.ab"**
Does that mean that one could chmod the directory from world:--x to world:r-x long enough to be able to fetch the files?
Yes, exactly. Weirdly enough, you also need the file to have the x bit set. (at least on Android 2.3)
chmod 755 all the way down worked to copy a file (but you should revert permissions afterwards, if you plan to continue using the device).
you can do:
adb pull /storage/emulated/0/Android/data//

Categories

Resources