Continuously synchronizing files through network on Linux
This is the way I use to sync files between two computers on same network.
It’s like a local Dropbox with less capabilities.
To accomplish the sync operation full autonomously we need three things to done.
1- File sync
2- Connecting computers without password
3- Task scheduling
You should definitively check given commands man page (man command) or help (command --help) to customize command for your own needs.
File Sync
We will use rsync utility to copy files between computers.
To fetch files from remote computer:
rsync -vrncz user@192.168.0.0:/source/folder/to/sync /target/folder/to/sync
To send files to remote computer:
rsync -vrncz /target/folder/to/sync user@192.168.0.0:/source/folder/to/sync
What vrncz parameters means:
- v (verbose) prints detailed operation log to screen.
- r (recursive) applies sync operation for subdirectories.
- n (dry-run) shows what is going to done without doing it. (Remove this parameter to actually run the command.)
- c (checksum) skip files if checksums are same to faster sync.
- z (compress) compress files to copy faster through network.
This operation will need to user password for every run, to bypass this we are going to create and share our public ssh-key between computers. If you are going to use rsync on local computer you do not need this step.
Connecting A Linux Computer Without Password
Basically what we are going to do is generate secret key pair and share the public one with the other computer, so our computer will know who is coming when the other computer knock the door.
To generate key pair run:
You can pass the questions asked during to key generation by pressing enter or modify defaults.
ssh-keygen -t rsa
To share our public key with remote computer:
This command will ask the user’s password for one time to share key with target computer.
ssh-copy-id user@192.168.0.0
Now we can automatize the operation via crontab.
Task Scheduling Via Crontab
There is more than one way to schedule tasks on Linux OS.
I think for user level operations crontab is more than enough.
To edit crontab file run:
crontab -e
Now we need to add our commands to opened file so it can run automatically. Add this lines at the end of the opened file:
*/15 * * * * rsync -vrncz user@192.168.0.0:/source/folder/to/sync /target/folder/to/sync >> /path/to/log/file.log
*/15 * * * * rsync -vrncz /target/folder/to/sync user@192.168.0.0:/source/folder/to/sync >> /path/to/log/file.log
I will not explain to how crontab works in detail, just to know */15 means run every fifteen minutes.
There are online cron editors you can find help.
At the and of the lines there are >> /path/to/log/file.log. ‘>>’ means redirect command output to ‘/path/to/log/file.log file’ so later we can check output to see if every thing is going well.