I have a laptop and a phone in which termux is installed. I want to move files from my phone to my computer using rsync if all files are older than 30 days. I think I should use find command with -exec rsync.
find . -mtime +30 -exec rsync something
but the problem is my phone’s IP address is always changing so how do I transfer file? I have to run this script in my laptop
solution
i wrote a script which move files and runs in my laptop
- i can’t use hostname because I am on android 9 and don’t want to install anything else.
- most of the time I am offline so using any internet service is not a good idea ( my devices are connected in themselves )
here is my script
#!/usr/bin/bash
#checking if script not already running and exit if already running
[ "${FLOCKER}" != "$0" ] && exec env FLOCKER="$0" flock -en "$0" "$0" "$@" || :
set -e
TARGET_MAC="d2:1c:3c:3d:6a:a7"
RSYNC_LOG="$HOME/log/rsync_log.txt" # Rclone log file
function get_ip() {
# need to edit sudoers file to run sudo nmap -sn 192.168.43.0/24 without password
# Allow members of group sudo to execute any command
# %sudo ALL=(ALL:ALL) ALL
# 'username' ALL=(ALL) NOPASSWD: /usr/bin/nmap -sn 192.168.43.0/24
sudo nmap -sn 192.168.43.0/24 | grep --before-context=3 -i "$TARGET_MAC" | tail -n 3 | head -n 1 | awk '{ print $6 }' | sed 's/[()]//g'; }
# run command only if wifi is connected
if echo $(nmcli general status) | grep -q -w "connected" ; then
# for ssh to work in a changing IP address, a configuration line has to be enabled so that it does not ask for yes/no while connecting to a changed ip address
TARGET_IP=$(get_ip)
ssh -p 8022 "$TARGET_IP" 'find /data/data/com.termux/files/home/storage/shared/Pictures/ -mtime +30 -type d,f -printf %P\\0' | rsync --append --update --times --atimes --mkpath --verbose --human-readable --remove-source-files --log-file="$HOME/log/synced.log" --exclude='.dtrash' --files-from=- --from0 -e "ssh -p 8022" "$TARGET_IP":/data/data/com.termux/files/home/storage/shared/Pictures/ $HOME/Pictures
Kudos, I think that is a great DIY solution!
VPN… This is a specific problem that VPNs were created to solve.
On your Wireguard network (or whichever), note your vLAN IPs, and configure
rclone
/rsync
with them like normal. Ensure you’re connected to Wireguard and then run your command;❯ fd --change-older-than 30days -X rclone sync phone:my/path/here pc:destination/path/here
Using a VPN ensures that regardless of WiFi/Cellular connections you’ll still be able to transfer at home, or remotely. Using methods like hostnames only work locally.
i use my phone’s hotspot to connect to laptop
DNS was invented in 1985 to solve this problem.
You could also run it from the phone.
i don’t know how to use DNS
So you’d rather reinvent TCP/IP from scratch?
it satisfies my use case
Use the hostname of your phone instead.
I am on android 9
Yeah, like @[email protected] said, Dynamic DNS ought to fix this issue. There are plenty of free ones. I currently use ClouDNS, but No-IP is also decent.
Hardest part is updating from a phone, but there’s articles on both sites for how to do that.I use syncopoli to rsync files from my phone to the computer. Perhaps this helps.
i dont want to install another app and tweak battey saver setting, auto start managent etc. termux is already set up
Is your phone running a GNU distro or rooted Android, or is it just regular Android with a Termux chroot?
I am on native android