Syncing directory to SFTP as a different user

I want to upload/sync some files to a specific directory on the server, which is owned by root or some other user, using the SFTP remote. I would like that the uploaded files are also owned by that specific user/group, and have some correct permissions set. However, I would like to do this from my own sudo-enabled user, without connecting to SFTP as that another user.

I think a common solution in SFTP for this is to modify the SFTP command to something like sudo /usr/lib/openssh/sftp-server. However, the server is using NixOS, where the sftp-server binary is not directly exposed, so it can't easily be set in the client config. I think there is a way to expose it, but in any case, the sftp-server path varies across different distributions, so having to configure the client config for each specific server does not seem like a good solution. Is there any way to just prepand the sudo to the server-configured binary from the rclone configuration?

Another solution that I thought of is to first upload files to a temporary staging directory owned by the current user, modify permissions and ownership to the target user through sudo or alternatives, and then copy the remote files to the target location. This solves the problem of having to run sftp-server as root, as all operations can be done either as connecting user or escalated with sudo. However, as that staging directory is temporary, rclone won't know which files are already present on the server (as they actually live in a different directory), so it will have to re-upload all of them again.

So, would it be possible to add some way to automatically sync files to a temporary directory while comparing them to the files already in the target directory, set the correct permissions and ownership and then sync them to the target directory?

Or, is there other possible solution how to achieve this?

welcome to the forum,

i do not have a solution for you but just to share a few flags.

--compare-dest
--copy-dest

and filters such as:
--include-from
--exclude-from
--filter-from

Hi,

If you are using your own PC you can install rclone and configure an SFTP remote with whatever user ID and password you want and then connect to the server and synchronize the files to wherever you want to.

When the files are synced to the server they should have the user privileges of the remote user ID and password if you created in rclone.

Have you tried this?

If you have the disk space, temporarily copy the files already on the server to the staging area, then rclone copy to that directory as your user so you don't have to upload the whole thing, chmod/chown and move into place.

Thanks for replies!

If I understand what --compare-dest does, this seems like a partial solution. So, if I pointed --compare-dest to the final location, while set the sync destination to a temporary directory, rclone would skip the files already present/identical in the final location, and upload all other to the temporary directory, where I could chown them and sync them again to the final location, right?

However, could chowning/chmoding the files break the check if the files are identical when comparing to the local version, so it would want to upload all of them again?

Also, I think this doesn't handle deletes well. If removed a local file, it obviously won't get synced to the temporary directory, but for the final sync between the temporary directory and the final location, there will be no way to know if the file was deleted locally or just not present because it was unchanged.

I have considered this, but I would like that user to be server-only without logins.

Yeah, that is also a possible solution, but having to double copy everything doesn't seem so nice...

again, just a suggestion.
to save the deleted file could be something like

rclone sync src: dst:current --backup-dir="dst:archive/`date +%Y%m%d.%I%M%S"`