一、实验要求
1. 备份机器需要每天凌晨1:03分定时同步MIS服务器的/app/java_project目录下的所有文件。
2. 要求记录同步日志,方便同步失败分析原因。
二、任务分析
1.使用crontab来编写计划任务
2.使用rsync来进行远程同步
3.因为是定时任务,rsync基于ssh服务,所以需要两台服务器设置ssh免密登录
三、实验拓扑图
四、实验环境介绍
IP为128的服务器为MIS服务器,存放代码的目录为/app/java_project,此目录需要去备份
IP为129的服务器为代码备份服务器,需要把128服务器上的代码备份到/backup/app/java_project
五、实验具体步骤
1.两台服务器配置ssh免密登录
需要129服务器能够免密登录到128上
[root@back ~]# ssh-keygen //生成密钥对 Generating public/private rsa key pair. Enter file in which to save the key (/root/.ssh/id_rsa): Created directory '/root/.ssh'. 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: 0d:8c:ee:fe:38:88:44:f9:1c:a3:8f:18:23:01:06:83 root@back The key's randomart image is: +--[ RSA 2048]----+ |= | |Eo o | |o . . o | |. o o. o | | o + o. S . | |+ o o. | |.= + .. | |. o o... | | oo. | +-----------------+ [root@back .ssh]# ssh-copy-id -i /root/.ssh/id_rsa.pub root@192.168.189.128 //把公钥远程拷贝到128主机上 The authenticity of host '192.168.189.128 (192.168.189.128)' can't be established. RSA key fingerprint is df:28:9d:09:a3:bf:52:a6:e5:ce:f2:a4:04:0d:b8:cc. Are you sure you want to continue connecting (yes/no)? yes Warning: Permanently added '192.168.189.128' (RSA) to the list of known hosts. root@192.168.189.128's password: Now try logging into the machine, with "ssh 'root@192.168.189.128'", and check in: .ssh/authorized_keys to make sure we haven't added extra keys that you weren't expecting.
[root@back .ssh]# ssh root@192.168.189.128 Last login: Sun Jul 21 22:45:44 2019 from 192.168.189.1 //测试ssh免密登录完成
2.开启rsync服务(MIS服务器上)
相关的文档可看https://www.cnblogs.com/feng0919/p/11223473.html
[root@MIS .ssh]# yum install rsync 已安装: rsync.x86_64 0:3.0.6-12.el6 完毕! [root@MIS .ssh]# vim /etc/rsyncd.conf [java] path = /app/java_project/ log file = /tmp/rsync.log [root@MIS .ssh]# rsync --daemon
3.通过脚本编写计划任务
[root@back ~]# vim rstnc.sh #!/bin/bash rsync -a 192.168.189.128::java /backup/app/java_project/ [root@back ~]# chmod +x rstnc.sh [root@back ~]# crontab -e no crontab for root - using an empty one 03 01 * * * /root/rsync.sh &>/dev/null