You are not logged in.

#1 2023-07-31 15:22:33

funkaddict
Member
Registered: 2018-08-13
Posts: 106

[SOLVED] Using rsync to sync two machines via ssh

Hi there,

I am currently trying to write a script which automatically syncs respective folders on two local machines. Both of them are in the same network. I use ssh and rsync for this purpose. However, there is one question which I am still not able to figure out. Let's say I have machine A and machine B. On one day I am altering file xy.txt on machine A and sync the changes to machine B. On the next day I am updating the file on machine B. Now I want to sync the updates back to machine A.
Can I still do this with the same bash script located on machine A or do I have to have two different scripts, one on machine A, one on machine B. Depending on which I have been working the last time, I have to start the sync script?

At the moment I do this:

#!/bin/bash

# Defining variables
local_folder1="/home/user/Desktop/Test"
remote_ip="ipaddress"
remote_port="port"
remote_folder1="/home/user/Desktop/Test"
username="user"

# Function to perform bidirectional synchronization
sync_folders() {
    # Sync local to remote
    rsync -avz --checksum --update --delete -e "ssh -p $remote_port" "$local_folder1/" "$username@$remote_ip:$remote_folder1"

    # Sync remote to local
    rsync -avz --checksum --update --delete -e "ssh -p $remote_port" "$username@$remote_ip:$remote_folder1/" "$local_folder1"
}

# Run the synchronization function
sync_folders

But I am not sure if that works correctly. Especially the --delete function might do something I am not 100% sure about.

Last edited by funkaddict (2023-07-31 16:17:53)

Offline

#2 2023-07-31 15:50:38

Trilby
Inspector Parrot
Registered: 2011-11-29
Posts: 29,535
Website

Re: [SOLVED] Using rsync to sync two machines via ssh

You want a two way sync.  I'd have to look up the proper flags to do so, but rsync can / will do that itself.  But if you do each one-way sync explicitly in sequence as you have done in your script, this will *not* get the goal you want as it will delete / overwrite some changes.

Rsync can look at file timestamps and udpate either the local from the remote or the remote from the local based on which one was more recently modified.  So your whole "sync_folders" function should be replaced by a single call to rysnc with the correct flags / options.


"UNIX is simple and coherent..." - Dennis Ritchie, "GNU's Not UNIX" -  Richard Stallman

Offline

#3 2023-07-31 16:17:26

funkaddict
Member
Registered: 2018-08-13
Posts: 106

Re: [SOLVED] Using rsync to sync two machines via ssh

Trilby wrote:

You want a two way sync.  I'd have to look up the proper flags to do so, but rsync can / will do that itself.  But if you do each one-way sync explicitly in sequence as you have done in your script, this will *not* get the goal you want as it will delete / overwrite some changes.

Thank you, this describes my problem quite well.

Trilby wrote:

Rsync can look at file timestamps and udpate either the local from the remote or the remote from the local based on which one was more recently modified.  So your whole "sync_folders" function should be replaced by a single call to rysnc with the correct flags / options.

Ah okay. I tried this as I also thought this might be reasonable compared to run two instances of rsync. However, it didn't work out for me. I will try to find the right flags.

In the meanwhile I also found out about Unison which also does the job quite well.

Offline

#4 2023-08-09 00:11:28

maximus-prime
Member
Registered: 2023-08-09
Posts: 1

Re: [SOLVED] Using rsync to sync two machines via ssh

Not sure if it fits your use case, but I'd recommend also checking out Syncthing

Offline

Board footer

Powered by FluxBB