数据同步自动化实现脚本 -sersync

本贴最后更新于 1478 天前,其中的信息可能已经时移世改

1 rsyncd 服务端方式实现

1.1 存放备份数据执行的脚本

#!/bin/bash # #******************************************************************** #Author:zhangzhuo #QQ: 1191400158 #Date: 2021-03-11 #FileName:install_backup_rsyncd.sh #URL: https://www.zhangzhuo.ltd #Description:The test script #Copyright (C): 2021 All rights reserved #******************************************************************** #验证用户名 USER='zz' #用户密码 PASS='123456' #备份目录 DIR='/data/backup' rpm -q rsync-daemon &>/dev/null || yum install -y rsync-daemon cat >> /etc/rsyncd.conf <<EOF uid = root gid = root max connections = 0 ignore errors exclude = lost+found/ log file = /var/log/rsyncd.log pid file = /var/run/rsyncd.pid lock file = /var/run/rsyncd.lock reverse lookup = no [backup] path = ${DIR} comment = backup dir read only = no auth users = ${USER} secrets file = /etc/rsync.pas EOF [ -d ${DIR} ] || mkdir -p ${DIR} echo "${USER}:${PASS}" > /etc/rsync.pas chmod 600 /etc/rsync.pas systemctl enable --now rsyncd &>/dev/null systemctl restart rsyncd

1.2 要备份端执行的脚本

#!/bin/bash # #******************************************************************** #Author:zhangzhuo #QQ: 1191400158 #Date: 2021-03-11 #FileName:sersync_rsync.sh #URL: https://www.zhangzhuo.ltd #Description:The test script #Copyright (C): 2021 All rights reserved #******************************************************************** #安装包名称 tar_name='sersync2.5.4_64bit_binary_stable_final.tar.gz' #安装位置 install_dir='/usr/local/sersync' #要备份的目录,路径中的/加\进行转义 backup_dir='\/data' #rsyncd服务器配置 rsyncd_ip='192.168.10.82' rsyncd_dir='backup' #验证rsyncd用户信息,路径中的/加\进行转义 user_file='\/etc\/rsync.pas' user='zz' pass='123456' install(){ [ -f $tar_name ] || { echo "sersync压缩文件不存在" ; exit ;} [ -d `echo "" | sed -rn "s/(.*)/\1${backup_dir}/p"` ] || mkdir `echo "" | sed -rn "s/(.*)/\1${backup_dir}/p"` [ -d /usr/local/sersync ] || { tar xf ${tar_name};mv GNU-Linux-x86 /usr/local/sersync; } echo "PATH=${install_dir}:'$PATH'" >/etc/profile.d/sersync.sh rpm -q rsync &>/dev/null || dnf install -y rsync cp ${install_dir}/confxml.xml ${install_dir}/confxml.xml.bak userpass=`echo "" | sed -rn "s/(.*)/\1${user_file}/p"` echo "$pass" > $userpass chmod 600 $userpass } set_xml(){ sed -ri "s/(.*<attrib start=\")false(\"\/>.*)/\1true\2/" ${install_dir}/confxml.xml sed -ri "24s/(.*<localpath watch=\").*(\">.*)/\1${backup_dir}\2/" ${install_dir}/confxml.xml sed -ri "25s/(.*<remote ip=\").*(\" name=\").*(\"\/>.*)/\1${rsyncd_ip}\2${rsyncd_dir}\3/" ${install_dir}/confxml.xml sed -ri "s/(.*<auth start=\").*(\" users=\").*(\" passwordfile=\").*(\"\/>.*)/\1true\2${user}\3${user_file}\4/" ${install_dir}/confxml.xml } install set_xml sersync2 -dro ${install_dir}/confxml.xml && echo "同步启动成功" || echo "同步启动失败"

2 ssh 方式备份(存放数据端执行)

#!/bin/bash # #******************************************************************** #Author:zhangzhuo #QQ: 1191400158 #Date: 2021-03-11 #FileName:sersync_ssh.sh #URL: https://www.zhangzhuo.ltd #Description:The test script #Copyright (C): 2021 All rights reserved #******************************************************************** #安装包名称 tar_name='sersync2.5.4_64bit_binary_stable_final.tar.gz' #安装位置 install_dir='/usr/local/sersync' #要备份的目录,路径中的/加\进行转义 backup_dir='\/data' #rsyncd服务器配置 rsyncd_ip='192.168.10.82' rsyncd_root_pass='123456' rsyncd_dir='\/data\/backup' ssh(){ [ -f /root/.ssh/id_rsa ] || ssh-keygen -f /root/.ssh/id_rsa -P '' rpm -q sshpass &>/dev/null || yum -y install sshpass export SSHPASS=${rsyncd_root_pass} sshpass -e ssh-copy-id -o StrictHostKeyChecking=no $rsyncd_ip rpm -q pssh &>/dev/null || yum install -y pssh pssh -H "${rsyncd_ip}" "rpm -q rsync || dnf install -y rsync" dir1=`echo "" | sed -rn "s/(.*)/\1${rsyncd_dir}/p"` pssh -H "${rsyncd_ip}" "[ -d ${dir1} ] || mkdir -p ${dir1}" } install(){ [ -f $tar_name ] || { echo "sersync压缩包不存在" ;exit ;} [ -d `echo "" | sed -rn "s/(.*)/\1${backup_dir}/p"` ] || mkdir `echo "" | sed -rn "s/(.*)/\1${backup_dir}/p"` [ -d /usr/local/sersync ] || { tar xf ${tar_name};mv GNU-Linux-x86 /usr/local/sersync; } echo "PATH=${install_dir}:'$PATH'" >/etc/profile.d/sersync.sh rpm -q rsync &>/dev/null || dnf install -y rsync cp ${install_dir}/confxml.xml ${install_dir}/confxml.xml.bak } set_xml(){ sed -ri "s/(.*<attrib start=\")false(\"\/>.*)/\1true\2/" ${install_dir}/confxml.xml sed -ri "24s/(.*<localpath watch=\").*(\">.*)/\1${backup_dir}\2/" ${install_dir}/confxml.xml sed -ri "25s/(.*<remote ip=\").*(\" name=\").*(\"\/>.*)/\1${rsyncd_ip}\2${rsyncd_dir}\3/" ${install_dir}/confxml.xml sed -ri "s/(.*<ssh start=\").*(\"\/>.*)/\1true\2/" ${install_dir}/confxml.xml } ssh install set_xml ${install_dir}/sersync2 -dro ${install_dir}/confxml.xml && echo "同步服务已经启动" || echo "同步服务启动失败"
  • 执行脚本时请根据自己实际环境调试变量参数
  • 执行脚本时需下载 sersync 软件,并且变量中要写明软件压缩包存放位置
  • Shell

    Shell 脚本与 Windows/Dos 下的批处理相似,也就是用各类命令预先放入到一个文件中,方便一次性执行的一个程序文件,主要是方便管理员进行设置或者管理用的。但是它比 Windows 下的批处理更强大,比用其他编程程序编辑的程序效率更高,因为它使用了 Linux/Unix 下的命令。

    124 引用 • 74 回帖 • 1 关注
  • Rsync
    9 引用 • 28 回帖

相关帖子

欢迎来到这里!

我们正在构建一个小众社区,大家在这里相互信任,以平等 • 自由 • 奔放的价值观进行分享交流。最终,希望大家能够找到与自己志同道合的伙伴,共同成长。

注册 关于
请输入回帖内容 ...