Publish files with Termux on Android
29.8.2024
I usually use server on Kapsi.fi to share files with people. But using it
with phone isn't so nice, meaning where the file is in phone and then
rsync'ing it to the correct folder on the server, possibly even having to
enter the passphrase for SSH key.
But you can have shortcut tasks with Termux, that does something background
and have a widget to start those tasks. So, to make sharing files easier I
want a folder where I can move files and those are uploaded to Kapsi server
automatically after I start the Termux task.
What you need is:
- Android phone
- Termux and Termux Widget installed on that phone
- Rsync installed in Termux
- Server and to be able to SSH into it
After you have done those you can put these files in ~/.shortcuts/tasks/ in
Termux and make them executable.
# Symbolink link to directory which contents you want to upload to server. dir=~/storage/publish/ while true; do inotifywait -qPr -e 'create' --format '%w%f' "$dir" |\ { read -r file # You can add server to ~/.ssh/config and use correct SSH key. I need to change file permissions. rsync -z --chmod=o+r "$file" server: & termux-toast -s "File $file published" } done
# start_publishing is name of the above script. pkill --full start_publishing && pkill inotifywait && termux-toast -s 'Publishing stopped'And on the SSH server's ~/.ssh/authorized_keys file you need to add this line.
# rrsync is part of rsync package and meant to be used for restricting only to rsync files to server. command="/usr/bin/rrsync /path/to/shared/directory",restrict ssh-ed25519 your_public_SSH_key's_fingerprint user@host
That should be all. The user who logins with that particular key can't do
anything else than rsync files under that directory and nowhere else.