If your laptop computer is a complement to your desktop machine, you’re probably well aware of the need to synchronize data between the two. When you’re in the middle of a big project and know you’re going to be spending the afternoon in a doctor’s waiting room or on an airplane, it’s nice to be able to quickly transfer your project files–and maybe your email and contacts–to the laptop, then vice-versa when you return to your desk. This article will show you two ways to accomplish this on GNU/Linux-based machines.
If you only have a handful of individual files or directories to transfer, it’s probably easiest to send them one at a time via secure shell (OpenSSH). Every GNU/Linux distribution includes OpenSSH, but not all of them run the SSH daemon (which allows you to connect to the computer via SSH) by default. Starting the daemon is as simple as typing sshd
in a root terminal. You can also configure your distribution to start OpenSSH at boot time; each distro handles startup scripts differently, so you’ll have to consult your distro’s documentation to learn how to do that.
Once the secure shell daemon is started, other computers can connect to your machine from a terminal window by using the ssh
command, or you can copy files over a secure connection by using scp
. The latter works just like the regular cp
command does, except you have to give it an address for at least one of the files:
scp picture.jpg 192.168.1.101:/home/user/pictures/
In the above example, a file called picture.jpg is transferred to the network machine with the address 192.168.1.101 in the /home/user/pictures/ directory. By default, scp
copies the target file or directory to the home directory of whatever user you’re logged in as; you can specify a different location by adding the path after the colon following the address.
By using your /etc/hosts file, you can create a nickname for the remote machine you’re copying files to. Just start a new line in the file, type in the IP address of the machine you want to nickname, then press the tab key once and type in the name you’d like to call it:
192.168.1.101laptop
In the next example, we’ll use the laptop nickname instead of the address, specify a different user than the one currently logged into the terminal we’re copying from, and copy an entire directory instead of just one file:
scp -r /home/user/pictures/ user2@laptop:/home/user2/
The -r switch means recursive, which tells scp
to copy the directory and everything in it. The above command will create a pictures directory to the /home/user/ directory on the laptop computer, and copy all of the contents of the local machine’s /home/user/pictures/ directory to it. So what do you do if the remote machine already has a /home/user/pictures/ directory, but you still want to copy everything in the local machine’s pictures directory? You use a wildcard:
scp /home/user/pictures/* laptop:/home/user/pictures/
There are many networking technologies that could be used to transfer a large number of files in several directories (CVS, FTP, NFS), but for what we’re doing, most of them don’t make as much sense as rsync.
rsync is a lot like scp, except it’s designed for doing complex transfers. If your laptop and desktop computer share pretty much the same software, /home directory structure, and data, rsync will intelligently update it. If rsync finds duplicate files on the remote machine, it will check to see which file is newer and update the remote file if it is out of date. If the file or directory doesn’t exist, rsync creates it. You can also set rsync to delete any files on the remote machine that are not detected on the local machine, but this can be dangerous, so we won’t use that switch in any examples.
Like with OpenSSH, in order to use rsync for file transfers, you must start a daemon on the remote machine. The command is rsyncd
, and you can of course add it to your operating system’s startup script if you like. The examples below do not require the rsync daemon; they use OpenSSH for the file transfers instead, so you’ll have to run the SSH daemon. You can specify which software to use for the file transfers by using either one (SSH) or two (rsync) colons after the remote host name or IP address. The examples that follow all use one colon.
The simplest way to use rsync is to synchronize the /home directories of two computers that have the same users and file system structures:
rsync -arvuz /home/user/ 192.168.1.101:/home/user/
Just like with SSH, you can use /etc/hosts to create a nickname for the IP address of the remote machine, and you can also specify other users with the @ symbol. The -arvuz flags mean (in order) that you’re going to keep user and group file permissions; recursively copy the /home/user/ directory and all files and directories therein; verbosely show which files are transferred or updated; ignore identical files that have the same timestamp; and compress the data to use less bandwidth over the network.
What if you only want to transfer important files and directories–not the whole /home/user/ directory? There are two ways. If you’re copying the same directories in your /home dir each time, you can create a simple script to save yourself all of the typing. Open up your favorite text editor and create a file called sync_laptop.sh
and put this into it, substituting your own directories for the ones in the example:
rsync -arvuz '/home/user/pictures /home/user/documents /home/user/jokes' laptop:
When it’s saved, make it executable with chmod +x
. The above command won’t work with directories that are more than one level in, so /home/user/pictures/summer/ won’t work. Neither can you copy files to anywhere other than the remote /home directory using the above example. You could add a new line to the script for each buried directory that you want to transfer, but there’s a more efficient way to do it.
First, create a directory in your user’s home dir called sync. Change to the sync directory and create symlinks to all of the directories that you want to transfer. Make sure you treat the sync dir as though it were your home directory when you create the destination links. You may have to create directories within the /home/user/sync/ dir if they are more than one level in:
ln -sf /home/user/documents ./documents mkdir .gconf mkdir .gconf/apps ln -sf /home/user/.gconf/apps/evolution ./.gconf/apps/evolution ln -sf /home/user/.evolution ./.evolution mkdir pictures mkdir pictures/summer ln -sf /home/user/pictures/summer ./pictures/summer
Now create the script that will do the transfers; call it sync_laptop.sh and put it in your user’s home directory:
# This script syncs a remote computer to this one cd /home/user/ # Uncomment the next command if you'd like to copy all of # the files (not directories) in your home dir to the remote machine. # cp * ./sync rsync -arLuvz /home/user/sync/ laptop:/home/user
The -L switch tells rsync to treat your symlinks as though they were real directories.
Save the script, then make it executable by using chmod +x
on it. When you run the script, your laptop will be updated with all of those files and directories from your desktop machine. The first time you do this it will take a while, but each subsequent sync will take less time because rsync will not overwrite files that have not changed.
To reverse the process, do the same thing for your laptop computer, but remember to change the IP address or nickname of the remote machine.
By the way–the above example will copy over your email accounts, address books, saved email, and all other Novell Evolution data to the remote machine. If you’re setting up a new laptop computer, this can make moving your Evolution data much simpler.
The examples and advice above are simplified for home use. Both OpenSSH and rsync are capable of much more advanced tasks. There are also different techniques and approaches for the processes outlined in our examples. The first and best place to look for more information on these programs is their respective manual pages. If you’re looking for more examples than the ones here and in the man pages, a Google search for the exact switches and options you want to use will turn up more information.
Jem Matzan is an experienced electronics technician, freelance technology journalist, and the editor-in-chief of The Jem Report, Entertainment in Review, Hardware in Review and Software in Review.