Using Rsync over SSH, to keep your files in sync.
Tested on FreeBSD 4.10, if you have a different version of rsync, this may not work.
If you don’t know what rsync is man rsync.
First the local and remote machine must have rsync installed, so…
on both machines pkg_add -r rsync
If you would like to automate the sync process, you need to create a key pair for ssh to use.
On the destination computer log in and type
$ ssh-keygen -t dsa
It will ask you where to put the files, just press enter.
It will then ask you for a passphrase, just press enter twice.
now, send the private key to the originating machine.
$ scp id_dsa username@remotehost.com:.ssh/id_dsa
Copy the public key to your authorized keys, set the permissions, and remove the private key.
$ cd ~/.ssh
$ cat id_dsa.pub >> authorized_keys
$ chmod 600 authorized_keys
$ chmod 700 .
$ rm id_dsa
Now log into the originating machine, and set permissions on the private key.
$ cd ~.ssh
$ chmod 600 id_dsa
$ chmod 700 .
Now check to see if you can log in to the destination machine without a password.
$ ssh username@remotemachine
Hopefully it should log you in with no problem, if not a good troubleshooting step is to
$ ssh -vvv username@remotemachine ( Verbose )
You can also try changing StrictModes no in /etc/ssh/sshd_config
Now that you have a working ssh connection without any password try to rsync, from the originating machine.
$ cd; mkdir test
$ rsync -rv username@remotemachine:/directory/to/copy /home/username/test
$ cd test
$ ls
You should have a copy of the files from the remote machine.
Now try to automate this, ( change emailaddress to your emailaddress )
script:
#!/usr/local/bin/bash
rsync -rv –timeout 30 username@remotemachine:/directory/to/copy /local/directory > /logdirectory/`date ‘+%d-%B-%Y’`.log 2>&1
if [ “$?” -ge “1″ ]
then mail -s “Rsync Failed” emailaddress < /logdirectory/`date ‘+%d-%B-%Y’`.log
fi
This will rsync the files, with a timeout of 30 seconds, create a log file with the date in the name containing the transfer information.
Finally if the rsync fails and kicks out an exit status of 1 or greater, then the script will send you an email with the contents of the failed log.
Happy Rsyncing
|