LoginSignup
1
0

More than 3 years have passed since last update.

CentOS8.2でSCPを利用する手順

Last updated at Posted at 2020-11-27
../

VPSサーバー(CentOS8.2)を構築しているが、既存の別サーバーからファイルをコピーしたい。方法はいろいろあるが、今回はscpコマンドを使った。大した内容ではないが、手順をメモしておく。

scpコマンドは、SSHプロトコルを使用するので、ホスト間でファイルを転送する場合にネットワーク通信は暗号化され、安全にファイルの転送を行える。

scpのインストール

SCP はopenssh-clientsパッケージに入っている。CentOS8ではインストール済みのようだ。


$ yum -y install openssh-clients

scpコマンドの使い方

コマンドの使い方は、以下の感じになる。ファイルやディレクトリをホスト間でコピーできる。from,toのどちらかには、ユーザー名とIPアドレスの指定が必要になる。-rオプションはサブディレクトリをリカーシブに処理し、-pオプションは属性(更新日時やアクセス権限)も合わせて保持させるかどうか。


$ scp -p  fromFile user@toIp:toDir
$ scp -pr fromDir user@toIp:toDir
$ scp -p  user@fromIp:fromFile toDir
$ scp -pr user@fromIp:fromDir toDir
$ scp -p  user@fromIp:fromFile user@toIp:toDir
$ scp -pr user@fromIp:fromDir user@toIp:toDir

実行例

例えば、稼働中の現行サーバー(157.xxx.xxx.xxx)にあるファイルをローカルサーバーの作業領域(/var/share9)にコピーしてみる。taconanaユーザーで行ってみる。MySQLのバックアップ用のシェルファイルと最新のバックアップファイルをコピーしている。


$ mkdir /var/share9
$ chmod 666 /var/share9

$ scp -p taconana@157.xxx.xxx.xxx:/var/share7/*.sh /var/share9
The authenticity of host '157.xxx.xxx.xxx (157.xxx.xxx.xxx)' can't be established.
RSA key fingerprint is SHA256:XTzxuKcaiqXUfexxxxxxxxxYXExceI9Ryscjs.
Are you sure you want to continue connecting (yes/no/[fingerprint])? yes
Warning: Permanently added '157.xxx.xxx.xxx' (RSA) to the list of known hosts.
taconana@157.xxx.xxx.xxx's password: ***
mysql-backup.sh                                  100%  537   630.0KB/s   00:00
$ ls -lag /var/share9
-rwxr-xr-x  1 root  537 Oct 14  2017 mysql-backup.sh

$ mkdir /var/share9/mysql-backup
$ chmod 666 /var/share9/mysql-backup

$ scp -p taconana@157.xxx.xxx.xxx:/var/lib/mysql/backup/backup-20201127.tar.gz /var/share9/mysql-backup
taconana@157.xxx.xxx.xxx's password: ***
backup-20201127.tar.gz                           100%   13MB   6.0MB/s   00:02
$ ls -lag /var/share9/mysql-backup
-rw-r--r-- 1 root  13813392 Nov 26 11:45 backup-20201127.tar.gz

以上

../
1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0