[Solved] move files older than 30 days from one computer to another using rsync

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

  1. i can't use hostname because I am on android 9 and don't want to install anything else.
  2. 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
30 points · 14 comments · view on lemmy.world

14 Comments

catloaf@lemm.ee · 19 pts · 1y (3 replies)

DNS was invented in 1985 to solve this problem.

You could also run it from the phone.

sartaj@lemmy.world · 2 pts · 1y (2 replies)

i don't know how to use DNS

catloaf@lemm.ee · 4 pts · 1y (1 reply)

So you'd rather reinvent TCP/IP from scratch?

sartaj@lemmy.world · 2 pts · 1y

it satisfies my use case

truthfultemporarily@feddit.org · 14 pts · 1y (1 reply)

Use the hostname of your phone instead.

sartaj@lemmy.world · 0 pts · 1y

I am on android 9

Xanza@lemm.ee · 6 pts · 1y (2 replies)

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.

sartaj@lemmy.world · 1 pts · 1y (1 reply)

i use my phone's hotspot to connect to laptop

Xanza@lemm.ee · 1 pts · 1y

Seems dubious, and will only work locally..

RedSnt@feddit.dk · 4 pts · 1y

Yeah, like @catloaf@lemm.ee 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.

gonzo028@sopuli.xyz · 3 pts · 1y (1 reply)

I use syncopoli to rsync files from my phone to the computer. Perhaps this helps.

https://f-droid.org/packages/org.amoradi.syncopoli

sartaj@lemmy.world · 3 pts · 1y

i dont want to install another app and tweak battey saver setting, auto start managent etc. termux is already set up

tasankovasara@sopuli.xyz · 1 pts · 1y

Kudos, I think that is a great DIY solution!

BaumGeist@lemmy.ml · 1 pts · 1y (1 reply)
[ removed ]
sartaj@lemmy.world · 1 pts · 1y

I am on native android