备份说明
备份目录
- /boot
- /etc
- /home
- /root
- /usr/local
- /var
客户端
- 114.212.239.114(Nova1)
服务器
- 114.212.235.54(Host)
环境
- 服务器上都有rsync
- 服务器都已启动SSH
- 两台装有Centos7的服务器
1.建立可以不用密码可以登录的SSH用户
参考:鸟哥的Linux私房菜-服务器架设篇
基本原理请参考上述资料。
1.1 Host建立两把钥匙
使用Rsync备份采用推的方式,所以Host要主动去联系Nova1,所以Nova1要验证Host的身份,这样的话Host要先和Nova1对好身份,在Host上产生私钥(id_rsa)和公钥(id_rsa.pub)。并且把公匙传给Nova1。
#用默认的方法建立密钥
[root@114-212-235-54 ~]# 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:
78:da:29:70:a3:7b:cc:1c:fe:46:00:60:8f:b0:7a:7f root@114-212-235-54.nju.edu.cn
The key's randomart image is:
+--[ RSA 2048]----+
|. o. |
| + o. |
|. . .. |
|. .. |
|. . . +.S |
| . . +.=.. |
| o=Eoo |
| o*.. |
| .. o. |
+-----------------+
#后两者为产生的私钥和密钥
[root@114-212-235-54 ~]# ls -ld ~/.ssh;ls -l ~/.ssh
drwx------. 2 root root 36 10月 28 22:01 /root/.ssh
总用量 8
-rw-------. 1 root root 1675 10月 28 22:01 id_rsa
-rw-r--r--. 1 root root 412 10月 28 22:01 id_rsa.pub
#设置权限,简单的来说root有rwx权限,别人啥权限也没有,那么在对比过程中不会被Nova1认为是危险的,而且自己有所以权限。
[root@114-212-235-54 ~]# chmod 700 ~/.ssh/
1.2 将公钥文件传到Nova1服务器上
[root@114-212-235-54 ~]#scp ~/.ssh/id_rsa.pub root@114.212.239.114:~
The authenticity of host '114.212.239.114 (114.212.239.114)' can't be established.
ECDSA key fingerprint is 50:cb:d5:89:6c:3d:99:0a:5e:56:9f:f0:ad:11:c1:cb.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '114.212.239.114' (ECDSA) to the list of known hosts.
#此处就需要密码验证,你验证了一次,以后就不用验证了。
root@114.212.239.114's password:
id_rsa.pub 100% 412 0.4KB/s 00:00
1.3 将公钥放在Nova1下正确的文件目录和文件名
#要放到~/root/.ssh文件下,目前处于/root目录下,查找不存在,故创建
[root@114-212-239-114 ~]# ls -ld .ssh
ls: 无法访问.ssh: 没有那个文件或目录
[root@114-212-239-114 ~]# mkdir .ssh;chmod 700 .ssh
[root@114-212-239-114 ~]# ls -ld .ssh
drwx------. 2 root root 6 10月 28 22:05 .ssh
#查看,Host的公钥是传过来了。
[root@114-212-239-114 ~]# ls -l *pub
-rw-r--r--. 1 root root 412 10月 28 22:03 id_rsa.pub
#把数据加到authorized_keys中,并且保证文件权限正确
[root@114-212-239-114 ~]# cat id_rsa.pub >> .ssh/authorized_keys
[root@114-212-239-114 ~]# chmod 644 .ssh/authorized_keys
[root@114-212-239-114 ~]# ls -l .ssh
总用量 4
-rw-r--r--. 1 root root 412 10月 28 22:06 authorized_keys
1.4 测试Host是不是可以连接到Nova1
[root@114-212-235-54 ~]# ssh root@114.212.239.114 "ls -l"
总用量 132296
-rw-------. 1 root root 810 5月 11 17:27 anaconda-ks.cfg
drwxr-xr-x. 4 root root 4096 9月 30 15:30 elasticsearch
-rw-r--r--. 1 root root 412 10月 28 22:03 id_rsa.pub
drwxr-xr-x. 3 root root 45 5月 26 05:58 python
-rwxr-xr-x. 1 root root 135457249 5月 11 13:10 xampp.run
可以在Host上不用密码连接到Nova1了。
2 使用rsync实现自动备份
参考:鸟哥的Linux私房菜-服务器架设篇
写脚本实现自动备份,具体语法去网上自行查阅
[root@114-212-235-54 ~]#mkdir ~/bin
[root@114-212-235-54 ~]# vim ~/bin/backup_nova1.sh
#!/bin/bash
localdir=/home/Nova1
remotedir="/boot /etc /home /root /usr/local /var"
remoteip=114.212.239.114
id=root
[ -d ${localdir} ] || mkdir ${localdir}
for dir in ${remotedir}
do
rsync -av -e ssh ${id}@${remoteip}:${dir} ${localdir}
done
[root@114-212-235-54 ~]#chmod 755 ~/bin/backup_nova1.sh
再建立Crontab工作,在每天的00:30执行脚本实行自动备份(在建立crontab工作前可以先试试脚本是否可以运行)
[root@114-212-235-54 ~]# vim /etc/crontab
SHELL=/bin/bash
PATH=/sbin:/bin:/usr/sbin:/usr/bin
MAILTO=root
# For details see man 4 crontabs
# Example of job definition:
# .---------------- minute (0 - 59)
# | .------------- hour (0 - 23)
# | | .---------- day of month (1 - 31)
# | | | .------- month (1 - 12) OR jan,feb,mar,apr ...
# | | | | .---- day of week (0 - 6) (Sunday=0 or 7) OR sun,mon,tue,wed,thu,fri,sat
# | | | | |
# * * * * * user-name command to be executed
30 0 * * * root sh ~/bin/backup_nova1.sh
~
~
备份好后会出现如下的情况
~
[root@114-212-235-54 ~]# ls
anaconda-ks.cfg bin
您在 /var/spool/mail/root 中有邮件
打开如下
From root@114-212-235-54.nju.edu.cn Sat Oct 29 00:30:04 2016
Return-Path: <root@114-212-235-54.nju.edu.cn>
X-Original-To: root
Delivered-To: root@114-212-235-54.nju.edu.cn
Received: by 114-212-235-54.nju.edu.cn (Postfix, from userid 0)
id 014E1806CCCF; Sat, 29 Oct 2016 00:30:03 +0800 (CST)
From: "(Cron Daemon)" <root@114-212-235-54.nju.edu.cn>
To: root@114-212-235-54.nju.edu.cn
Subject: Cron <root@114-212-235-54> sh ~/bin/backup_nova1.sh
Content-Type: text/plain; charset=UTF-8
Auto-Submitted: auto-generated
Precedence: bulk
X-Cron-Env: <XDG_SESSION_ID=7>
X-Cron-Env: <XDG_RUNTIME_DIR=/run/user/0>
X-Cron-Env: <LANG=zh_CN.UTF-8>
X-Cron-Env: <SHELL=/bin/bash>
X-Cron-Env: <PATH=/sbin:/bin:/usr/sbin:/usr/bin>
X-Cron-Env: <MAILTO=root>
X-Cron-Env: <HOME=/root>
X-Cron-Env: <LOGNAME=root>
X-Cron-Env: <USER=root>
Message-Id: <20161028163004.014E1806CCCF@114-212-235-54.nju.edu.cn>
Date: Sat, 29 Oct 2016 00:30:02 +0800 (CST)
/root/bin/backup_nova1.sh:行7: [-d: 未找到命令
mkdir: 无法创建目录"/home/Nova1": 文件已存在
receiving incremental file list
sent 19 bytes received 5657 bytes 3784.00 bytes/sec
total size is 132713177 speedup is 23381.46
receiving incremental file list
sent 204 bytes received 30543 bytes 61494.00 bytes/sec
total size is 20748680 speedup is 674.82
receiving incremental file list
home/
home/test.txt
sent 34 bytes received 122 bytes 312.00 bytes/sec
total size is 15 speedup is 0.10
receiving incremental file list
root/
root/.bash_history
root/.viminfo
sent 3121 bytes received 381418 bytes 256359.33 bytes/sec
total size is 315892838 speedup is 821.48
"/var/spool/mail/root" 64L, 2009C