To print the first names of users using cut: cut -d: f5 /etc/passwd Here, -d is the delimiter, f1 denotes the first field (username), and /etc/passwd is the text file that contains the data. To print the usernames in Linux using cut: cut -d: f1 /etc/passwd The syntax of cut is quite similar to the awk command. PIDFile = /run/syncservice/syncservice.pidĮxecStartPre = /bin/mkdir /run/syncserviceĮxecStartPre = /bin/chown -R root:root /run/syncserviceĮxecStopPost = /bin/rm -rf /run/syncserviceĬhmod this service file and reload the systemd daemon: chmod 755 /etc/systemd/system/sync.Alternatively, you can also use cut instead of the awk command. To finish things off, lets create the systemd service file that can stop, start, and reset the script on demand or on specific events like a system bootup.Ĭreate a file called rvice in the directory /etc/systemd/system/ and put the following contents in it: Rsync -avz -e "ssh -i /root/rsync-key -o StrictHostKe圜hecking=no“ /opt/syncfiles/ saved this script in the /opt directory as file-sync.sh. Inotifywait -r -e modify,attrib,close_write,move,create,delete /opt/syncfiles
# Supposed to run on rsync-host01, change rsync-host02 to rsync-host01 to make a script that is meant to run on rsync-host02. Now that you have got all the pre-configuration work done, it's about time to write a script that goes through an infinite loop with inotifywait in it: #!/bin/bash # Remember to execute this script on all servers separately! Then, copy the output of the script in all of your servers' authorized_keys files. # Removing public key for security purposes. # Paste the output in your destination servers' ~/.ssh/authorized_keys file:
Let's call it SyncFiles: mkdir /opt/syncfilesĪnd for secure file transfer, we want a public-private key link for the transfer link that rsync uses, this is how to configure it: ssh-keygen -t rsa -f ~/rsync-key -N ''
All these 3 machines needed to be setup with the following software packages:Īlso noteworthy is that these machines are absolutely not connected through a private network. I needed to get myself a nice development environment at first so I started off 3 virtual servers which all run Ubuntu 16.04, my personal favourite. Rsync is a great solution, but having to run rsync manually would take a lot of unnecessary time away, right? And that is where inotify is for: real-time monitoring of your filesystem so that your files can be synced between multiple machines with the power of rsync! Well, I got the solution for you: with a little bit of thinking in an innovative way I have found the solution that might bring you onto the right path as well. They contain files that you want to have automatically synced when possible, because that would save a lot of time. So: you've got two or more clients and/or servers. Sync folders and files on Linux with rsync and inotify