OpenStreetMap

Transfer Script for Android 6 via USB under Devuan/Debian

Posted by alexkemp on 8 August 2018 in English (English)

In Transfer files to/from Android 6 via USB under Devuan/Debian it was detailed how to setup both Devuan & the Android smartphone as to import/export files between the two devices. In this post, a script is given to quickly one-touch files from the phone to a designated directory in the desktop.

The script below will need saving into a file called ~/Personal/copyFromPhone. You will then need to:-

:~$ chmod +x ~/Personal/copyFromPhone
:~$ ls -al ~/Personal/copyFromPhone
-rwxr-xr-x 1 user user 1857 Aug  8 21:18 /home/user/Personal/copyFromPhone

The script is launched as :~$ Personal/copyFromPhone. The values $CAMERA, $FUSERMOUNT, ... , $MOUNTPOINT will all need checking. $LOCAL + $MOUNTDIR are almost certainly wrong as provided for any specific user.

Problems:

The major problem I had was 'device is busy' reports on attempted unmount at end of script. Using the unmount command from terminal always worked (fusermount -u /media/user/disk), but I added the ‘lazy unmount’ option to try to get rid of that.

#!/bin/bash
#
# ~/Personal/copyFromPhone
#
# Copy photos from phone to ~/Personal
#
# 2018-08-08 started

GENERAL_ERROR=1
NOT_A_DIR=66
ALREADY_MOUNTED=67

# Internal variables
# Change these to match your own system
CAMERA="Internal storage/DCIM/Camera"
FUSERMOUNT="/bin/fusermount"
JMTPFS="/usr/bin/jmtpfs"
LOCAL="/home/user/Personal"
MOUNTDIR="/media/user/disk"
MOUNTPOINT="/bin/mountpoint"

# pre-checks
# $MOUNTDIR needs to pre-exist, but $CAMERA must not yet be mounted
if [ ! -d "$MOUNTDIR" ] # file is NOT a directory
then
  echo "ERROR: The directory '$MOUNTDIR' does NOT exist"
  echo "('$MOUNTDIR' used as mount-point for camera/phone)"
  exit $NOT_A_DIR
else
  if $MOUNTPOINT -q "$MOUNTDIR"
  then
    echo "ERROR: Cannot continue; something is mounted at '$MOUNTDIR'"
    echo "(If camera, unmount using 'fusermount -u $MOUNTDIR', then try again)"
    exit $ALREADY_MOUNTED
  fi
fi

# Show summary of action
echo "!! REMEMBER TO UNLOCK SCREEN !!"
echo -n "Mount phone at '$MOUNTDIR'; copy files at 'phone:$CAMERA' to '$LOCAL'. "

# Give option to drop-out of script
read -p "Continue? (y/n): " -n 1 -r
echo    # move to a new line
if [[ $REPLY =~ ^[Yy]$ ]]
then
  echo "Script continues…"
else
  echo "Script abandoned."
  exit 0
fi

# OK; ready to go...
# 1. mount the phone
# 2. copy files to desktop
# 3. unmount the phone
if ! $JMTPFS "$MOUNTDIR"
then
  echo "Error whilst mounting the phone/camera"
  exit $GENERAL_ERROR
fi

ls "$MOUNTDIR/$CAMERA/"*

if ! cp "$MOUNTDIR/$CAMERA/"* "$LOCAL/"
then
  echo "Error whilst copying $MOUNTDIR/$CAMERA/* to '$LOCAL/'"
fi

if ! $FUSERMOUNT -uz "$MOUNTDIR"
then
  echo "Error whilst unmounting phone/camera using '$FUSERMOUNT -uz $MOUNTDIR'"
  exit $GENERAL_ERROR
fi

exit 0

Comment from giggls on 9 August 2018 at 09:36

Hm,

I always use scp in conjunction with Termux sshd tor transfer files to and from my Android device.

This is the most Unix like ways to do this after all.

http://blog.gegg.us/2018/06/a-sshd-on-port-22-hack-for-termux-on-android/

Login to leave a comment