Dateien
scripts/sync-iPhone.sh
T

125 Zeilen
3.4 KiB
Bash
Ausführbare Datei

#!/bin/bash
# Define required programs and libraries
program_list=("ifuse" "ldconfig" "rsync")
library_list=("libimobiledevice")
sync_folder="$HOME/iPhone"
sync_mount="${sync_folder}/.mnt"
sync_data="${sync_folder}/data"
sync_archive="${sync_folder}/archive"
current_date=$(date +%F)
safety_dir="changes_${current_date}"
# Check if required programs are installed
for program in "${program_list[@]}"; do
if command -v "${program}" >/dev/null 2>&1
then
echo "OK, ${program} exists."
else
echo "Error: ${program} is missing. Please install: ${program_list[*]}"
exit 1
fi
done
# iteration thru list of libraries
for library in "${library_list[@]}"; do
if ldconfig -p | grep ${library} >/dev/null 2>&1
then
echo "OK, ${library} exists."
else
echo "Error: Library ${library} is missing. Please install: ${library_list[*]}"
exit 2
fi
done
# Clear older dead mounts if they block the directory
if [ -d "${sync_mount}" ]; then
fusermount -u "${sync_mount}" >/dev/null 2>&1 || umount -l "${sync_mount}" >/dev/null 2>&1
rmdir "${sync_mount}" >/dev/null 2>&1
fi
echo "Create the folder ${sync_mount} ..."
if mkdir -p ${sync_mount} >/dev/null 2>&1
then
echo "done."
else
echo "Folder ${sync_mount} already exists."
fi
echo "Mount the iPhone into that folder using ifuse ..."
if ifuse ${sync_mount}
then
echo "ok."
else
echo "did not work. Check your connection to the iPhone. Check PIN on the iPhone."
exit 3
fi
# Display storage capacity of the iPhone
echo -e "\n--- IPHONE STORAGE USAGE ---"
df -h "${sync_mount}"
echo -e "----------------------------\n"
echo "RSYNC the iPhone ..."
FROM="${sync_mount}/"
TO="${sync_data}/"
while true; do
echo "** Synchronization from ${FROM} to ${TO} **"
echo "Options:"
echo " (s) sync & delete from 'data' (matches iPhone state)"
echo " (n) normal sync to 'data' (keeps files deleted on iPhone)"
echo " (a) safety-net ARCHIVE (syncs 'data' to 'archive', backups changed files, NEVER deletes)"
echo " (m) mount only & exit"
echo " (c) cancel"
read -n 1 -p "Action: " response
echo
case ${response} in
[Nn]* )
mkdir -p "${sync_data}"
rsync -avP "${FROM}" "${TO}"
break
;;
[Ss]* )
mkdir -p "${sync_data}"
rsync -avP --delete "${FROM}" "${TO}"
break
;;
[Mm]* )
echo "iPhone remains mounted at ${sync_mount}. Remember to unmount manually later!"
exit 0
;;
[Aa]* )
# Ensure the archive folder exists
mkdir -p "${sync_archive}"
if [ ! -d "${sync_data}" ] || [ -z "$(ls -A "${sync_data}")" ]; then
echo "Error: The 'data' folder is empty or does not exist. Run a sync (n or s) first!"
else
echo "Starting safety-net archive backup from ${sync_data} to ${sync_archive}..."
# Safety-net sync: moves modified/overwritten files into a dated subfolder
rsync -avP --backup --backup-dir="${safety_dir}" "${sync_data}/" "${sync_archive}/"
echo "Archive completed. Safe directory for modified files today: ${sync_archive}/${safety_dir}"
fi
break
;;
[Cc]* )
echo "Synchronization skipped by user."
break
;;
* )
echo "Invalid selection. Please choose s, n, a, m, or c."
;;
esac
done
echo "Unmounting iPhone..."
if fusermount -u "${sync_mount}" 2>/dev/null || umount "${sync_mount}" 2>/dev/null; then
echo "Successfully unmounted."
rmdir "${sync_mount}"
else
echo "WARNING: Unmount failed. A process might still be accessing the mount directory."
fi
echo "Process finished."