#!/bin/bash
export LANG=C date=`date +%Y-%m-%d-%H%M` red=`echo -e "\033[0;31m"`
blue=`echo -e "\033[0;36m"`
white=`echo -e "\033[37m"` rsync=/usr/bin/rsync
rsync_bak_dir=/opt/data_bak/rsync_update_bak
exclude_web_file=/root/sh/rsync_online_web/exclude_web_file.txt
exclude_res_file=/root/sh/rsync_online_web/exclude_res_file.txt
www_bak_dir=/opt/data_bak/rsync_update_bak/www/$date
res_bak_dir=/opt/data_bak/rsync_update_bak/res/$date
logs=/var/log/rsync_web #删除历史备份函数
del_rsync_history(){
del_rsync_history=${rsync_bak_dir:?error} #删除历史目录是否定义,如果未定义程序退出,不在往下执行。
if [ $del_rsync_history != "/opt/data_bak/rsync_update_bak" ];then #判断删除历史目录是否正确,避免误删除
printf $red
echo "$del_rsync_history File directory error !!!"
echo "Exit..."
sleep
printf $white
exit
fi echo "find $del_rsync_history -mtime +30 | xargs rm -rf"
find $del_rsync_history -mtime + | xargs rm -rf
} #同步是否成功函数
is_ok(){
if [ $? -eq ];then
printf $blue
echo "file update Success"
printf $white
else
printf $blue
echo "file update Fail !!!"
printf $white
fi
} #同步排除文件是否存在函数
file_web_exist(){
if [ ! -f $exclude_web_file ];then
printf $red
echo "$exclude_web_file file does not exist !!!"
printf $white
exit
fi
} file_res_exist(){
if [ ! -f $exclude_res_file ];then
printf $red
echo "$exclude_res_file file does not exist !!!"
printf $white
exit
fi
} #日志目录是否存在
if [ ! -d $logs ];then
mkdir -p $logs
fi #目标文件夹
target_www=/opt/web/test/
target_res=/opt/web/test/res/ ip=192.168.1.100
user=rsyncuser
pass=/root/sh/rsync_online_web/rsyncpass #源文件夹
source_www=$user@$ip::web_www
source_res=$user@$ip::web_res #同步流程
if [ "$1" = "www" ];then
file_web_exist #判断同步排除文件是否存在
$rsync -vzrtopg \
-n \
--progress \
--delete-after \
--exclude-from=$exclude_web_file \
--password-file=$pass \
$source_www $target_www printf $blue
echo "-----------------------------------------------------"
echo " Are you sure update www.test.com ?[Y/y]"
echo "-----------------------------------------------------"
printf $white
read -p "> " s
if [ $s == "Y" -o $s == "y" ];then if [ ! -d $www_bak_dir ];then
mkdir -p $www_bak_dir
fi $rsync -vzrtopg \ #同步参数,-v显示详细信息,-z传输文件压缩,-r递归模式,-t保持文件时间信息,-o保持文件属主信息,-p保持文件权限,-g保持文件组信息
--progress \ #显示rsync备份过程
--delete-after \ #同步后删除文件
--exclude-from=$exclude_web_file \ #排除文件列表
--backup \ #备份
--backup-dir=$www_bak_dir \ #备份目录
--log-file=$logs/www.test.com.log \ #日志文件
--log-file-format="%f" \ #日志格式
--password-file=$pass \ #rsync密码
$source_www $target_www #将source_www文件及目录同步到target_www目录
is_ok #判断是否同步成功函数
del_rsync_history #删除历史备份文件函数
else
printf "No choice,Exit...\n"
exit
fi elif [ "$1" = "res" ]; then
file_res_exist
$rsync -vzrtopg \
-n \
--progress \
--delete-after \
--exclude-from=$exclude_res_file \
--password-file=$pass \
$source_res $target_res printf $blue
echo "-----------------------------------------------------"
echo " Are you sure update res.test.com ?[Y/y]"
echo "-----------------------------------------------------"
printf $white
read -p "> " s
if [ $s == "Y" -o $s == "y" ];then if [ ! -d $res_bak_dir ];then
mkdir -p $res_bak_dir
fi $rsync -vzrtopg \
--progress \
--delete-after \
--exclude-from=$exclude_res_file \
--backup \
--backup-dir=$res_bak_dir \
--log-file=$logs/res.test.com.log \
--log-file-format="%f" \
--password-file=$pass \
$source_res $target_res
is_ok
del_rsync_history
else
printf "No choice,Exit...\n"
exit
fi else
echo "----------------------------------------------------------------------------------------"
printf $blue
printf "\twww.test.com\n"
printf "\tUsage: /root/sh/rsync_online_web/rsync_test_web_update.sh {www|res}\n"
printf $white
echo "----------------------------------------------------------------------------------------"
fi

rsync同步脚本的更多相关文章

  1. shell脚本 inotify + rsync 同步脚本

    1.这是很简单的一个 文件监控+触发同步脚本 监控一台服务器下的一个文件夹,发生改变则向另一个个文件夹进行同步. 具体的inotify命令  rsync命令  可以看linux分类下的介绍 inoti ...

  2. shell生成rsync同步脚本

    test #!/bin/bash # # Rsync Install Script # Last Updated # ##### modify by Jinayf ##### ######手动修改以下 ...

  3. Windows Linux 之间rsync同步CODE文件

    Windows Linux 之间rsync同步CODE文件 一.环境Windows:OS:Microsoft Windows Web Server 2008 SP1IP:192.168.88.197 ...

  4. rsync同步Nginx日志遇到问题总结

    一.目的 将nginx 日志通过普通用户利用rsync公钥认证的方式实时同步到本地服务器上,之后使用elk程序进行处理. 二.遇到问题及解决方法思路 问题1.文件权限:nginx 的日志默认权限如下: ...

  5. Linux rsync 同步实践

    目录[-] 1. rsync 同步的大致思路 2. rsync的安装 3. rsync的配置 4. rsync的基本操作 服务器端启动 注2. 实时同步 注3. rsync通过linux防火墙 公司网 ...

  6. Ubuntu下多服务器 Rsync同步镜像服务配置

    主服务器:192.168.5.13_ubuntu 从服务器:192.168.5.11_centos ================== 1> 在两台主机上分别安装rsync========== ...

  7. Windows 之间用rsync同步数据(cwRsyncServer配置)

    rsync是一款优秀的数据同步软件,在跨服务器,跨机房,跨国备份服务器的首选工具,下面就来介绍下如何配置安装cwRsyncServer很大多数软件一样是B/C架构,cwRsyncServer是rsyn ...

  8. Linux下inotify监控文件夹状态,发生变化后触发rsync同步

    1.安装工具--inotifywget http://cloud.github.com/downloads/rvoicilas/inotify-tools/inotify-tools-3.14.tar ...

  9. rsync同步目录及同步文件

    最简单的只读同步工作. 一,服务端的配置 1,安装rsync(阿里云默认已有此程序) 略 2,生成文件rsyncd.conf,内容如下: #secrets file = /etc/rsyncd.sec ...

随机推荐

  1. linux(1)

    Linux/Unix操作系统 OS 系统软件 用户.应用程序 <-OS-> 硬件:CPU Memory Disk 外设管理软件测试方向: 被测系统主要的操作系统,监控系统资源.使用系统常用 ...

  2. Almost Acyclic Graph CodeForces - 915D (思维+拓扑排序判环)

    Almost Acyclic Graph CodeForces - 915D time limit per test 1 second memory limit per test 256 megaby ...

  3. ActiveMQ部署和代码尝试(二)

    部署和代码尝试 1. 部署在linux 上的acvtiveMQ 要可以通过前台windows 的页面访问,必须把linux 的IP和 windows的 IP 地址配置到同一个网关下 .这种情况一般都是 ...

  4. 第三章 URL与视图

    配置文件两种方式详解 先讲两种直接传参: 直接简单传参 app =Flask(__name__) app.config['DEBUG']=True app.config.update( DEBUG=t ...

  5. UVALive 6858——分类讨论&&水题

    题目 链接 题意:对于一个$n \times m$的矩阵的最外一圈,问是否能用$k \times 1$的方块填满 分析 考虑左右两边的情况,分类讨论,切记考虑所有可能的情形. #include< ...

  6. Codeforces Round #586 (Div. 1 + Div. 2) B. Multiplication Table

    链接: https://codeforces.com/contest/1220/problem/B 题意: Sasha grew up and went to first grade. To cele ...

  7. 报错Too many connections

    com.mysql.jdbc.exceptions.jdbc4.MySQLNonTransientConnectionException: Data source rejected establish ...

  8. nginx 之 root和alias

    转载:  https://www.jianshu.com/p/4be0d5882ec5 https://blog.csdn.net/Erica_1230/article/details/7855311 ...

  9. CSRF(跨站请求伪造)

    跨站请求伪造(Cross-site request forgery)是一种冒充受信任用户,向服务器发送非预期请求的攻击方式. 攻击的前提是: 用户已经登录过某网站. 攻击者通过一些诱惑性的标题,诱惑用 ...

  10. learning armbian steps(9) ----- armbian 源码分析(四)

    在上一节的分析当中,我们知道是通过对话框来选择到底编译的是哪块板子,基于什么样的配置. 接下来我们来拿一个实例来分析一下具体的案例,我们会选中如下所示的版本 iotx-3 AM335X 1Gb SoC ...