Transfer files between two Linux machines (details + bash script)

Direct file transfer between two Linux devices has many application scenarios

1. Possible solutions

(1) Download first and then upload

Download from linux to windows via ssh, and then upload to another linux via ssh.

1. Advantages: simple
2. Disadvantages: low efficiency, requiring additional equipment

(2) http server

For example, through nginx, the settings directory that needs to be downloaded, and another linux use wget to download

1. Advantages: Relatively simple
2. Disadvantages: It needs to be set every time. If the directory is set too broadly, the security will be worrying.

(3) rsync

rsync can be used with some tools to achieve real-time synchronization. You can refer to this article:
rsync+lsync real-time backup
Simply using rsync is relatively simple a>
1. Advantages: Relatively simple
2. Disadvantages: It also requires setting the directory, and the operation is not flexible. And rsync eats resources

(4) secure copy (scp)

This is a better method, equivalent to direct connection through ssh.

2. SCP specific operations

1.Install scp

apt-get install openssh-client

or

dnf install openssh-clients

2.Specific operations

Configuration 1: 192.168.0.100
Configuration 2: 192.168.0.200

Copy local files to another device
Copy file.ini from the /aaa directory of device 1 to the /bbb directory of device 2, log in as root: < /span>

scp /aaa/file.ini [email protected]:/bbb/

Copy another device to the local device
Copy the file.ini of the /bbb directory of device 2 to the local /aaa directory, and log in as root:

scp [email protected]:/bbb/file.ini /aaa/

When connecting for the first time, you will be prompted whether to continue the connection, select yes, and then enter the password. You will need to set a password every time thereafter

3. How to automatically fill in passwords

You need to re-enter your password every time you use it, and you need to enter it twice
Installation

apt-get install sshpass
yum install sshpass

Add sshpass -p ‘password’ to the above command, and replace the password in quotation marks with the login password.

sshpass -p 'password' scp /aaa/file.ini [email protected]:/bbb/

3. bash script

1.Create script file

vi /etc/scp.sh

Note that the constants in the script below are changed to your actual situation. There are instructions for using the script below:

#!/bin/bash

# 常量设置区域(也可以改造成参数)
remote_ip="192.168.0.200" # 连接的设备地址
login_user="root" # 登录的账户
password="aaabbbccc" # 远程root的密码

# 运行区域
if [ $1 == "send" ];
then 
	# 发送文件/文件夹
	if [ -d "$3/$2" ]; then
		sshpass -p $password scp -r $3/$2 $login_user@$remote_ip:$4/
	else
		sshpass -p $password scp $3/$2 $login_user@$remote_ip:$4/
	fi	
elif [ $1 == "get" ]; then
	# 下载文件/文件夹
	if [ $5 ]; then
		if [ $5 == "t" ]; then
			# 文件夹
			sshpass -p $password scp -r $login_user@$remote_ip:$4/$2 $3/
		else
			# 文件
			sshpass -p $password scp $login_user@$remote_ip:$4/$2 $3/
		fi
	else
		# 文件
		sshpass -p $password scp $login_user@$remote_ip:$4/$2 $3/
	fi
else
	echo "请输入传输方式"
fi

Grant execution permissions

chmod +x /etc/scp.sh

2. Operation and instructions

I use this script like this
The first parameter: transmission method, receive (get) or send (send)
The second parameter: File name or directory name (without upper directory)
Third parameter: local directory (without following /)
Fourth parameter: remote directory (Without the trailing /)
The fifth parameter (optional): whether the upload is a directory, yes t, no f (effective when downloading, local judgment can be made when uploading, so there is no need to write this parameter)


Send file.ini from the local /aaa directory to /bbb/ on the remote host:

/etc/scp.sh send file.ini /aaa /bbb

transfer folder

You need to enter the command manually for the first time. After the first prompt to enter yes, you can use the script later.
This script can be further encapsulated and then called.

Guess you like

Origin blog.csdn.net/ziqibit/article/details/133818085