php执行rsync,rsync安装及.sh执行脚本写法

本文介绍了rsync工具在Unix系统中的使用,它能够高效地同步和备份文件,仅传输文件差异。rsync支持多种特性,如保留文件属性、通过不同协议传输,并且可以通过shell脚本实现自动站点数据更新。文章还详细阐述了rsync的安装、配置文件rsyncd.conf的创建以及如何用Shell脚本执行网站数据的自动更新。
摘要由CSDN通过智能技术生成

zhoz评论:rsync 很好很强大!刚才用一个脚本即实现了对网站的同步镜备份。

系统地了解一下:

rsync is a file transfer program for Unix systems. rsync uses the "rsync algorithm" which provides a very fast method for bringing remote files into sync. It does this by sending just the differences in the files across the link, without requiring that both sets of files are present at one of the ends of the link beforehand.

rsync 是一个Unix系统下的文件同步和传输工具。rsync是用 “rsync 算法”提供了一个客户机和远程文件服务器的文件同步的快速方法。

Some features of rsync include

rsync 包括如下的一些特性:

* can update whole directory trees and filesystems

能更新整个目录和树和文件系统;

* optionally preserves symbolic links, hard links, file ownership, permissions, devices and times

有选择性的保持符号链链、硬链接、文件属于、权限、设备以及时间等;

* requires no special privileges to install

对于安装来说,无任何特殊权限要求;

* internal pipelining reduces latency for multiple files

对于多个文件来说,内部流水线减少文件等待的延时;

* can use rsh, ssh or direct sockets as the transport

能用rsh、ssh 或直接端口做为传输入端口;

* supports anonymous rsync which is ideal for mirroring

支持匿名rsync 同步文件,是理想的镜像工具;

rsync的安装:

[root@zhoz.com:zhoz]$ sudo apt-get  install  rsync  注:在debian、ubuntu 等在线安装方法;

[root@zhoz.com:zhoz]# slackpkg  install  rsync   注:Slackware 软件包在线安装;

[root@zhoz.com:zhoz]# yum install rsync    注:Fedora、Redhat 等系统安装方法;

其它Linux发行版,请用相应的软件包管理方法来安装;如果是源码包,也就是用下面的办法;

[root@zhoz.com:zhoz]# tar xvf  sync-xxxx.tar.gz 或sync-xxx.tar.bz2

[root@zhoz.com:zhoz]# cd  sync-xxx

[root@zhoz.com:/home/beinan/sync-xxx]# ./configure --prefix=/usr  ;make ;make install   注:在用源码包编译安装之前,您得安装gcc等编译开具才行;

rsync服务器的配置文件rsyncd.conf

[root@zhoz.com:~]#mkdir /etc/rsyncd  注:在/etc目录下创建一个rsyncd的目录,我们用来存放rsyncd.conf 和rsyncd.secrets文件;

[root@zhoz.com:~]#touch /etc/rsyncd/rsyncd.conf  注:创建rsyncd.conf ,这是rsync服务器的配置文件;

[root@zhoz.com:~]#touch /etc/rsyncd/rsyncd.secrets  注:创建rsyncd.secrets ,这是用户密码文件;

[root@zhoz.com:~]#chmod 600 /etc/rsyncd/rsyncd.secrets  注:为了密码的安全性,我们把权限设为600;

[root@zhoz.com:~]#ls -lh /etc/rsyncd/rsyncd.secrets

-rw------- 1 root root 14 2007-07-15 10:21 /etc/rsyncd/rsyncd.secrets

[root@zhoz.com:~]#touch /etc/rsyncd/rsyncd.motd

内容太多,就说到这里。

现在说明用Shell文件,执行站点数据自动更新:

目标:将/home/zhoz 同步更新/home/zhoztest

#!/bin/bash

# zhoz.com 09/04/20

## definetion

zhoz=/home/zhoz;

zhozcopy=/home/zhozcopy;

## site user and path etc.[server: zhoz.com]

site_user_list=();

site_path_list=();

site_user_list[0]=zhoz;

site_path_list[0]=/home/zhoztest;

## exclude file directory

exclude_file_list=();

exclude_file_list[0]=/admin/zhoz/*

exclude_file_list[1]=/config/settings.inc;

## run

exclude_string="";

for exclude_file in ${exclude_file_list[@]}

do exclude_string=`echo $exclude_string --exclude $exclude_file`; done;

if [ -d $zhozcopy ]; then rm -rf $zhozcopy; fi;

if [ ! -d $zhozcopy ]; then mkdir -p $zhozcopy/contents; fi;

rsync -a $exclude_string $zhoz/contents/ $zhozcopy/contents/;

## update all site

site_total_nums=`expr ${#site_user_list[@]} - 1`

for i in `seq 0 $site_total_nums`

do

chown -R ${site_user_list[$i]}:${site_user_list[$i]} $zhozcopy/;

rsync -a $zhozcopy/contents/ ${site_path_list[$i]}/contents/;

done

这个保存为:zhoz_rsync.sh然后用root执行,即可完成以上目标!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值