centos7文件备份

Centos7系统文件备份

本文件使用的两台服务器是centos7系统,并且经过测试,文件可以成功备份;如果是其他Linux系统;请部署完成先进行测试。 准备工作:

两台centos7系统的服务器

一.安装rsync

centos7系统默认安装了rsync(可使用命令 rsync 进行查看);如果没有安装,请使用命令安装

yum -y install rsync

二.配置rsync

安装成功后,修改配置文件:/etc/rsyncd.conf。使用vim命令打开(没有安装vim命令的,请先安装vim:yum -y install vim)

# /etc/rsyncd: configuration file for rsync daemon mode # See rsyncd.conf man page for more options. # configuration example: # uid = nobody # gid = nobody # use chroot = yes # max connections = 4

# pid file = /var/run/rsyncd.pid # exclude = lost+found/ # transfer logging = yes # timeout = 900

# ignore nonreadable = yes

# dont compress = *.gz *.tgz *.zip *.z *.Z *.rpm *.deb *.bz2 # [ftp]

# path = /home/ftp

# comment = ftp export area

上面这是默认的配置。 添加如下配置:

uid = root # //设置运行rsync 进程的用户 gid = root use chroot = no max connections = 4

pid file = /var/run/rsyncd.pid #//CentOS7中yum安装不需指定pid file 否则报错 lock file=/var/run/rsyncd.lock log file = /var/log/rsyncd.log # //此文件定义完成后系统会自动创建

#exclude = lost+found/ 需要排除的文件 transfer logging = yes timeout = 900

ignore nonreadable = yes # //同步时跳过没有权限的目录

dont compress = *.gz *.tgz *.zip *.z *.Z *.rpm *.deb *.bz2 # //传输时不压缩的文件

在两台服务器上完成上面两步的配置。 三.服务器配置:

1.在配置文件中添加如下配置:(位置:/etc/rsyncd.conf)

[test] //此名字即备份服务器端使用rsync来同步的模块名 path=/usr/local/test //实际需要同步的路径 comment=test //规则描述 ignore errors

read only=no //表示可以pull write only=no //表示可以push list=no

auth users=rsyncuser

//备份服务器端获取文件的身份此用户并不是本机中确实存在的用户 secrets file=/etc/rsyncd.password

//用来认证客户端的秘钥文件格式 username:password,此文件权限一定需要改为600,且属主必须与运行rsync的用户一致。

hosts allow=* //允许所有主机访问

完成上述配置,并保存。 2. 创建密码文件

echo 'rsyncuser:123456'>/etc/rsyncd.password //文件用户名和路径为上面定义,别写错,密码自己定 chmod 600 /etc/rsyncd.passwd //修改权限

3. 重启服务

systemctl restart rsyncd.service

四.备份服务器

1. 创建密码文件

echo '123456' >>/etc/rsyncd.password //注意这里只需要服务器rsyncd.password 中的密码 chmod 600 /etc/rsyncd.password

2. 重启服务

systemctl restart rsyncd.service

3. 编写文件同步的脚本

执行vim /root/test.sh命令

#!/bin/bash

rsync -auv --password-file=/etc/rsyncd.password rsyncuser@192.168.101.22::test

/root/test/

说明:password-file:备份服务器上密码文件

rsyncuser:服务器上配置的用户 192.168.101.22:服务器地址 test:服务器上配置的模块名 /root/test:备份文件的地址

4. 执行定时任务

输入命令crontab -e

输入执行时间:*/5**** sh /root/test.sh(每5分钟执行一次)

五.其他操作

1. 两台服务器是需要开启873端口的

firewall-cmd --add-port=873/tcp --permanent //添加端口到默认区域 firewall-cmd --reload //重启

2. 关于SELinux

临时关闭SELinux

setenforce 0 查看状态

getenforce

开机关闭selinux(永久关闭)

编辑/etc/selinux/config文件,将SELINUX的值设置为disabled。下次开机SELinux就不会启动了

3. rayncd服务开机自启

systemctl enable rsyncd.service 检查是否成功启动 netstat -lnp|grep 873

4. 定时任务相关

查询任务

命令:crontab -l

开启服务

systemctl start crond

关闭服务

systemctl stop crond

重启服务

systemctl restart crond

重新加载配置

systemctl reload crond

要把cron设为在开机的时候自动启动,在 /etc/rc.d/rc.local 脚本中加入 /sbin/service crond start即可

联系客服:779662525#qq.com(#替换为@) 苏ICP备20003344号-4