【SRE笔记 2022.9.30 集群知识及Centos基础优化】

集群内服务软件

  • 防火墙:深信服,天融信,华为,思科
  • CDN服务: 阿里CDN,腾讯CDN,网宿CDN
  • DNS服务:阿里云DNS,腾讯DNS,硬件F5,bind,powerDNS
  • 负载均衡:ngingx(L4tcp和L7http),lvs,Haproxy(L4 和L7)
  • 高可用:keepalived heartbeat
  • 静态web服务:Nginx,Apache IIS
  • 动态web服务:php(php-fpm),java(tomcat,resinm,weblogic),python(python)
  • 关系型数据库服务:mysql(mariadb),oracle,postgresql
  • 非关系型数据库:Mongodb,Redis,hbase
  • 存储服务:NFS,MFS,FASTDFS,CEPH,HADOOP
  • 复制备份 :sersync,rsync 定时备份(crond+rsync)实时备份(rsync+sersync)
  • ELFK分布式日志集群
    Elasticsearch 数据存储
    Filebeat 数据收集
    Logstash 数据处理
    Kibana 数据分析和展示
    Kafaka消息队列服务
    Zookeeper集群
    Hadoop大数据存储
  • VPN服务:硬件VPN,openvpn,pptp
  • 跳板机服务:硬件跳板机,jumperver(可视化、图形化、记录日志)
  • 批量管理服务: ssh(key)服务,ansible服务(常用),saltstak,cfengine,expect,ssh-pass,SecureCRT
  • 监控服务:nagios(早期的),zabbix,promothous(普罗米修斯),grafana(可视化工具)
  • 时间服务
  • 数据仓库:yum仓库,rpm包
  • 变更管理:jira
  • 版本管理:git gitlab
  • 代码及配置发布:jenkins ants maven
  • 代码质量检测:sonarqube
  • 代码发布流程:办公室测试-idc测试-idc1/2服务器组-余下的服务器(发布一个地区,灰度发布)
  • 运维开发平台,将变更管理、版本管理、代码发布及配置,代码质量检测,监控服务等所有组件整合到一起
  • Vmware workstation:学习使用
  • 虚拟主机:xen、kvm
  • 云计算服务 :openstack(私有云),python语言开发
  • 容器化:docker+k8 go语言开发
  • 公有云服务:阿里云,腾讯云。
  • 未来就是公有云和容器化时代!

集群模板机创建

  • 使用标准分区
  • 配置两个网卡,第二块网卡无需配置网关
    在这里插入图片描述

Linux系统优化

  • 以下优化方面有部分需要在服务稳定后再进行操作,避免环境出现问题

用户优化

  • 安装系统最后一步将oldboy加了管理员的设置方法。是将新用户加入了wheel组。
    在这里插入图片描述
    在这里插入图片描述
[root@zhk ~]# grep wheel /etc/sudoers
## Allows people in group wheel to run all commands
%wheel	ALL=(ALL)	ALL #%wheel代表wheel组
# %wheel	ALL=(ALL)	NOPASSWD: ALL
  • 如果没有上述操作,需要新建用户加入sudo管理
  • 禁止root远程登录,使用新增的用户登录,并配置xshell连接(复制之前成功会话,无需新建)
  • ssh端口也可修改
    在这里插入图片描述
[root@zhk ~]# vi /etc/ssh/sshd_config 

-

ssh远程连接效率提升

  • 修改为no
    在这里插入图片描述
  • 修改为no
    在这里插入图片描述
  • 修改为no
    在这里插入图片描述
  • 监听IP修改(内网,避免外网探测及连接),默认SSH的22端口监听所有IP
    在这里插入图片描述
    -修改位置(练习模板机未修改,生产环境需要修改)
    在这里插入图片描述
  • 修改完成后,外网IP无法连接

配置yum源

[root@zhk ~]# curl -s -o /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo
[root@zhk ~]# curl -s -o /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo

常用软件安装

yum install -y net-tools tree nmap dos2unix lrzsz nc lsof wget tcpdump htop iftop sysstat nethogs psmisc bash-completion vim-enhanced

在这里插入图片描述

安全优化

  • selinux关闭
[root@zhk ~]# sed -i 's#SELINUX=enforcing#SELINUX=disable#g' /etc/selinux/config
[root@zhk ~]# grep -i selinux /etc/selinux/config
# This file controls the state of SELinux on the system.
# SELINUX= can take one of these three values:
#     enforcing - SELinux security policy is enforced.
#     permissive - SELinux prints warnings instead of enforcing.
#     disabled - No SELinux policy is loaded.
SELINUX=disable
# SELINUXTYPE= can take one of three values:
SELINUXTYPE=targeted 
[root@zhk ~]# setenforce 0
[root@zhk ~]# getenforce 
Permissive
  • 防火墙关闭
[root@zhk ~]# systemctl stop firewalld
[root@zhk ~]# systemctl disable firewalld
Removed symlink /etc/systemd/system/multi-user.target.wants/firewalld.service.
Removed symlink /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service.

中文字符集(非必要)

  • 方法一
[root@zhk ~]# cp /etc/locale.conf /etc/locale.conf.ori
[root@zhk ~]# echo 'LANG="zh_CN.UTF-8"' > /etc/locale.conf
[root@zhk ~]# source /etc/locale.conf
[root@zhk ~]# echo $LANG
zh_CN.UTF-8
  • 方法二
[root@zhk ~]# cp /etc/locale.conf /etc/locale.conf.ori
[root@zhk ~]# localectl set-locale LANG="zh_CN.UTF-8"
[root@zhk ~]# echo $LANG
en_US.UTF-8
[root@zhk ~]# source /etc/locale.conf
[root@zhk ~]# echo $LANG
zh_CN.UTF-8

时间同步

[root@zhk ~]#  crontab -e
#时间同步
*/30 * * * * /usr/sbin/ntpdate ntp5.aliyun.com &> /devl/null

提升命令行安全(可选)

[root@zhk ~]# echo 'export TMOUT=300' >> /etc/profile
[root@zhk ~]# echo 'export HISTSIZE=5' >> /etc/profile
[root@zhk ~]# echo 'export HISTFILESIZE=5' >> /etc/profile
您在 /var/spool/mail/root 中有邮件
[root@zhk ~]# tail -f /etc/pr
prelink.conf.d/ printcap        profile         profile.d/      protocols
[root@zhk ~]# tail -f /etc/profile
            . "$i" >/dev/null
        fi
    fi
done

unset i
unset -f pathmunge
export TMOUT=300 #300s无操作超时断开
export HISTSIZE=5# 控制内存中保存的历史命令5条,默认1000条
export HISTFILESIZE=5
[root@zhk etc]# . /etc/profile

加大文件描述符

[root@zhk etc]# ulimit -n
1024
[root@zhk etc]# echo '* - nofile 65535' >> /etc/security/limits.conf 
[root@zhk etc]# ulimit -SHn 65535
[root@zhk etc]# ulimit -n
65535
  • 之后退出重新登录生效

优化内核

cat >> /etc/sysctl.conf << EOF
#针对网络
net.ipv4.tcp_fin_timeout = 2
net.ipv4.tcp_max_orphans = 16384
net.ipv4.ip_local_port_range = 2000 65000
net.ipv4.tcp_max_syn_backlog = 16384
net.core.somaxconn = 16384
net.ipv4.tcp_max_tw_buckets = 36000
net.ipv4.tcp_syn_retries = 1
net.ipv4.tcp_synack_retries = 1
net.ipv4.tcp_tw_reuse = 1
net.ipv4.tcp_tw_recycle = 1
net.ipv4.tcp_syncookies = 1
net.ipv4.tcp_keepalive_time = 600
net.ipv4.route.gc_timeout = 100
net.core.netdev_max_backlog = 16384

#针对iptables,防火墙如果关闭不会提示
net.nf_conntrack_max  =25000000  # 防火墙最大连接数
net.netfilter.nf_conntrack_max = 25000000 net.netfilter.nf_conntrack_tcp_timeout_established = 180 net.netfilter.nf_conntrack_tcp_timeout_time_wait = 120
net.netfilter.nf_conntrack_tcp_timeout_close_wait = 60 net.netfilter.nf_conntrack_tcp_timeout_fin_wait = 120
#nfs 共享存储优化
net.core.wmem_default = 8388608
net.core.rmem_default = 8388608
net.core.wmem_max = 16777216
net.core.rmem_max
EOF
[root@zhk ~]# sysctl -p
net.ipv4.tcp_fin_timeout = 2   
net.ipv4.tcp_max_orphans = 16384  #
net.ipv4.ip_local_port_range = 2000 65000
net.ipv4.tcp_max_syn_backlog = 16384
net.core.somaxconn = 16384
net.ipv4.tcp_max_tw_buckets = 36000
net.ipv4.tcp_syn_retries = 1
net.ipv4.tcp_synack_retries = 1
net.ipv4.tcp_tw_reuse = 1 
net.ipv4.tcp_tw_recycle = 1 
net.ipv4.tcp_syncookies = 1
net.ipv4.tcp_keepalive_time = 600
net.ipv4.route.gc_timeout = 100
net.core.netdev_max_backlog = 16384
sysctl: cannot stat /proc/sys/net/nf_conntrack_max: 没有那个文件或目录
sysctl: cannot stat /proc/sys/net/netfilter/nf_conntrack_max: 没有那个文件或目录
sysctl: cannot stat /proc/sys/net/netfilter/nf_conntrack_tcp_timeout_close_wait: 没有那个文件或目录
net.core.wmem_default = 8388608
net.core.rmem_default = 8388608
net.core.wmem_max = 16777216
sysctl: /etc/sysctl.conf(35): invalid syntax, continuing..

扩展优化

-为以后意见安装网站集群留好rpm及依赖工具包

[root@zhk /]# sed -i 's@keepcache=0@keepcache=1@g' /etc/yum.conf 
[root@zhk /]# cat /etc/yum.conf 
[main]
cachedir=/var/cache/yum/$basearch/$releasever
keepcache=1
debuglevel=2
logfile=/var/log/yum.log
exactarch=1
obsoletes=1
gpgcheck=1
plugins=1
installonly_limit=5
bugtracker_url=http://bugs.centos.org/set_project.php?project_id=23&ref=http://bugs.centos.org/bug_report_page.php?category=yum
distroverpkg=centos-release

关键文件锁定

[root@zhk ~]# chattr +i  /etc/passwd /etc/shadow /etc/gshadow /etc/group /etc/inittab 
[root@zhk ~]# lsattr  /etc/passwd /etc/shadow /etc/gshadow /etc/group /etc/inittab 
----i----------- /etc/passwd
----i----------- /etc/shadow
----i----------- /etc/gshadow
----i----------- /etc/group
----i----------- /etc/inittab
[root@zhk ~]# 
[root@zhk bin]# mv chattr hd01
[root@zhk bin]# mv lsattr hd02
  • 锁定并给相关命令改名
  • 以后添加用户需要先解锁,然后再加锁,可写脚本处理
hd01 -i   /etc/passwd /etc/shadow /etc/gshadow /etc/group /etc/initta
useradd $1
hd02 +i  /etc/passwd /etc/shadow /etc/gshadow /etc/group /etc/inittab                                                                     

清楚多余虚拟账户(可选)

  • 也可加注释屏蔽

grub引导加密

[root@zhk bin]# grub2-setpassword 
Enter password: 
Confirm password: 
  • 修改后查看grub配置文件
[root@zhk bin]# cat /boot/grub2/user.cfg 
GRUB2_PASSWORD=grub.pbkdf2.sha512.10000.824CCD028FD9910D276A77D9FEBB4DA88264057C146F31C6AB7C0C803C6B8D7C81D81195694E1014EEB8ED5F7E339F07CA8000990CDE4BE2FE2129A30B0B1B64.2A404210EC0FE108FDA011CBEF594820E382FC4E2B54A6B96FDD0A1C4721C987579CBD6D9ED8563360EB1B46B7BCA71ADC9244F3D371888530C53C3D202E61D9
  • 重启未生效,待处理

禁ping(内核参数)

[root@zhk bin]# echo "net.ipv4.icmp_echo_ignore_all=1" >> /etc/sysctl.conf 
[root@zhk bin]# sysctl -p

在这里插入图片描述

补丁升级

  • 环境部署好后升级,生产后不要操作
yum update -y

开机自启精简

[root@zhk ~]$ systemctl list-unit-files | grep enable|egrep -v "sshd.service|crond.service|sysstat|rsyslo^NetworkManager.service|irqbalance,service"|awk '{print "systemctl disable",$1}'|bash
  • 保留以上服务
[root@zhk ~]$ systemctl list-unit-files | grep enable
autovt@.service                               enabled 
crond.service                                 enabled 
getty@.service                                enabled 
sshd.service                                  enabled 
sysstat.service                               enabled 

生产企业最小化原则

  • 安装软件包最小化
  • 用户权限最小化
  • 目录文件权限最小化
  • 自启动服务最小化
  • 服务运行用户权限最小化
  • 服务运行用户权限最小化

扩展优化

echo "PS1='\[\e[32;1m\] [\u@\h \W]\\$ \[\e[0m\]'" >> /etc/profile
source /etc/profile

在这里插入图片描述

创建规范目录

[root@zhk ~]$ mkdir -p /server/scripts
[root@zhk ~]$ mkdir -p /server/tools

检查端口

  • 剩余ssh端口是最安全的状态
  • postfix服务需要重启才能生效
[root@zhk ~]$ netstat -lntup
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name    
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN      1214/sshd           
tcp        0      0 127.0.0.1:25            0.0.0.0:*               LISTEN      1371/master         
tcp6       0      0 :::22                   :::*                    LISTEN      1214/sshd           
tcp6       0      0 ::1:25                  :::*                    LISTEN      1371/master         
[root@zhk ~]$ systemctl stop postfix
[root@zhk ~]$ netstat -lntup
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name    
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN      1214/sshd           
tcp6       0      0 :::22                   :::*                    LISTEN      1214/sshd           

集群IP规划

服务器说明eth0 外网IP(NAT)eth1 内网IP(NAT)主机名称规则
A1-负载服务器 0110.0.0.5/24172.16.1.5/16lb01
A2-负载服务器 0210.0.0.6/24172.16.1.6/16lb02
b1-web 服务器 110.0.0.7/24172.16.1.7/16web01
b2-web 服务器 210.0.0.8/24172.16.1.8/16
SB1-web 服务器 110.0.0.9/24172.16.1.9/16sweb01
SB2-web 服务器 210.0.0.10/24172.16.1.10/16sweb02
C1-NFS 存储服务器10.0.0.31/24172.16.1.31/16nfs01
C2-rsync 备份服务器10.0.0.41/24172.16.1.41/16backup
C3-msyql 数据库服务器10.0.0.51/24172.16.1.51/16db01
X-管理服务器10.0.0.61/24172.16.1.61/16m01

记得两个网卡要设置开机自启

配置内网解析

[root@zhk ~]$ cat >> /etc/hosts << EOF
> 172.16.1.5  lb01          
> 172.16.1.6   lb02         
> 172.16.1.7     web01       
> 172.16.1.8    web02        
> 172.16.1.9    sweb01     
> 172.16.1.10   sweb02
> 172.16.1.31   nfs01
> 172.16.1.41   backup
> 172.16.1.51  db01
> 172.16.1.61   m01
> EOF
[root@zhk ~]$ cat /etc/hosts
127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
::1         localhost localhost.localdomain localhost6 localhost6.localdomain6
172.16.1.5  lb01          
172.16.1.6   lb02         
172.16.1.7     web01       
172.16.1.8    web02        
172.16.1.9    sweb01     
172.16.1.10   sweb02
172.16.1.31   nfs01
172.16.1.41   backup
172.16.1.51  db01
172.16.1.61   m01
  • eth网卡的LAN区段要修改为172
    在这里插入图片描述
  • 内网区段网卡不必配置DNS

安装及配置路径规划

目录说明目录结构说明
/server/scripts服务器本地存放脚本程序的目录
/server/tools服务器本地存放软件安装包的目录
/application/软件名服务器本地软件安装的根目录,软件名不带版本号,例如/application/nginx
/application/nginx/htmlweb服务器站点目录(bbs.blog.www.edu)
/application/nginx/logweb服务器日志(bbs
/back服务器本地数据备份目录,也是备份服务器RSYC的备份目录
/dataNFS共享存储共享文件目录

拍摄快照

  • 拍摄优化后的集群模板机快照

克隆

  • 链接克隆,节省空间
  • 克隆web01 nfs01 backup三台。
    在这里插入图片描述
  • 先开启backup
  • 通过脚本修改IP和主机名,节省时间
    在这里插入图片描述
  • 配置xshell会话

rsync复制软件

简介及特性

  • 全称是Remote sync hronization,是开源的,高速的,可实现本地及远程,全量以及增量的数据复制拷贝工具。
  • rsync工作在网络层
  • wget工作在应用层
  • 支持复制链接、设备、所有者、组和权限
  • exclude和exclude-from类似于GNU tar的选项
  • 不需要超级用户权限
  • 文件传输流水线以最小化延迟成本
  • 可以使用任何透明的远程shell 包括ssh rsh,借助ssh隧道加密
  • 不需要超级用户权限
  • 文件传输流水线以最小化延迟成本
  • 支持匿名或经过身份验证的rsync守护进程(有服务端)

增量复制原理

  • 使用quick check算法,只对增量的部分进行复制,该算法查找大小或上次修改时间已更改的文件进行增量复制

版本

  • 2.x 比对差异后复制
  • 3.x 一边比对一边复制

三种工作模式

  • 参数 avg
  • expect工具帮助人工交互省的输入密码了。

local(本地)

rsync [选项] src [dest]

[root@backup ~]$ rsync -avz /etc /tmp/  # a=rtopg
  • 注意etc目录如果不带斜线是把整个目录直接拷贝过去,
[root@backup ~]$ ls /tmp/
etc
  • 带斜线是把目录下所有的内容拷贝过去
  • 删除文件内容
[root@backup /]$ touch null.txt
[root@backup /]$ ll
总用量 44
-rw-r--r--.   1 root root   12 104 11:42 aa.txt
lrwxrwxrwx.   1 root root    7 103 22:12 bin -> usr/bin
dr-xr-xr-x.   5 root root 4096 104 11:57 boot
drwxr-xr-x.  19 root root 3180 105 17:33 dev
drwxr-xr-x.  82 root root 8192 105 17:33 etc
drwxr-xr-x.   3 root root   17 103 22:17 home
lrwxrwxrwx.   1 root root    7 103 22:12 lib -> usr/lib
lrwxrwxrwx.   1 root root    9 103 22:12 lib64 -> usr/lib64
drwxr-xr-x.   2 root root    6 411 2018 media
drwxr-xr-x.   3 root root   18 104 11:54 mnt
-rw-r--r--.   1 root root    0 105 18:58 null.txt
drwxr-xr-x.  82 root root 8192 105 17:33 opt
dr-xr-xr-x. 100 root root    0 105 17:32 proc
dr-xr-x---.   2 root root  164 105 17:31 root
drwxr-xr-x.  21 root root  500 105 17:33 run
lrwxrwxrwx.   1 root root    8 103 22:12 sbin -> usr/sbin
drwxr-xr-x.   4 root root   34 104 21:22 server
drwxr-xr-x.   2 root root    6 411 2018 srv
dr-xr-xr-x.  13 root root    0 105 17:33 sys
drwxr-xr-x.  88 root root 8192 105 18:51 tmp
drwxr-xr-x.  13 root root  155 103 22:12 usr
drwxr-xr-x.  19 root root  267 103 22:18 var
[root@backup /]$ cat aa.txt 
#UseDNS yes
[root@backup /]$ rsync --delete null.txt aa.txt # 参数改为avg也可以
sending incremental file list
null.txt

sent 90 bytes  received 35 bytes  250.00 bytes/sec
total size is 0  speedup is 0.00
[root@backup /]$ cat aa.txt 
  • 删除目录
[root@backup /]$ rsync -r --delete /null/ /opt
  • 查看文件属性
[root@backup /]$ rsync aa.txt 
-rw-r--r--              0 2022/10/05 18:58:46 aa.txt

Access via remote shell(隧道)

  • pull rsync [选项] [user@]host:src…[dest]
  • push rsync [ 选项] src… [user@]host:dest
  • 企业场景 需要加密的重要数据 未搭建服务端的
远程shell企业场景
  • 配合ssh+免秘钥连接登录(单向从私钥到公钥),进行文件备份传输工作
  • ssh-keygen
[root@nfs01 ~]$ ssh-keygen 
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa): 
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:
SHA256:R6xmtpOtWSm//wSdnG/7/sB1bj29VEG4bnNm+6UAfkA root@nfs01
The key's randomart image is:
+---[RSA 2048]----+
|              .. |
|         .   ..  |
|          E   .. |
|         +   + o.|
|        S + o = +|
|       + * + * B=|
|        = = + OoX|
|         B . o.*=|
|        o oo..o+O|
+----[SHA256]-----+
  • 密钥对存放位置,其中rsa相当于钥匙(私钥),pub文件相当于锁(公钥)
[root@nfs01 ~]$ ls ~/.ssh/
id_rsa  id_rsa.pub  known_hosts
  • 把锁放在目标机上(需要把之前的root远程登录打开)
[root@nfs01 ~]$ ssh-copy-id -i ~/.ssh/id_rsa.pub 10.0.0.41
/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/root/.ssh/id_rsa.pub"
/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed
/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys
root@10.0.0.41's password: 
Number of key(s) added: 1
Now try logging into the machine, with:   "ssh '10.0.0.41'"
and check to make sure that only the key(s) you wanted were added.
  • 公钥位置,文件权限为600,该目录权限为700(满足以上的条件才能使用)
    在这里插入图片描述
  • 之后操作有无隧道传输都可以免密
无隧道
  • 命令如下,root已禁用远程登录
[root@backup ~]$ rsync -avz /etc/hosts zhk@10.0.0.31:/tmp/
The authenticity of host '10.0.0.31 (10.0.0.31)' can't be established.
ECDSA key fingerprint is SHA256:cARYRR8mqmyHRJezuHO1GTiFEwHKwdV6WMEPOWE/NRI.
ECDSA key fingerprint is MD5:17:88:0a:02:e5:60:01:f0:40:72:94:86:96:28:ad:f2.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '10.0.0.31' (ECDSA) to the list of known hosts.
zhk@10.0.0.31's password: 
sending incremental file list
hosts

sent 225 bytes  received 35 bytes  57.78 bytes/sec
total size is 392  speedup is 1.51
有隧道
  • 参数e
[root@backup ~]$ rsync -avz /etc -e 'ssh -p 22' zhk@10.0.0.31:/tmp/

access via rsync daemon(CS架构)(常用)

  • pull rsync [选项] [user@]host:src…[dest] 或
    rsync [选项] rsync://[user@]host:[:port]/src…[dest]
  • push rsync [ 选项] src… [user@]host:dest 或
    rsync [选项] src … rsync://[user@]host[:port]/dest
  • 服务器启动rsync服务,客户端执行推拉命令。主动发起
服务端配置文件
  • 修改之前记得做备份
  • /etc/rsync.conf
    在这里插入图片描述
  • **上图配置文件折腾了半天也没完成测试,清空重新配置一遍,问题就在于配置后面的#和注释,会引起问题。如密码文件后还有空格,那认证会出现问题。
  • 做上下注释
    在这里插入图片描述
  • 下图清空了注释**
    在这里插入图片描述
  • hosts allow 和denny最好不要同时用,容易有冲突;二选一进行配置。默认允许,且白名单优先级高于黑名单。
配置密码文件
[root@backup ~]$ echo "rsync_backup:1" > /etc/rsync.password
[root@backup ~]$ cat /etc/rsync.password
rsync_backup:1
创建相关备份目录
  • 并授权back目录的用户组合主为rsync
启动服务端
  • 端口号 873
[root@backup ~]$ systemctl start rsyncd
[root@backup ~]$ systemctl enable rsyncd
Created symlink from /etc/systemd/system/multi-user.target.wants/rsyncd.service to /usr/lib/systemd/system/rsyncd.service.
[root@backup ~]$ systemctl status rsyncd
● rsyncd.service - fast remote file copy program daemon
   Loaded: loaded (/usr/lib/systemd/system/rsyncd.service; enabled; vendor preset: disabled)
   Active: active (running) since 四 2022-10-06 10:07:43 CST; 1h 8min ago
 Main PID: 3918 (rsync)
   CGroup: /system.slice/rsyncd.service
           └─3918 /usr/bin/rsync --daemon --no-detach

10月 06 10:07:43 backup systemd[1]: Started fast remote file copy program daemon.
[root@backup ~]$ netstat -lntup | grep rsync
tcp        0      0 0.0.0.0:873             0.0.0.0:*               LISTEN      3918/rsync          
tcp6       0      0 :::873                  :::*                    LISTEN      3918/rsync  
客户端配置
方法一
  • 配置密码文件
[root@nfs01 ~]$ echo '1' > /etc/rsync.password
[root@nfs01 ~]$ cat /etc/rsync.password
1
  • 配置密码文件权限
[root@nfs01 ~]$ ll /etc/rsync.password
-rw-r--r--. 1 root root 2 106 11:18 /etc/rsync.password
[root@nfs01 ~]$ chmod -R 0600 /etc/rsync.password
[root@nfs01 ~]$ ll /etc/rsync.password
-rw-------. 1 root root 2 106 11:18 /etc/rsync.password
方法二
[root@nfs01 ~]$ echo ' export RSYNC_PASSWORD=1' >> /etc/bashrc 
[root@nfs01 ~]$ tail -1 /etc/bashrc 
[root@nfs01 ~]$ . /etc/bashrc 
[root@nfs01 ~]$ echo $RSYNC_PASSWORD
测试
  • 客户端拉取
[root@nfs01 ~]$ rsync -avz rsync_backup@172.16.1.41::backup /data --password-file=/etc/rsync.password
receiving incremental file list

sent 24 bytes  received 271 bytes  590.00 bytes/sec
total size is 0  speedup is 0.00
  • 客户端推送 注意目录后的斜线
[root@nfs01 ~]$ rsync -avz /data/  rsync_backup@172.16.1.41::backup  --password-file=/etc/rsync.password
客户端命令参数
  • v --verbose 显示输出过程
  • z --compress 压缩
  • a --archive 多参数集合(-rtopgDl)
  • r --recursive 递归
  • t --times 保持修改时间属性
  • o --owner 保持属主不变
  • p – perms 保持权限不变
  • g --group 保持用户组不变
  • l --links 保持拷贝软链接
  • q --quiet 静默拷贝
  • –delete 删除
  • –exclude 排除
  • –exclude-from 从文件中排除
  • –bwlimit=KBPS 限制I/O带宽,KBbytes per Second
  • 查看帮助
[root@backup ~]$ man /etc/rsyncd.conf 
# /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 connec‐
tions  =  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
客户端复制报错案例
  • 客户端连接服务端时报错如下,是服务端没有rsync用户
[root@nfs01 ~]$ rsync -avz /etc  zhk@10.0.0.41:/backup/ 
zhk@10.0.0.41's password: 
sending incremental file list
rsync: recv_generator: mkdir "/backup/etc" failed: Permission denied (13)
*** Skipping any contents from this failed directory ***
etc/

sent 52,559 bytes  received 781 bytes  21,336.00 bytes/sec
total size is 33,179,085  speedup is 622.03
rsync error: some files/attrs were not transferred (see previous errors) (code 23) at main.c(1179) [sender=3.1.2]
  • 需要额外添加参数,允许root以外的用户存储文件的完整属性:fake super = yes
多模块复制
  • rsyncd.conf的配置文件中,如果参数不在模块内,默认全局生效,也可单独放在某些模块内。
排除文件复制
  • 排除文件
root@nfs01 ~]$ rsync -avg --exclude={a.1,a.3}.txt rsync_backup@172.16.1.41::backup /data
Password: 
receiving incremental file list
./
a.10.txt
a.2.txt
a.4.txt
a.5.txt
a.6.txt
a.7.txt
a.8.txt
a.9.txt
sent 209 bytes  received 525 bytes  293.60 bytes/sec
total size is 0  speedup is 0.00
  • 从文件中排除
[root@nfs01 ~]$ rsync -avg --exclude-from=/root/test.txt rsync_backup@172.16.1.41::backup /data
Password: 
receiving incremental file list
./
a10.txt
a6.txt
a7.txt
a8.txt
a9.txt
sent 186 bytes  received 360 bytes  218.40 bytes/sec
total size is 0  speedup is 0.00

主机之间数据无差异复制

  • 目录演示,注意目录后的斜线
  • 使data目录和null目录保持一直,相当于清空。
[root@nfs01 ~]$ mkdir /null -p

[root@nfs01 ~]$ ls /data/
a10.txt  a6.txt  a7.txt  a8.txt  a9.txt

[root@nfs01 ~]$ rsync -avg --delete /null/  /data/
sending incremental file list
deleting null/
deleting a9.txt
deleting a8.txt
deleting a7.txt
deleting a6.txt
deleting a10.txt
./

sent 47 bytes  received 79 bytes  252.00 bytes/sec
total size is 0  speedup is 0.00
[root@nfs01 ~]$ ls /data/
  • 守护进程模式和远程模式同样可执行。

断点

  • -partial 保留部分传输过的文件,使该文件剩余部分传输更快速

限速

  • –bwlimit=BPS

优缺点

优点

  • 增量备份,支持socket(daemon)守护进程模式,集中备份(支持推拉模式备份)
  • 远程shell通道模式还可以加密(SSH)传输
  • socket(daemon)需要加密传输,可以利用VPN服务或IPSEC服务

缺点

  • 大量小文件复制的时候,rsnc可能会停止僵死或效率不高。
  • 一次性远程拷贝可以使用scp,如果大量小文件可打成压缩包再拷贝。
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

学习使我清醒

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值