1、在需要免密的机器上生成rsa,如127.0.0.1
ssh-keygen -t rsa
2、将id_rsa.pub文件同步的另外一台机,如127.0.0.2,并且导入authorized_keys
cat ~/id_rsa.pub >> ~/.ssh/authorized_keys
完成以上操作后,可以在127.0.0.1的机器上免密传输127.0.0.2 的文件。
如果之前有做过同步,可能会报错,遇到这种错误的时候,把127.0.0.1 上的know_hosts 文件中对应的127.0.0.2 这个IP的key删除即可。
ECDSA host key for 127.0.0.2 has changed and you have requested strict checking.
附python同步代码
#!/usr/bin/env python
# encoding: utf-8
import datetime
import os
import shutil
import subprocess
import time
# 备份目录
backup_dir = "/root/db_backup"
# backup_prefix和backup_suffix分别为备份文件的前缀和后缀
backup_prefix = "db_backup"
backup_suffix = "%Y-%m-%d-%H"
# 获取备份文件名
def get_backup_filename():
t = time.strftime(backup_suffix, time.localtime())
return "%s_%s" % (backup_prefix, t)
cmd_template = "scp 127.0.0.2:/root/db_backup/{fileName}.zip /root/db_backup/"
# 备份指定数据库
def backup_database(fileName):
d = {
"fileName": fileName,
}
cmd = cmd_template.format(**d)
subprocess.call(cmd, shell=True)
# 备份数据库
def backup():
fileName = get_backup_filename()
try:
backup_database(fileName)
finally:
print('scp finished')
#shutil.rmtree(backup_path)
if __name__ == "__main__":
try:
backup()
finally:
print('scp>>>')