phone-dump.nix (2166B)
1 {pkgs, ...}: let 2 phone-dump = pkgs.writeShellScriptBin "phone-dump" '' 3 # variables 4 phone_remote=phone 5 phone_ip=$(grep -A4 "$phone_remote" "$(rclone config file | tail -1)" | awk '/host/ {print $3}') 6 destination="/mnt/pictures/personal" 7 if [ ! -d "$destination" ]; then 8 echo "Destination $destination does not exist." 9 exit 1 10 fi 11 12 # if ping phone 13 if ping -c 1 "$phone_ip" &>/dev/null; then 14 echo "Phone reachable, mounting remote" 15 directory_temp="$(mktemp -d)" 16 rclone mount "$phone_remote": "$directory_temp" --daemon 17 cd "$directory_temp" || exit 18 19 declare -a directories=( 20 "$directory_temp/DCIM/Camera" 21 "$directory_temp/Pictures" 22 "$directory_temp/Pictures/Whatsapp" 23 "$directory_temp/Android/media/com.whatsapp/WhatsApp/Media" 24 "$directory_temp/Android/media/com.whatsapp/WhatsApp/Media/WhatsApp Images" 25 "$directory_temp/Android/media/com.whatsapp/WhatsApp/Media/WhatsApp Video" 26 ) 27 for i in "''${directories[@]}" 28 do 29 if [ -d "$i" ]; then 30 echo "$i" 31 find "$i" -type f -name '.nomedia*' -delete -print 32 ${pkgs.phockup}/bin/phockup "$i" "$destination/" -m 33 fi 34 done 35 36 if [ -d "$directory_temp/Pictures/Screenshots" ]; then 37 find "$directory_temp/Pictures/Screenshots" -type f -exec mv '{}' "$destination/screenshots/" -vi \; 38 fi 39 40 echo "Tidying up..." 41 find "$destination" -type f -iname 'thumbs.db' -delete -print 42 find "$destination" -type f -name '.nomedia*' -delete -print 43 find "$destination" -type d -name '.thumbnails*' -delete -print 44 find "$directory_temp" -maxdepth 2 -type d -not -path "*/\.*" -empty -delete -print 2>/dev/null 45 echo "Unmounting storage..." 46 sleep 2s 47 umount "$directory_temp" || fusermount -uz "$directory_temp" 48 echo "Deduplicating photos..." 49 ${pkgs.jdupes}/bin/jdupes "$destination" -r 50 find "/tmp/tmp.*" -maxdepth 1 -type d -not -path "*/\.*" -empty -delete -print 2>/dev/null 51 else 52 echo "Phone not reachable via ping, exiting" && exit 1 53 fi 54 ''; 55 in { 56 environment.systemPackages = [phone-dump]; 57 }