|
Synchronizing your Linux Laptop and Desktop
Transferring Single ObjectsIf 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 Once the secure shell daemon is started, other computers can connect to your machine from a terminal window by using the 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, 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.101 laptop 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 /home/user/pictures/* laptop:/home/user/pictures/
Complex TransfersThere 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 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 rsync -arvuz '/home/user/pictures /home/user/documents /home/user/jokes' laptop: When it's saved, make it executable with 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 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.
Further ReadingThe 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.
|