Ho creato un piccolo shell script in bash su Ubuntu Linux per fare un backup delle foto presenti sul mio smartphone Android. Il requisito è che il telefono sia collegato tramite USB in modalità MTP (e non PTP); in questo modalità, appena si collega il telefono alla porta USB del computer, la sua memoria viene montata come File System GVfs. Lo script gestisce anche il caso in cui sullo smartphone sia presente una scheda SD montata.

  • Invocazione dello script (con set -x per verificare se i path sono giusti)
alex@vegeta:~$ ./rsync-smartphone.sh asus
+ '[' -z asus ']'
+ DEVICE=asus
+ HOME=/home/alex
++ mount
++ grep gvfs
++ awk '{ print $3 }'
+ ORIG=/run/user/1000/gvfs
+ DEST=/home/alex/asus
+ '[' '!' -d /home/alex/asus ']'
+ cd /run/user/1000/gvfs
+ cd mtp:host=%5Busb%3A001%2C008%5D
++ pwd
+ CWD=/run/user/1000/gvfs/mtp:host=%5Busb%3A001%2C008%5D
+ read LINE
++ find . -maxdepth 3 -name WhatsApp -o -name DCIM -o -name Pictures -type d
+ cd /run/user/1000/gvfs/mtp:host=%5Busb%3A001%2C008%5D
+ mkdir -p '/home/alex/asus/./Memoria interna/DCIM'
+ cd '/run/user/1000/gvfs/mtp:host=%5Busb%3A001%2C008%5D/./Memoria interna/DCIM'
+ pwd
/run/user/1000/gvfs/mtp:host=%5Busb%3A001%2C008%5D/Memoria interna/DCIM
+ rsync -av --exclude '*.Statuses*' . '/home/alex/asus/./Memoria interna/DCIM'
sending incremental file list
. . .

alex@vegeta:~/asus$ pwd
/home/alex/asus
alex@vegeta:~/asus$ ls -l
totale 8
drwxr-xr-x 5 alex alex 4096 nov 1 21:16 'Memoria interna'
drwxr-xr-x 3 alex alex 4096 nov 1 21:16 'Scheda SD'
  • Sorgente (disponibile anche su github)

#!/bin/bash
set -x

# rsync photos for Android devices connected in MTP mode

# Check for arguments before running script:

if [ -z "$1" ]; then
echo
echo " $(basename $0) Error: No argument. Enter device name"
echo
echo " Example: $(basename $0) Nexus-5x"
echo
exit 1
fi

DEVICE=$1
HOME="/home/${USER}"
ORIG=$(mount | grep gvfs | awk '{ print $3 }')
DEST="${HOME}/${DEVICE}"
[ ! -d $DEST ] && mkdir -p $DEST
FOUND="$(find $ORIG -maxdepth 1 -name 'mtp*' -type d | wc -l)"
if [ $FOUND -eq 0 ];then
echo "Error: Device not found - File system gvfs not mounted"
exit 10
elif [ $FOUND -gt 1 ];then
echo "Error: Found $FOUND devices"
echo "Disconect one or more devices; you must have only one connected device"
exit 11
elif [ $FOUND -eq 1 ];then
echo "OK: Found $FOUND devices"
fi
cd $ORIG
cd mtp*
CWD=$(pwd)

while read LINE; do
cd "$CWD"
mkdir -p "${DEST}/${LINE}"
cd "$CWD/${LINE}"
pwd
# exclude ".Statuses" WhapsApp directory because rsync returns read errors failed verification
rsync -av --exclude '*.Statuses*' --exclude '.thumbnails' --exclude '.aux' --exclude '*.db.crypt*' . "${DEST}/${LINE}"
done < <(find . -maxdepth 3 -name 'WhatsApp' -o \
-name 'DCIM' -o \
-name 'Music' -o \
-name 'Screenshots' -o \
-name 'Video' -o \
-name 'Pictures' -type d)

# vim: ts=2 sw=2 noet ai nohls

 

Lo script prevede il backup delle cartelle DCIM (fotocamera) e  media (foto, video) di WhatsApp ma è possibile aggiungere altre cartelle come ad esempio Music, documents, Screenshots, ecc.

Lascia un commento

Il tuo indirizzo email non sarà pubblicato. I campi obbligatori sono contrassegnati *

Questo sito usa Akismet per ridurre lo spam. Scopri come i tuoi dati vengono elaborati.