hæx.com logo

Syncing/backup phone media to computer

After moving to linux main, WinSCP was no longer an option for an easy one-way-sync to backup my phone media.

I spent hours scanning google/stackoverflow/superuser for a better solution, but in the end I opted for a solution based on discussions with GPT-4.

LFTP

The solution was simple; lftp let's you mirror files with wildcard patterns. So this new script solved my problem perfectly:

#!/bin/bash

# FTP credentials
HOST='10.0.0.1'
USER=''
PASS=''
PORT=21

# Remote directory to start from
REMOTE_DIR='/'

# Local directory where you want to store files
LOCAL_DIR='/home/stig/Phonesync'

# Directories to exclude
EXCLUDE_DIRS=('android','Android','emulated','self','cache','_media','LOST.DIR','ANRSnap','Alarms','AzRecorderFree','Cardboard','Notifications','Podcasts','Ringtones','TTImages_cache','airdroid','caustic')

# File patterns to include
INCLUDE_PATTERNS=('*.jpg' '*.jpeg' '*.png' '*.mp4' '*.webm' '*.pdf' '*.txt')

# Build the exclusion arguments for each directory in the list
EXCLUDE_ARGS=()
for DIR in "${EXCLUDE_DIRS[@]}"; do
EXCLUDE_ARGS+=("--exclude-glob */${DIR}/*")
done

# Build the inclusion arguments for each pattern in the list
INCLUDE_ARGS=()
for PATTERN in "${INCLUDE_PATTERNS[@]}"; do
INCLUDE_ARGS+=("--include-glob ${PATTERN}")
done

# Start building the lftp command
LFTP_COMMAND="
set ftp:ssl-allow no;
set ftp:list-options -a;
set net:timeout 30;
set net:max-retries 2;
set net:reconnect-interval-base 5;
open ftp://$HOST:$PORT;
lcd $LOCAL_DIR;
cd $REMOTE_DIR;
mirror --verbose --use-pget-n=8 --parallel=8 --only-newer"

# Add the include and exclude arguments to the lftp command
for INCLUDE_ARG in "${INCLUDE_ARGS[@]}"; do
LFTP_COMMAND+=" $INCLUDE_ARG"
done

for EXCLUDE_ARG in "${EXCLUDE_ARGS[@]}"; do
LFTP_COMMAND+=" $EXCLUDE_ARG"
done

# Finalize and execute the lftp command
LFTP_COMMAND+=" . ; bye"

lftp -u $USER,$PASS -e "$LFTP_COMMAND"

echo "phonesync done"