Using scp to copy files remotely in Linux

Source: Internet
Author: User
Tags apache log file apache log scp command
In Linux, copy files remotely using scp-Linux Enterprise Application-Linux server application information. The following is a detailed description. Use "scp" to copy an object

SSH provides instructions and shell for logging on to the remote server. By default, it does not allow you to copy files, but still provides a "scp" command.

Suppose you want to copy a file named "dumb" in the current directory of the Local Computer to your home directory on the remote server www.foobar.com. The name of your account on the remote server is bilbo ". You can use this command:

Scp dumb bilbo@www.foobar.com :.

Copy the file back and use this command:

Scp bilbo@www.foobar.com: dumb.

"Scp" calls SSH to log on, copy the file, and call SSH to close the connection.

If your "~ The/. ssh/config file has already configured www.foobar.com as follows:

Host * fbc
HostName www.foobar.com
User bilbo
ForwardAgent yes

Then you can replace "bilbo@www.foobar.com" with "fbc", and the command is simplified to "scp dumb fbc :.".

"Scp" assumes that your home directory on the remote host is your working directory. If you use a relative directory, it is relative to the home directory.

The "-r" parameter of the "scp" command allows recursive directory copying. "Scp" can also copy files between two different remote hosts.

For more information about SSH, see the http://www.linuxaid.com.cn/engineer/brimmer/html/ssh.htm

Scp user @ host:/path/to/files local_file_name

Copy to remote

Scp local_file_name user @ host:/path/to/files

Ftp:

Wget ftp://ftp.isc.org/isc/bind9/9.2.2/bind-9.2.2.tar.gz

Http://blog.chinaunix.net/u/8983/showart.php? Id = 55609

Http://www.unix206.com/8/166189.html

Scp command

The scp command is the most convenient and useful command in SSH. Just use the scp command to directly transfer files between two servers. You can run it as root on a server # scp servername:/home/ftp/pub/file1. in this way, the file/home/ftp/pub/file1 on the other server is directly transferred to the current directory of the machine. Of course, you can also use # scp/tmp/file2 servername: /boot sends/tmp/file2 files on the local machine to the/boot directory of another machine. The entire transfer process is still encrypted using SSH.



Use openssh to remotely execute commands --- automatically download apache Log Files


Application. Everything is an application!

This article introduces two points: one is to use ssh to directly log on to other machines (no password is required), and the other is to configure apache to automatically switch log files at intervals. This is all introduced through an example of autosftp. sh. v2 script.


Use openssh to remotely execute commands --- automatically download apache Log Files

I. Preparations

Objective: To automatically download apache logs from multiple machines to a local machine for analysis

Background:
1. There are three web hosts, web1 web2 web3 are solaris systems
2. The local machine traffic is linux 7.3.
3. The entire process requires automatic execution without manual intervention.
4. Consider using Secure ssh protocol to transmit log files

Ideas:
1. Configure the ssh tool between traffic and web host to log on directly
2. Configure apache on the web host to automatically switch logs every day without writing logs to the previous day.
3. Execute a script on traffic to automatically compress the log files on the web host, and then download the files to the local device.
4. Run the local command traffic to decompress the downloaded log file.

II. Implementation Process

1. Configure the ssh tool between traffic and web host to log on directly

1.1 edit the traffic hosts file to add web host entries

......
203. 81. *. * web1
203. 81. *. * web2
203. 81. *. * web3
1.2 generate an ssh client key

# Ssh-keygen-t rsa

Screen Display (to generate a public key file and so on, press enter all)
Generating public/private rsa key pair.
Enter file in which to save the key (/root/. ssh/id_rsa ):

Enter passphrase (empty for no passphrase ):
Enter same passphrase again:
Your identification has been saved in/root/. ssh/id_rsa.
Your public key has been saved in/root/. ssh/id_rsa.pub.
The key fingerprint is:
D1: 69: 34: 80: 56: 2a: 2d: df: 70: 2d: 10: ac: 63: 5e: 1e: 1c root @ web

After 1.3 is generated, copy the local id_rsa.pub to the remote./ssh directory and change the name to authorized_keys2.

Sftp root @ web1
Sftp web1
Connecting to web1
Root @ web1's password: (enter the password)
Sftp>
Sftp> cd. ssh
Sftp> put id_rsa.pub authorized_keys2
Uploading id_rsa.pub to/. ssh/authorized_keys2

1.4 test successful

Scp web1:/usr/local/apache2/logs/hoho/root/copy remote hoho files to the local/root/. ssh directory without entering a password
Hoho 100% 40KB 40.2KB/s)
1.5 addendum or annotation

A. When you log on to web1 for the first time, ssh will prompt you to add web1 to your know_hosts.
B. Direct login verification distinguishes users. The authorized_keys2 file is divided into users.

2. Configure daily automatic switch log files for web hosts

Add the following two sentences to the configuration file. If there is a VM, add it to the configuration of the VM.

CustomLog "|/usr/local/apache2/bin/rotatelogs/usr/local/apache2/logs/mytestsite-access_log. % Y % m % d 86400" combined

TransferLog "|/usr/local/apache2/bin/rotatelogs/usr/local/apache2/logs/mytestsite-access_log. % Y % m % d 86400

3. Script automatically executed on traffic

#! /Bin/bash
# This script is for auto download the log life of apache
# Editor anwei, green
# Version 2.0
# Add 2 parameters GateWay & LogFile
# Add more information for debug
# Add The starting time & the ending of time

# Step 1: parameters setting, add default gateway
Dest_dir =/data/logs
Sour_dir =/usr/local/apache2/logs
LogFile = mytestsite-access_log
Host_table = "web1 web2 web3"
Date = 'date + % Y % m % d-d'-1 Day''
GateWay = 192.168.1.1
Echo Beginning: 'date + '% Y/% m/% d % H: % m''
/Sbin/route add default gw $ GateWay
# Step 2: compress log file

For loop in $ Host_table

Do

Ssh $ loop "/usr/bin/gzip $ Sour_dir/$ LogFile. $ Date" & echo $ loop: compress apache log file complete.

Done

# Step 3: download form web server

For loop in $ Host_table

Do

Scp $ loop: $ Sour_dir/$LogFile.$Date.gz $ Dest_dir/$ loop & echo $ loop: download apache log file complete.

Done

# Step 4: uncompress log files

For loop in $ Host_table

Do

/Bin/gzip-df $ Dest_dir/$ loop/export logfile..gz
/Bin/cat $ Dest_dir/$ loop/$ LogFile. $ Date> $ Dest_dir/$ loop/$ LogFile & echo $ loop: log files Ready now.

Done

/Sbin/route delete default gw $ GateWay
Echo End: 'date + '% Y/% m/% d % H: % m''

4. automatically execute the cron task deployed on the traffic

15 8 */tools/autosftp. sh. v2>/tools/sftp. log 2> & 1


5. Check the content of sftp. log every day to view the script execution status.

The execution has been completed successfully for one week. The following is a piece of sftp. log.

Beginning:
Web1: compress apache log file complete.
Web2: compress apache log file complete.
Web3: compress apache log file complete.
Web1: download apache log file complete.
Web2: download apache log file complete.
Web3: download apache log file complete.
Web1: log files Ready now.
Web2: log files Ready now.
Web3: log files Ready now.
End: 2005/11/06 08:46
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.