如何使用SSH连接到远程主机,并创建一个Bash脚本,以便每天将所有文件和文件夹从旧服务器复制到新服务器进行备份?
发布于 2018-04-02 04:50:37
。
首先,您需要生成一个ssh键。在你连接的机器上,运行:
$ ssh-keygen
Generating public/private rsa key pair.
Enter file in which to save the key (/home/vidarlo/.ssh/id_rsa):
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /home/vidarlo/.ssh/id_rsa.
Your public key has been saved in /home/vidarlo/.ssh/id_rsa.
The key fingerprint is:
SHA256:/jxfxiWiao0m7YG9MiHgXBFKoo7kJcgTOrPtAZNtpVg vidarlo@hannah.bitsex.no
The key's randomart image is:
+---[RSA 2048]----+
|..E o. |
|=B.+. |
|@==. . |
|=O= . |
|o=oo S . . .|
| .o.. .+ . o o |
| . ..o+o. + |
| + =*o o |
| B+ oo. |
+----[SHA256]-----+
[~]$
询问时只需按enter;默认位置和密码没有问题。
这将生成一个私钥和公钥。下一步是将公钥复制到远程服务器,以便可以使用它。ssh-copy-id
可用于此:
$ ssh-copy-id user@host
/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/home/vidarlo/.ssh/id_rsa.pub"
/usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed
/usr/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys
user@host's password:
Number of key(s) added: 1
Now try logging into the machine, with: "ssh 'user@host'"
and check to make sure that only the key(s) you wanted were added.
在这个阶段,您应该能够运行ssh user@host
,并在不输入密码的情况下登录。
你想要一个简单的scp。这是不好的,有几个原因:
但是无论如何。这是可以做到的,只要你知道这些警告。使用crontab -e
编辑用户crontab。插入如下一行:
0 5 * * * /usr/bin/scp "/path/to/backup" "user@remote:/path/to/store/backups"
此命令将在05:00夜间运行。如果你愿意的话,这是可以改变的。对这些领域的解释如下:
https://askubuntu.com/questions/1021302
复制相似问题