天天看点

Linux 服务器间文件传输命令以及同步命令rsync的使用

*******************************也可以用文件共享nfs

scp命令的实际应用

1>从本地服务器复制到远程服务器

实例:

scp /home/linux/soft/scp.zip [email protected]:/home/linux/others/soft

(2) 复制目录

scp -P 2002 -r /home/linux/soft/ [email protected]:/home/linux/others/

2>从远程服务器复制到本地服务器

scp [email protected]:/home/linux/soft/scp.zip /home/linux/others/scp.zip

scp www.mydomain.com:/home/linux/soft/ -r /home/linux/others/

scp 加端口

 scp -P ×××  test.tar.gz      root@×××:/root/

rsync的用法

安装yum install rsync 

which rsync  查找安装的位置

rmp -qf /usr/bin/rsync  查找软件包

参考地址:http://www.ttlsa.com/web/let-infotify-rsync-fast/

rsync是类unix系统下的数据镜像备份工具,从软件的命名上就可以看出来了——remote sync。rsync是Linux系统下的文件同步和数据传输工具,它采用“rsync”算法,可以将一个客户机和远程文件服务器之间的文件同步,也可以在本地系统中将数据从一个分区备份到另一个分区上。如果rsync在备份过程中出现了数据传输中断,恢复后可以继续传输不一致的部分。rsync可以执行完整备份或增量备份。它的主要特点有:

1.可以镜像保存整个目录树和文件系统;

2.可以很容易做到保持原来文件的权限、时间、软硬链接;无须特殊权限即可安装;

3.可以增量同步数据,文件传输效率高,因而同步时间短;

4.可以使用rcp、ssh等方式来传输文件,当然也可以通过直接的socket连接;

5.支持匿名传输,以方便进行网站镜象等;

6.加密传输数据,保证了数据的安全性;

一。实例:(文件由117复制到114的配置)(备注:这个用法相当于“拉”源服务器的文件备份到当前服务器)

1、资源服务器117配置, 目录:/etc/rsyncd.conf  内容(安装yum -y install rsync后就会在etc目录下有rsyncd.conf文件配置下面需求就可以了)

#[global]设置全局变量

uid=0

gid=0

max connections=50

use chroot=no

log file=/var/log/rsyncd.log

pid file=/var/run/rsyncd.pid

lock file=/var/run/rsyncd.lock

secrets file=/etc/rsyncd.server

hosts allow = 192.168.1.114

hosts deny = *

#上传文件

[UploadFile]

path = /home/cooguo/uploadfiles

comment = UploadFile

ignore errors

read only = yes

list = yes

auth users = work

secrets file = /etc/rsyncd.server

#自动打包文件

[repack]

path=/home/cooguo/repack

comment=repack

ignore errors

read only = yes

list = yes

auth users = work

secrets file = /etc/rsync.server

保存虚拟用户密码:/etc/rsyncd.server (权限需要600)

重启:rsync --daemon --config=/etc/rsyncd.conf 

2、目标服务器114,执行配置/home/cooguo/rsync.sh如图

#! /bin/sh

logFile=/home/cooguo/rsync_log.txt

message=/home/cooguo/rsync_message.txt

cdate=$(date '+%Y-%m-%d %H:%M')

echo " " >> $logFile

echo " " >> $logFile

echo " " >> $logFile

echo "------------------开始增量备份,时间:$cdate -------------------------" >> $logFile

#echo "-----备份webapp-----" >> $logFile

#/usr/bin/rsync -avz --progress --exclude=*.log --exclude=*.out --exclude=logs/ --password-file=/home/cooguo/rsyncd.secrets [email protected]::Webapp /home/cooguo/veegaoserver/webapp/ >> $message

#sleep 5

#echo "-----备份server1-----" >> $logFile

#/usr/bin/rsync -avz --progress --exclude=*.log --exclude=*.out --exclude=logs/ --password-file=/home/cooguo/rsyncd.secrets [email protected]::server1 /home/cooguo/server1/ >> $message

echo "-----备份uploadfile27-----" >> $logFile

/usr/bin/rsync -avz --progress --password-file=/home/cooguo/rsyncd.secrets [email protected]::UploadFile /home/cooguo/veegaoserver/uploadfiles/ >> $message

echo "-----备份cdn-----" >> $logFile

/usr/bin/rsync -avz --progress --password-file=/home/cooguo/rsyncd.secrets [email protected]::cdn /home/cooguo/veegaoserver/cdn/ >> $message

#echo "-----备份repack-----" >> $logFile

#/usr/bin/rsync -avz --progress --password-file=/home/cooguo/rsyncd.secrets [email protected]::repack /home/cooguo/cxptserver/repack/ >> $message

#sleep 5

#echo "-----备份cxptsClient1-----" >> $logFile

#/usr/bin/rsync -avz --progress --exclude=*.log --exclude=*.out --exclude=logs/ --password-file=/home/cooguo/rsyncd.secrets [email protected]::client1 /home/cooguo/client1/ >> $message

文件/home/cooguo/rsyncd.secrets配置内容:

njgwork123

配置后好启动同步功能:

rsync --daemon --config=/etc/rsyncd.conf 

二.通过rsync+inotify实现数据的实时备份(备注:相当于推文件到备份服务器)

1.查看是否支持inotify,出kernel2.6.13开始正式并入内核

[[email protected] download]# ll /proc/sys/fs/inotify/

total 0

-rw-r--r-- 1 root root 0 Apr 27 14:53 max_queued_events

-rw-r--r-- 1 root root 0 Apr 27 14:53 max_user_instances

-rw-r--r-- 1 root root 0 Apr 27 14:53 max_user_watches

在linux内核中,默认的inotify机制提供了三个调控参数:

max_queued_exents #表示监控事件队列

max_user_instances #表示最多监控实例数

max_user_watches #表示每个实例最多监控文件数

注:当要监控的目录,文件数量较多或者变化比较频繁时,要加大这三个数的值

例如:可直接修改/etc/sysctl.conf配置文件,将管理队列设为32768,

实例数设为1024,监控数设为9000000(建议大于监控目标的总文件数)

改完-需要生效 执行sysctl -p

比如172.16.204.139 文件 同步到 172.16.204.138

172.16.204.138 安装

安装rsync

修改/etc/rsyncd.conf

#[global]设置全局变量

uid=0

gid=0

max connections=50

use chroot=no

log file=/var/log/rsyncd.log

pid file=/var/run/rsyncd.pid

lock file=/var/run/rsyncd.lock

secrets file=/etc/rsyncd.server

hosts allow = 172.16.204.138

hosts deny = *

#上传文件

[UploadFile]

path = /data/syhd_storage/game/uploadCommentImg

comment = UploadFile

ignore errors

read only = yes

list = yes

auth users = work

secrets file = /etc/rsyncd.server

修改 /etc/rsync.secrets (权限需要600)

rsynclsl:hetao123456

配置后好启动同步功能:

rsync --daemon --config=/etc/rsyncd.conf 

172.16.204.139

安装rsync+inotify

安装rsync和inotify(rsync只安装不启动不配置)

1

yum install 

-

y rsync

3、inotify是一种强大的、细粒度的、异步的文件系统事件控制机制。linux内核从2.6.13起,加入了inotify支持,通过inotify可以监控文件系统中添加、删除、修改、移动等各种事件,利用这个内核接口,第三方软件就可以监控文件系统下文件的各种变化情况,而inotify-tools正是实施监控的软件。

1

2

3

4

5

#wget http://github.com/downloads/rvoicilas/inotify-tools/inotify-tools-3.14.tar.gz

#tar zxf inotify-tools-3.14.tar.gz

#cd inotify-tools-3.14

#./configure --prefix=/usr/local/inotify

#  make && make install        

#touch  inotify.sh

修改

inotify.sh

#!/bin/bash host=172.16.204.138 #server的ip(备份服务器) src=/data/syhd_storage/game/uploadCommentImg #所要监控的备份目录(此处可以自定义,但是要保证存在) des=UploadFile #自定义的模块名,需要与client端定义的一致 password=/etc/rsync.password #密码文件(权限需要600) user=rsynclsl #用户名(一个实际存在的账号) inotify=/usr/local/inotify #inotify的安装目录 ${inotify}/bin/inotifywait -mrq --timefmt '%d/%m/%y %H:%M' --format '%T %w%f%e' -e modify,delete,create,attrib $src \ | while read files do /usr/bin/rsync -avzP --delete --timeout=100 --password-file=${password} $src $user@$host::$des echo "${files} was rsynced" >>/data/inotify/rsync.log 2>&1 done

修改

/

etc

/

rsync.password

hetao123456

后端运行

nohup  /data/inotify/inotify.sh >rsync.log   2>&1 &

继续阅读