In this article, I’ll be taking a look at an open-source utility that doesn’t get as much credit as I feel it should.
If you have followed my open-source articles here and at the i-UG user group meetings, you will often have to copy files from one IFS directory to another.
Yes you can use the CP (Copy) command to achieve this, but sometimes this command lacks functionality and we have to use other methods to get around the CP shortcommings.
This is where RSync (Remote Sync) shines.
It is a utily that we use often in our day to day workings, and felt that a PowerWire article would give it some of the credit it deserves. A true favourite of ours.
Some of the benefits of using RSync are;
- Can be used to copy files onto our IFS
- It allows synchronization of files.
- Can be used to sync files over the network to another IBM i
- Can be scheduled to allow nightly backups
How do we install it?
Like all the open-source offerings made available by Jesse and his team at IBM, installation is easy, by using either the Access for Client Solutions (ACS) or using Yum.
In the figure below we can see, the Yum command to install RSync,
YUM INSTALL RSYNC
And here we can see RSync in ACS.
This will install globally, so we do not have to worry about prefixing the command with any path parameters.
If we check the version, we can see I have it installed at version 3.1.2
The syntax for entering RSync is very simple.
RSYNC -Options Source Destination
For example, if I want to copy my Andy IFS directory to our Backups directory, I will use the command, and syntax as follows.
rsync -avhz /andy /backups/
As I said, every function of this utility starts with RSync, then we have the options parameter which is prefixed with a minus sign.
- a – Keep all the file attributes when copying, such as created, owner, security etc
- v – Is for verbose logging
- h – Is for a human readout. It will convert bytes into Gigabytes, use a thousand separator, etc.
- z – Is for compression
Personally, I always use this parameter set.
There are more options if you need them. They can be found on the RSync website, https://rsync.samba.org along with bags of examples.
After the options, we have the source directory and the destination directory, as shown in the figure below.
If the destination directory does not exist, RSync will create a directory for the destination automatically.
Synchronisation is a huge advantage of RSync, instead of blindly copying the whole contents of a directory, it analyses and transfer only the differences between the source and destination, saving the system resources, bandwidth and critically time.
If I create a new file in my ANDY directory, and call it ajax.txt, then try the same RSync command again, it will now only copy over the new file. I did not have to change any of the parameters to say do an incremental save, it worked it all out.
Transfer to Another Server
Another great feature of RSync is the ability to also save and restore files to the IFS of another IBM i. Just like the SAVRST green screen commands, we do not have to go through the rigmarole of saving, transferring, then restoring files, RSync does it all for us, with a slight change to the syntax.
If I wanted to save and restore my Andy directory onto one of our other partitions, I would use the following syntax.
rsync -zavh –progress /andy andy@proteus:/backups
By specifying a user ID and server name, RSync will use the secure SSH protocol to transfer files. Again, it will only copy over files that are new.
And to reverse the procedure, we could copy remote files/directories onto our local machine by prefixing the source directory with the SSH user/remote server details.
rsync -zavh andy@proteus:/backups/f_mtd /f_mtd
Using this procedure, we could easily schedule a job to perform a nightly backup automatically.
Another useful function of RSync is the ability to do a dry run of a copy.
I am sure you can all appreciate you could do a lot of damage with RSync, copying over to directories you should stay away from.
By adding the parameter –dry-run RSync will produce all the same output as a normal copy, indicating where files are going, but does not do any of the actual copying.
Try doing that with CP!
The syntax for this would be
rsync -avhz –dry-run /andy/ /test/
Once happy with it, remove the –dry-run parameter and away it goes and performs the copy you intended.
Another great addition to the open-source opportunities we now have on the IBM i, keep them coming IBM!
If you have any questions, either on this article, or anything else open-source, use the comments below, or send me a message on twitter @AndyYouens
Andy Youens is an IBM i consultant/instructor at Milton Keynes, UK-based FormaServe Systems with over 40 years IBM midrange experience.
Steve Munday says
Thanks for sharing, rsync is a great tool and wins hands down over cp especially when considering the “sync” capabilities.