You are not logged in.
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
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
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.
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
Not sure if it fits your use case, but I'd recommend also checking out Syncthing
Offline