文章 78
评论 0
浏览 8359
数据同步自动化实现脚本-sersync

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

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软件,并且变量中要写明软件压缩包存放位置

标题:数据同步自动化实现脚本-sersync
作者:Carey
地址:HTTPS://zhangzhuo.ltd/articles/2021/03/12/1615549592752.html

生而为人

取消