Synchronizing your Linux Laptop and Desktop - page 2
Transferring Single Objects
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.