Search code examples
sshpasswordsrsyncssh-keygen

How to create repeating rsync that runs on one password input?


I am wanting to rsync files from my home computer to a cloud server. I am able to set up my continuous rsync with the following:

#!/bin/bash

while :

do 

rsync -rav * --include=*.bz2 --exclude=*.* --exclude=ZIP.sh --exclude=UPLOAD.sh
--chmod=a+rwx [email protected]:/home/user/date

sleep 180

done

This of course will run continuously if i set up a keygen as here states. I want to run the rsync continuously with me entering the password the first time and after that it will continuously run until I push CTRL+C. Is there a way to do this?


Solution

  • Yes, using SSH connection sharing:

    Add this top your ~/.ssh/config file:

    ControlMaster auto
    ControlPath /tmp/ssh_%r@%n:%p
    ControlPersist 8h
    

    Connection sharing means that all your SSH connections to the same server will share the same connection. This means you can skip the authentication process for all but the first connection. The ControlPersist setting controls how long the connection will idle for before being closed (8 hours means I can login in the morning, and the connection will still be active at the end of the day, but will have expired before the next day).

    The ControlPath specifies where the cached sockets will live. It can be anywhere you like, and they can be called anything you like, but the /tmp directory will do fine, and the name must be unique to each user, server, and port you wish to use, or else you'll get clashes.


    Incidentally, you should probably check out the lsyncd tool as an alternative to continuous active scanning. It uses kernel notifications to watch the file-system, and launches rsync only when something actually changes.