Run adb command inside the terminal emulator or programmatically without root


Question

Is there a way to run theadb backupcommand on the device itself without using a ᴘᴄ computer ?



Of course when I try to do it,adbcomplains the daemon isn’t started and it fails to find a device.



u0_a192@klimtwifi:/ $ adb connect localhost:9999
* daemon not running. starting it now on port 5038 *
* daemon started successfully *
unable to connect to localhost:9999:9999
u0_a192@klimtwifi:/ $ adb connect localhost:5038
connected to localhost:5038
u0_a192@klimtwifi:/ $ adb shell
error device not found
u0_a192@klimtwifi:/ $

Answer

Addressing Revision 4


You're still doing things incorrectly even though I noted in revision 1 of my answer that you would need in PC for once and once alone as long as no reboot or disabling of USB debugging occurs.


Regarding your attempt:


u0_a192@klimtwifi:/ $ adb connect localhost:9999  
* daemon not running. starting it now on port 5038 *
* daemon started successfully *
unable to connect to localhost:9999:9999
u0_a192@klimtwifi:/ $ adb connect localhost:5038
connected to localhost:5038
u0_a192@klimtwifi:/ $ adb shell
error device not found

adb connect localhost:9999 didn't work because you didn't do adb tcpip 9999 from a PC. The said command would set the property service.adb.tcp.port to specified port which in turn is read by adbd when it is started/restarted and used to listen for connections.


adb connect localhost:5038 didn't benefit you simply because either you got confused or failed to understand the basics I noted in revision 1 of my answer.


First, clear up any confusion. Just because the term daemon is mentioned doesn't mean that it is adbd. A daemon can be for lots of things. A description of protocol between adb clients and server is noted here. A good understanding of how adb client-server-device/emulator communication occurs is noted here. You're requested to read all the documents to gain understanding of how adb works.


Now, here are the details for your attempt: When you did adb connect localhost:9999, you actually instructed adb client in your host Android device to connect to a slave running adbd listening on 9999. Your adb client tried to look for adb server on your host Android device. Because it didn't find the server running, it started a server and bound the server to port 5038. Nowhere you instructed adbd to listen on port 9999 or even 5038. You can confirm this with root access. Do su -c 'netstat -natup' and you would find adb not adbd listening on port 5038 after execution of your command.


It should now be clear why adb shell resulted in the error.


I should stress that it is futile to even think about running adb client related command on a device without access to PC for once or without root access. I have already noted the procedure in regard to PC in revision 1. If you do have root access and still need to use adb on device then do:


su 2000 -c 'setprop service.adb.tcp.port  PORT'
# choose PORT greater than 1024.
su -c 'stop adbd && start adbd' # this would stop and start adbd.
su -c adb connect localhost:PORT # you shall now be connected and would be required to accept fingerprint to authorize USB debugging.

(Click an image to enlarge it)


IMG:
IMG:




Edited Revision 1


Looking for this?


IMG:


You're doing things incorrectly. First, a bit of basics:



  • adb is a bridge which helps in communicating with a slave (an Android device) from a client on host machine (typically a PC) through a server running on host machine.

  • A slave has a daemon named adbd which has to running in order for slave to communicate back.


When you attempt to run an adb command which requires an Android device for desired output, the adb server expects a slave connected to host using USB. Since no Android device is connected to your Android device through USB, you get error: device not found. If you connect an another Android device through OTG and have USB debugging activated there, you would/might be able to run adb successfully. At least I was able to.


A simplest workaround is to have adbd listening on TCP as well. In that case, you can connect to localhost and get the job done. There is no way a process with ordinary privilege in Android can restart adbd to listen on TCP. The official way on stock Android is to do adb tcpip PORT from host machine. In this case, you would need a PC with setup there. Connect the device to PC and execute from PC, the command:


adb tcpip PORT
# PORT is port number in your device to listen for and establish connection on TCP. Default port is 5555. Choose a PORT greater than 1024 

You can now unplug the device. As long as you don't reboot the device or disable USB debugging from Developer options, adbd would continue to run and listen for connection on your supplied PORT.


Don't worry about security. While adbd would receive connection from local Wi-Fi network, one cannot successfully connect to your device unless you authorize debugging by accepting their fingerprint.


Anyhow, in your terminal emulator, execute


adb connect localhost:PORT

You would be required to accept a fingerprint. If you followed instructions carefully, you would get the output:


connected to localhost:PORT

Now you can run your backup command. But honestly, it is not clear to me what you're trying to achieve. If backup alone is the goal, then Helium app might be able to help, although it also requires root access or PC with setup.


In case you're not aware of this, not all the apps can be backed up using adb.


Topics


2D Engines   3D Engines   9-Patch   Action Bars   Activities   ADB   Advertisements   Analytics   Animations   ANR   AOP   API   APK   APT   Architecture   Audio   Autocomplete   Background Processing   Backward Compatibility   Badges   Bar Codes   Benchmarking   Bitmaps   Bluetooth   Blur Effects   Bread Crumbs   BRMS   Browser Extensions   Build Systems   Bundles   Buttons   Caching   Camera   Canvas   Cards   Carousels   Changelog   Checkboxes   Cloud Storages   Color Analysis   Color Pickers   Colors   Comet/Push   Compass Sensors   Conferences   Content Providers   Continuous Integration   Crash Reports   Credit Cards   Credits   CSV   Curl/Flip   Data Binding   Data Generators   Data Structures   Database   Database Browsers   Date &   Debugging   Decompilers   Deep Links   Dependency Injections   Design   Design Patterns   Dex   Dialogs   Distributed Computing   Distribution Platforms   Download Managers   Drawables   Emoji   Emulators   EPUB   Equalizers &   Event Buses   Exception Handling   Face Recognition   Feedback &   File System   File/Directory   Fingerprint   Floating Action   Fonts   Forms   Fragments   FRP   FSM   Functional Programming   Gamepads   Games   Geocaching   Gestures   GIF   Glow Pad   Gradle Plugins   Graphics   Grid Views   Highlighting   HTML   HTTP Mocking   Icons   IDE   IDE Plugins   Image Croppers   Image Loaders   Image Pickers   Image Processing   Image Views   Instrumentation   Intents   Job Schedulers   JSON   Keyboard   Kotlin   Layouts   Library Demos   List View   List Views   Localization   Location   Lock Patterns   Logcat   Logging   Mails   Maps   Markdown   Mathematics   Maven Plugins   MBaaS   Media   Menus   Messaging   MIME   Mobile Web   Native Image   Navigation   NDK   Networking   NFC   NoSQL   Number Pickers   OAuth   Object Mocking   OCR Engines   OpenGL   ORM   Other Pickers   Parallax List   Parcelables   Particle Systems   Password Inputs   PDF   Permissions   Physics Engines   Platforms   Plugin Frameworks   Preferences   Progress Indicators   ProGuard   Properties   Protocol Buffer   Pull To   Purchases   Push/Pull   QR Codes   Quick Return   Radio Buttons   Range Bars   Ratings   Recycler Views   Resources   REST   Ripple Effects   RSS   Screenshots   Scripting   Scroll Views   SDK   Search Inputs   Security   Sensors   Services   Showcase Views   Signatures   Sliding Panels   Snackbars   SOAP   Social Networks   Spannable   Spinners   Splash Screens   SSH   Static Analysis   Status Bars   Styling   SVG   System   Tags   Task Managers   TDD &   Template Engines   Testing   Testing Tools   Text Formatting   Text Views   Text Watchers   Text-to   Toasts   Toolkits For   Tools   Tooltips   Trainings   TV   Twitter   Updaters   USB   User Stories   Utils   Validation   Video   View Adapters   View Pagers   Views   Watch Face   Wearable Data   Wearables   Weather   Web Tools   Web Views   WebRTC   WebSockets   Wheel Widgets   Wi-Fi   Widgets   Windows   Wizards   XML   XMPP   YAML   ZIP Codes