Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Linux Timesaving Techniques For Dummies.pdf
Скачиваний:
59
Добавлен:
15.03.2015
Размер:
15.98 Mб
Скачать

Combining the Power of tar with ssh for Quick Remote Backups

387

Combining the Power of tar with ssh for Quick Remote Backups

tar is an archiving tool that can create new archives, display the contents of an existing archive, or extract data from an archive. An archive is just a simple file that contains other files within it. Archives are handy because they let you work with (e-mail, copy, or back up, for example) a whole collection of files with ease. When you use tar to create an archive, tar copies the contents of each file into the archive, along with some bookkeeping information such as the file owner, file permissions, and modification date. The Linux version of tar (actually, the GNU version included with Linux) can compress an archive on the fly, and the compressed archive is usually smaller than the sum of its parts.

tar by itself is a useful command, but add the power of ssh, and you’ve got a great combination that makes it easy to back up your data to a remote system. You can also use tar and ssh to access tape drives mounted on remote machines.

Testing the ssh connection to the remote host

tar and ssh are installed with most Linux distributions by default — you already have a working copy of each program on your system. Before you try to use tar and ssh together, test the ssh connection to the remote host:

$ ssh remotehost

If the ssh connection is good, you’re prompted for your password.

It’s unlikely that you’ll get connection errors, but if you do, you need to resolve them before you can continue.

Creating a tar archive over the ssh connection

After you’ve tested the ssh connection, you’re ready to go. To create a tar archive over an ssh connection, use the following command:

$tar -zcv -f - /source | ssh remotehost “cat > targetfile.tgz

Replace the source and destination names with your own. Then press Enter to start the backup.

Here’s a closer look at the preceding command:

tar -zcv -f -: Creates a tar archive, using the following options:

z: Create the archive in compressed gzip format.

c: Create a tar archive.

v: Use verbose mode. Display progress on the screen.

-f -: The -f archive option tells tar where to write the archive. In this case, you want tar to write the archive to the standard output stream (which you’ll connect to the standard input stream of the ssh command with the pipe character | ).

/source: Specifies the files and/or directories that should be added to the tar archive. You can add multiple files or directories to the archive by listing each filename (or directory name), separated by a space:

$ tar zcvf - /home /usr | ssh remotehost

“cat > targetfile.tgz

The preceding command creates an archive (on the machine remotehost) containing the

contents of the /home directory and the contents of the /usr directory.

|: Pipes the standard output from the first command into the standard input for the second command.

ssh remotehost: Opens an encrypted connection to the remote computer.

cat > targetfile.tgz: Receives the data coming through the standard input of the ssh command and directs it, byte-by-byte, to the file named targetfile.tgz. This creates a duplicate of the archive on the remote host, in the filename specified.

Соседние файлы в предмете Операционные системы