Automating Photos and Media Sync with mtpcopy
Overview
mtpcopy is a command-line tool for copying files to/from MTP (Media Transfer Protocol) devices (phones, cameras, media players). Use it to automate transferring photos and media when a device connects.
Typical workflow
- Detect device — run mtpcopy (or an MTP listing tool) to confirm the device is visible.
- Mount/claim device — ensure no other process (gvfs, gphoto2, Windows services) is holding the device.
- Run mtpcopy with filters — copy only new or matching files (by extension, date, or size).
- Post-actions — delete/rename on device, move files into dated folders, update local index or cloud upload.
Example commands (Linux)
- Copy all JPEGs from device root to local folder:
Code
mtpcopy get /DCIM/.JPG ~/Pictures/Phone/$(date +%F)/
- Copy only newer files (assume mtpcopy supports skip-by-size/time — if not, combine with rsync-like logic after download):
Code
mtpcopy get /DCIM/.JPG ~/Pictures/Phone/ && rsync -au –remove-source-files ~/Pictures/Phone/$(date +%F)/ /final/backup/
(adapt to your mtpcopy options; many MTP tools use flags like –skip-existing or –overwrite)
Automating on connect
- Linux (udev + systemd user service):
- Create a udev rule matching your device vendor/product that triggers a systemd –user service.
- Systemd service runs a sync script that calls mtpcopy and performs post-actions.
- macOS: use a Folder Action or launchd job triggered by device mount (if device exposes a mount) or a small watcher script.
- Windows: use Task Scheduler triggered by device connection (or a small PowerShell script polling for MTP device).
Example simple Linux systemd user unit + script
- Script (/home/user/bin/phone-sync.sh):
Code
#!/bin/bash device_wait_cmd=“mtpcopy list”# replace with appropriate detectionwait for device
until \(device_wait_cmd | grep -q "YourDeviceName"; do sleep 1; done mkdir -p ~/Pictures/Phone/\)(date +%F) mtpcopy get “/Internal shared storage/DCIM/*.{JPG,MP4}” ~/Pictures/Phone/$(date +%F)/
optional: run image processing or cloud upload here
- systemd unit (run on user login) calls the script when needed.
Reliability tips
- Ensure exclusive access: stop gvfs/gphoto2 or any app that may lock the device before running mtpcopy.
- Use checksums or file size checks to avoid partial/ corrupted copies.
- Log operations and retry failed transfers.
- Test with your specific device model — MTP behavior varies between vendors.
Alternatives / complements
- Use libmtp-based tools (mtp-tools, mtpfs, mtpcmd) or projects that add wireless MTP (airmtp) or camera-specific sync apps if mtpcopy lacks needed features.
If you want, I can: provide a ready-to-run systemd + udev example tuned to a specific device vendor/product ID, or adapt the script to Windows/macOS — tell me which OS and device model.
Leave a Reply