在系统运维工作中,难免会有不同操作系统之间进行数据同步或数据备份的场景,本文主要介绍通过SSH从Windows系统备份数据到Linux系统的实现过程。
一、Windows 安装openssh
1、下载微软开源的openssh
下载最新的二进制版本: Releases · PowerShell/Win32-OpenSSH · GitHub
2、解压到C:\Program Files并重命名为OpenSSH
3、启动Window PowerShell 并进入C:\Program Files\OpenSSH目录
4、输入以下命令安装sshd和ssh-agent服务
powershell -ExecutionPolicy Bypass -File install-sshd.ps1
5、打开防火墙,开启22端口
netsh advfirewall firewall add rule name=sshd dir=in action=allow protocol=TCP localport=22
6、设置开机启动
Set-Service sshd -StartupType Automatic
Set-Service ssh-agent -StartupType Automatic
7、启动服务
Start-Service sshd
Start-Service ssh-agent
8、连接测试
使用SSH连接工具测试连接情况。
二、Linux机器生成建立安全信任关系的证书
1、执行ssh-keygen -b 1024 -t rsa命令生成证书
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: ……
注意:提示输入passphrase时直接输入回车,表示无证书密码。上述命令将生成私钥证书id_rsa和公钥证书id_rsa.pub,存放在用户目录的.ssh子目录中。
2、将公钥证书id_rsa.pub复制到Windows机器文件C:\ProgramData\ssh\administrators_authorized_keys中(若无文件手动创建)
3、在Windows机器系统服务中重启OpenSSH SSH Server和OpenSSH Authentication Agent服务
5、测试免密登录情况
ssh [email protected]
三、免密备份windows文件
备份脚本:
nohup scp -r -l 20000 '[email protected]:d:/"Magic Winmail"/server/archive/*.zip' "/data/10.x.x.x/Magic Winmail/server/archive" &
以上脚本实现免密限速备份windows机器d:/Magic Winmail/server/archive/*.zip文件到linux机器的/data/10.x.x.x/Magic Winmail/server/archive目录。
注意:
scp路径如有空格,需要引号括起来,同时整个路径需要用单引号。