Log in 22 after tar compress/var/log directory output to standard input via pipeline to local 22_log.tar.gz file ssh 192.168.0.22 "Cd/var; Tar-zcvf-log"|cat >22_log.tar.gz re-creates the file in a relative location when extracting the archive file. You can use this method to help synchronize the directory. Because Tar creates a byte stream of the directory structure, you can copy the file from one location to another by using a combination of tar and pipelines: $ tar CF-./etc | (cd/backup; tar XF-). "-" specifies that tar should use standard output (at write time) or standard input (at read time). Parentheses let the statement execute in a child shell. Take a look at the code in front of the pipe symbol, which creates a byte stream of the file and sends it to standard output. After the pipe break, switch to another directory, and then extract the byte stream from the standard input.
To preserve the owner and permissions of the file, you can use the P option to preserve metadata for each file and directory: $ tar CFP-. etc | (cd/backup; tar xfp-).
Once you have mastered this basic structure, you can perform more complex operations. For example, you can copy only files that have been modified after a specific time: $ tar CF---newer 20090101./etc | (cd/backup; tar XF-). This command creates a copy of the file that was modified after January 1, 2009.
by using with rsh or SSH, you can also sync files to a remote host: $ tar CFP-./etc | (Ssh [email protected] --tar xfp-). Using SSH and tar in this way is a good way to create a local file backup on a remote host. However, there are more efficient ways of synchronizing information. )
1, after a machine compression, copy to the B machine and unzip cd/opt ; Tar-cf-datadir1 | SSH 192.168.0.116 "cd/opt; TAR-XF-" 2, in a machine compression, and copy the compressed file to B machine cd/opt; Tar-cf-datadir1 | SSH 192.168.0.116 "cd/opt; Cat-> Datadir1.tar " 3, copy the compressed file of a machine to B machine and unzip Zcat Dir.tar | SSH 192.168.0.116 "cd/opt; TAR-XF-" |
Transfer to remote: Tar czf-file| SSH server "tar zxf-"
Compress to remote: Tar czf- file| SSH server "cat > file.tar.gz"
Unzip to remote: SSH server "tar zxf-" < file.tar.gz
Unzip to Local: SSH server "cat file.tar.gz" | Tar zxf-
-----------------------------------------------------------------
$target means the target machine, the remote machine.
$backup _server means the backup machine, also refers to the remote machine
$save refers to the backup directory
Require remote backup and pull back to local
SSH $target tar-cf-/--exclude/mnt | bzip2-9 | Cat > $target. tar.bz2
Unlock the local backup and go back to the remote
BUNZIP2-DC $target. tar.bz2 | SSH $target "CD/;TAR-PXKF-"
Back up local data, drop to remote to compress and archive
Tar CF-/--exclude XXX | SSH $backup _server "bzip2-9 > $save/test.tar.bz2"
Pull the remote data back to local for decompression action
SSH $backup _server "cat $save/test.tar.bz2" | BUNZIP2 | TAR-XPKF-
The local script is placed in remote execution:
Cat myscript.sh | SSH kramer/bin/sh
Hostname
Date
W
Ifconfig
Ps-ef | grep http | awk ' {print $NF} '
Long pipe command in remote (this same can be done with the above script)
- #! /bin/sh
- SSH 172.16. 1. 2 <<eof< div= "" >
- sa= '/usr/sbin/alternatives--config java 2>&1 '
- echo "Result:"
- echo "$sa"
- Echo
- echo "Would I find anything when I look in so result for Jdk1.6.0_05???"
- echo "$sa" | grep "jdk1.6.0_05"
- echo "Nope. I thought not! But the reason should is clear now! "
- Eof
SSH Tips
Remotely compare files
SSH remote-hosts Cat Memos/memo.draft | Diff Memos.new-Cat Memos.new | SSH speedy Diff-memos/memo.draft#diff < (ssh-n George CAT/ETC/PASSWD) < (ssh-n Kramer CAT/ETC/PASSWD) package files and pass them directly To the remote directory to be unpacked (not locally generated files)
Tar cf-some_directory | SSH Kramer "(cd/path/to/destination; Tar XF-) "
SSH 192.168.100.166 tar-cf-/var/www/html/centos/5/repodata |cat > Tt.tar
SSH 192.168.10.145 tar-czf-'--exclude=*.gz '--exclude=*.tgz '--exclude=*.tar '/a8root/phpweb/website/images
Get the remote file tar directly and keep it locally
SSH 192.168.100.166 "Find/var/www/html/centos/5/repodata-depth-print | Cpio-oav "| Cat > Tt.cpio Reference documentation for exclusions: http://www.android100.org/html/201407/28/47322.html
SSH combines the tar command to pull the remote file back or push it over (no landing data is pushed)