I'm trying to write an android app that backs up data from selected directory of an android device to a remote host using rsync and ssh. When I run the rsync command from adb shell as follows, it works:
rsync -rvz -e "/system/xbin/ssh -y -p 22" "/mnt/sdcard/" "rajeesh#10.0.2.2:backup/"
But my java code using Runtime.exec fails with an error that says:
Error: rsync: failed to exec /system/xbin/ssh -y -p 22: No such file or directory (2)
The code I used is as follows:
String[] commands = {
"rsync", "-rvz", "-e", "\"/system/xbin/ssh -y -p 22\"",
"\"/mnt/sdcard/\"", "\"rajeesh#10.0.2.2:backup/\""
};
Process process = Runtime.getRuntime().exec(commands);
Both rsync and ssh have been placed at /system/xbin and chmoded to 755. Tried replacing "rsync" with "/system/xbin/rsync" also but the issue remains. What would be the issue here?
Found out the issue myself.
When run directly from the shell, quotes have specific meaning and they are required here as follows:
rsync -rvz -e "ssh -y -p 22" "/path/to/src/" "/path/to/dest"
My java snippet above was trying to run the command with the quotes escaped, like "\"arg\"". But quotes not required when used outside shell. The correct usage is:
String[] commands = {
"/system/xbin/rsync", "-rvz", "-e", "/system/xbin/ssh -y -p 22",
"/mnt/sdcard", "rajeesh#10.0.2.2:backup/"
};
I'm not sure but maybe problem is that the destination path (rajeesh#10.0.2.2:backup/) is not absolute?
Also if you what to sync your files in the same device, maybe you should try to not use ssh? And do something like that:
rsync -rvz /mnt/sdcard/some_directory /backup
Related
Im going to use 7za library
now i try to use 7za to extract files and add files from 7z
My problem is that every time i execute nothing happen
Here is my 7z.sh code
and also i set permission and its on /data/local/d
#!/system/bin/sh
export LD_PRELOAD=
umask 000
cd "$1"
shift
/data/local/d/7za "$#"
and in terminal i execute using
su
cd /data/local/d/
sh 7z.sh x -tzip d.zip
And i getting error
Incorrect command line and x : no such file directory
Help me thanks for solution
Story
I take photos and record videos with my phone camera and keep all of them on my internal storage/sdcard. I periodically back them up on my PC, so I keep these camera photos on PC storage in sync with phone storage.
For years, I've been backing up my phone camera photos to my PC in the following way:
Plug in phone into PC and allow access to phone data
Browse phone storage → DCIM → Camera
Wait several minutes for the system to load a list of ALL photos
Copy only several latest photos which haven't been backed up yet
I figured that waiting several minutes for all photos to load is an unnecessary drag so I downloaded adb platform tools. I've added the folder bin to my Path environment variable (i.e. %USERPROFILE%\Tools\adb-platform-tools_r28.0.3) so that I can seamlessly use adb and not write its full path each time.
The script
I wrote the following script for Git Bash for Windows. It is also compatible with Unix if you change the $userprofile variable. Essentially, the script pulls camera photos between two dates from phone storage to PC.
# Attach device and start deamon process
adb devices
# Initialize needed variables
userprofile=$(echo "$USERPROFILE" | tr "\\" "/") # Windows adjustments
srcFolder="//storage/06CB-C9CE/DCIM/Camera" # Remote folder
dstFolder="$userprofile/Desktop/CameraPhotos" # Local folder
lsFile="$dstFolder/camera-ls.txt"
filenameRegex="2019061[5-9]_.*" # Date from 20190615 to 20190619
# Create dst folder if it doesn't exist
mkdir -p "$dstFolder"
# 1. List contents from src folder
# 2. Filter out file names matching regex
# 3. Write these file names line by line into a ls file
adb shell ls "$srcFolder" | grep -E "$filenameRegex" > "$lsFile"
# Pull files listed in ls file from src to dst folder
while read filename; do
if [ -z "$filename" ]; then continue; fi
adb pull "$srcFolder/$filename" "$dstFolder" # adb: error: ...
done < "$lsFile"
# Clean up
rm "$lsFile"
# Inform the user
echo "Done pulling files to $dstFolder"
The problem
When I run the script (bash adb-pull-camera-photos.sh), everything runs smoothly except for the adb pull command in the while-loop. It gives the following error:
': No such file or directoryemote object '//storage/06CB-C9CE/DCIM/Camera/20190618_124656.jpg
': No such file or directoryemote object '//storage/06CB-C9CE/DCIM/Camera/20190618_204522.jpg
': No such file or directoryemote object '//storage/06CB-C9CE/DCIM/Camera/20190619_225739.jpg
I am not sure why the output is broken. Sometimes when I resize the Git Bash window some of the text goes haywire. This is the actual error text:
adb: error: failed to stat remote object '//storage/06CB-C9CE/DCIM/Camera/20190618_124656.jpg': No such file or directory
adb: error: failed to stat remote object '//storage/06CB-C9CE/DCIM/Camera/20190618_204522.jpg': No such file or directory
adb: error: failed to stat remote object '//storage/06CB-C9CE/DCIM/Camera/20190619_225739.jpg': No such file or directory
I am sure that these files exist in the specified directory on the phone. When I manually execute the failing command in bash, it succeeds with the following output:
$ adb pull "//storage/06CB-C9CE/DCIM/Camera/20190618_124656.jpg" "C:/Users/User/Desktop/CameraPhotos/"
//storage/06CB-C9CE/DCIM/Camera/20190618_124656.jpg: 1 file pulled. 15.4 MB/s (1854453 bytes in 0.115s)
The question
I can't figure out what's wrong with the script. I thought the Windows system might be causing a commotion, because I don't see the reason why the same code works when entered manually, but doesn't work when run in a script. How do I fix this error?
Additional info
Note that I had to use // in the beginning of an absolute path on Windows because Git Bash would interpret / as its own root directory (C:\Program Files\Git).
I've echoed all variables inside the script and got all the correct paths that otherwise work via manual method.
camera-ls.txt file contents
20190618_124656.jpg
20190618_204522.jpg
20190619_225739.jpg
Additional questions
Is it possible to navigate to external sdcard without using its name? I had to use /storage/06CB-C9CE/ because /sdcard/ navigates to internal storage.
Why does tr "\\" "/" give me this error: tr: warning: an unescaped backslash at end of string is not portable?
Windows batch script
Here's a .bat script that can be run by Windows Command Prompt or Windows PowerShell. No Git Bash required.
:: Start deamon of the device attached
adb devices
:: Pull camera files starting from date
set srcFolder=/storage/06CB-C9CE/DCIM/Camera
set dstFolder=%USERPROFILE%\Desktop\CameraPhotos
set lsFile=%USERPROFILE%\Desktop\CameraPhotos\camera-ls.txt
set dateRegex=2019061[5-9]_.*
mkdir %dstFolder%
adb shell ls %srcFolder% | adb shell grep %dateRegex% > %lsFile%
for /F "tokens=*" %%A in (%lsFile%) do adb pull %srcFolder%/%%A %dstFolder%
del %lsFile%
echo Done pulling files to %dstFolder%
Just edit the srcFolder to point to your phone camera folder,
plug a pattern into the dateRegex for matching the date interval and
save it as a file with .bat extension, i.e: adb-pull-camera-photos.bat.
Double-click the file and it will pull filtered photos into CameraPhotos folder on Desktop.
Keep in mind that you still need have adb for Windows on your PC.
The problem was with Windows line delimiters.
Easy fix
Just add the IFS=$'\r\n' above the loop so that the read command knows the actual line delimiter.
IFS=$'\r\n'
while read filename; do
if [ -z "$filename" ]; then continue; fi
adb pull "$srcFolder/$filename" "$dstFolder"
done < "$lsFile"
Explanation
I tried plugging the whole while-loop into the console and it failed with the same error:
$ bash adb-pull-camera-photos.sh
List of devices attached
9889db343047534336 device
tr: warning: an unescaped backslash at end of string is not portable
': No such file or directoryemote object '//storage/06CB-C9CE/DCIM/Camera/20190618_124656.jpg
': No such file or directoryemote object '//storage/06CB-C9CE/DCIM/Camera/20190618_204522.jpg
': No such file or directoryemote object '//storage/06CB-C9CE/DCIM/Camera/20190619_225739.jpg
Done pulling files to C:/Users/User/Desktop/CameraPhotos
This time I started investigating why the output was broken. I remembered that windows uses \r\n as newline, which means Carriage Return + Line Feed, (CR+LF), so some text must have been overwritten.
It was because of broken values stored inside the $filename variable.
This is the loop from the script:
while read filename; do
if [ -z "$filename" ]; then continue; fi
adb pull "$srcFolder/$filename" "$dstFolder"
done < "$lsFile"
Since each iteration of the while-loop reads a line from $lsFile in the following form:
exampleFilename.jpg\r\n
It misinterprets the newline symbols as part of the file name, so adb pull tries to read files with these whitespaces in their names, but fails and it additionally writes a broken output.
Adb Photo Sync
This might not be the answer but might be useful for others looking for android photo/files backup solution.
I use this script on my Windows with git bash. This can be easily used for Linux. A common issue with a long backup process is that it might get interrupted and you might have to restart the entire copy process from start.
This script saves you from this trouble. You can restart the script or interrupt in between but it will resume copy operation from the point it left.
Just change the rfolder => android folder, lfolder => local folder
#!/bin/sh
rfolder=sdcard/DCIM/Camera
lfolder=/f/mylocal/s8-backup/Camera
adb shell ls "$rfolder" > android.files
ls -1 "$lfolder" > local.files
rm -f update.files
touch update.files
while IFS= read -r q; do
# Remove non-printable characters (are not visible on console)
l=$(echo ${q} | sed 's/[^[:print:]]//')
# Populate files to update
if ! grep -q "$l" local.files; then
echo "$l" >> update.files
fi
done < android.files
script_dir=$(pwd)
cd $lfolder
while IFS= read -r q; do
# Remove non-printable characters (are not visible on console)
l=$(echo ${q} | sed 's/[^[:print:]]//')
echo "Get file: $l"
adb pull "$rfolder/$l"
done < "${script_dir}"/update.files
I want to create a encrypting filesystem by encfs(Android's FDE or FBE can't encrypt single directory),but it doesn't work well when I execute the command by local shell(JuiceSSH):
encfs --no-default-flags --public --stdinpass /data/home/MediaStore-e /data/media/0/MediaStore -- -o uid=1023,gid=1023,umask=002 #1023 is media_rw's ID
Only JuiceSSH can access the /data/media/0/MediaStore what I want instead of the old directory(in other words,the true MediaStore).And the other applications(not only Java applications,and also adb etc.) don't think it is a mountpoint:
adb shell
shell#oneplus3:/ $ su
shell#oneplus3:/ # mountpoint /data/media/0/MediaStore
shell#oneplus3:/ # /data/media/0/MediaStore is not a mountpoint
In local shell(JuiceSSH):
oneplus3 ~ # mountpoint /data/media/0/MediaStore
oneplus3 ~ # /data/media/0/MediaStore is a mountpoint
And /system/bin/sdcard(the application which provides /storage/emulated/) can't access it,so I can't read my data from /storage/emulated/0/MediaStore.
But If I execute the command above by adb,it will work well.
I use this script to find the processes which know the directory is a mountpoint:
for dir in /proc/*;do
if [ -e $dir/mounts ];then
if grep -q MediaStore $dir/mounts;then
cat $dir/cmdline
echo
fi
fi
done
When I run encfs --no-default-flags --public --stdinpass /data/home/MediaStore-e /data/media/0/MediaStore -- -o uid=1023,gid=1023,umask=002 by local shell,the script print this:
com.sonelli.juicessh
/data/user/0/com.sonelli.juicessh/files/bin/arm/pie/bash--rcfile/data/user/0/com.sonelli.juicessh/files/share/bashrc
su0-c/data/data/com.sonelli.juicessh/files/bin/arm/pie/bash--rcfile/data/home/.bashrc
/data/data/com.sonelli.juicessh/files/bin/arm/pie/bash--rcfile/data/home/.bashrc
/data/bin/encfs--no-default-flags--public--stdinpass/data/home/MediaStore-e/data/media/0/MediaStore---ouid=1023,gid=1023,umask=002
When I do that by adb,it prints so many lines that I can't put them here,but almost all processes are in them.And other applications (such as music player)can access the data what I want.
The adb shell and local shell run as the same user and group,and own the same secure context(u:r:su:s0),I even tried to clear environment variables,and setenforce 0,I got the same result as before.How can I fix it?I can't use adb to do it every time I reboot.
I have solved this problem by myself,so I answer myself question to share my solution.
This reason why other processes can't access the mount tree what I want is than they are in different mount namespace.ADB's name space is same as /sbin/init,but JuiceSSH(or other terminal application created by zygote64) is in another mount namespace create by clone(2),so you can't access the mount tree by other application even as root.
Solution:
1.
If your su supports --mount-master option,just use it.
2.
Write a shell script which was executed by /system/bin/sysinit * on boot,so it is in the same mount namespace as init(1).It read commands from a FIFO and excute it.It is like this:
#!/system/bin/sh
mknod /data/.global_fifo p
while true;do
eval "$(cat /data/.global_fifo)"
done
Don't forget set correct mode and secure context for it.
sysinit is a application,it execute /system/etc/init.d/* just like most of linux distribution,it was installed on cyanogenmod 13.0.You can write one by yourself and add it into /init.rc.)
I am trying to write a bash script that decompiles several .apk files using apktool. Each apk file is located in a subdirectory of the sample folder.
#!bin/bash
for item in $(ls samples);
do
for apk in $(ls "samples/$item");
do
echo ./apktool/apktool d "./samples/$item$apk"
$(./apktool/apktool d "./samples/$item$apk")
done
done
When I run the script I get the following output:
./apktool/apktool d ./samples/ADRD/53dc.apk*
Input file (./samples/ADRD/53dc.apk*) was not found or was not readable.
The input file error message is the standard for when apktool cannot find a file. However, if I run the following command in the terminal the apktool will work correctly.
./apktool/apktool d ./samples/ADRD/53dc.apk*
I have changed the permissions of all the files located in the samples folder to rw for all users. I also have tried using sudo with the shell script, but this causes the script to hang. However, when I use sudo with the apktool in the command line it also hangs. Therefore, I am not sure if using sudo with apktool is doable.
Any help is appreciated, thanks.
So it looks like this ls gives you an output with an asterisk * appended at the end of the apk filename, because the file is executable.
for apk in $(ls "samples/$item");
This is not the default behaviour of ls, you are getting this probably because you have aliased ls to ls -F or similar. To bypass the alias, rewrite the line this way:
for apk in $(\ls "samples/$item");
Notice the \ I added there.
BTW, is it normal that an apk file is executable? Perhaps you can remove the executable bit:
find samples -name '*.apk' -exec chmod -x {} \;
Also, possibly your script can be replaced with this one liner:
find samples -name '*.apk' -exec ./apktool/apktool d {} \;
Mind you, this is not exactly the same thing, because it may go deeper than two directories. If you need to limit the depth, that's possible too, see man find
So I'm trying to use memfetch to dump the memory of a particular process from my Samsung Galaxy Nexus.
I downloaded memfetch from http://lcamtuf.coredump.cx/
Extracted out its contents using the following command:
tar -xvf memfetch.tgz
Ran an ls on my memfetch directory:
ls
COPYING Makefile memfetch.c mffind.pl README
At this stage i'm supposed to run the make command to get my memfetch executable.
Editing the memfetch.c file, i removed the #include page.h line
So first i downloaded the Android ARM Architecture it with the following command. This was for static cross-compilation for the memfetch:
apt-get install gcc-arm0-linux-gnueabi
Then i edited Makefile at the following areas:
FILE = memfetch
CFLAGS = -Wall -09 -static
CC = arm-linux-gnueabi-gcc
So success, i had my memfetch executable:
make
arm-linux-gnueabi-gcc -Wall -09 -static memfetch.c -o memfetch
ls
COPYING Makefile memfetch memfetch.c mffind.pl README
Then i pushed the memfetch executable into my Android phone:
adb push memfetch /sdcard/memfetch
Now from here i worked things from my phone. I ran executed the adb shell command and created a directory to store the memfetch exectable, ideally to run it from there:
adb shell
su
cd /sdcard
mkdir tmp
mount -t tmpfs tmpfs tmp
cp memfetch tmp
cd tmp
chmod 6755 memfetch
Now here's where the problem comes. When i executed the memfetch, i was getting the following error:
./memfetch 1197
memfetch 0.05b by Michal Zalewski <lcamtuf#coredump.cx>
Usage ./memfetch [ -sawn ] [ -S xxx ] PID
-s - wait for fault signal before generating a dump
-a - skip non-anonymous maps (libraries etc)
-w - write index file to stdout instead of mfetch.lst
-m - avoid mmap(), helps to prevent hanging on some 2.2 boxes
-S xxx - dump segment containing address xxx (hex) only
No matter what PID i tried to dump, i always got the same error. I even tried with various flag combinations but none of them worked :(
Thanks for any help in advanced, let me know if any additional information is required.
That is because of stupidity of Windows Command Line, that make some null characters as input to linux commands.
the solution is, if you don't use other inputs "samwS" comment this lines,
while ((opt=getopt(argc,(void*)argv, "+samwS:h"))!=EOF)
switch(opt) {
case 's': waitsig=1; break;
case 'a': skipmap=1; break;
case 'w': textout=1; break;
case 'm': avoid_mmap=1; break;
case 'S': if (sscanf(optarg,"%x",&onlyseg)!=1)
fatal("Incorrect -S syntax (hex address expected).\n");
break;
default: usage(argv[0]);
}
it you use them just comment
default: usage(argv[0]);