Linux 用rsync备份系统快照

用法:

将原目录赋值到目的端创建镜像:

$rsync -av source_path destination_path

其中,-a表示要进行归档,-v表示在stdout上打印出细节信息或进度。

这里的路径可以是本地路径也可以是远程路径,路径格式可以是/home/zhang/data 也可以是zhang@192.168.0.9:/home/backups/data

举例:[root@test temp]# rsync -av words.txt wordsssssending incremental file listwords.txt

sent 167 bytes received 31 bytes 396.00 bytes/sectotal size is 79 speedup is 0.40[root@test temp]# lswords.txt wordssss

要将数据备份到远程主机或服务器上,可以使用:

$rsycn -av source_dir username@host:PATH

举例:[root@test temp]# rsync -av words.txt zhang@10.10.1.7:/home/backups/temp/The authenticity of host ‘10.10.1.7 (10.10.1.7)’ can’t be established.RSA key fingerprint is 9d:87:f2:4b:51:b8:08:6f:dc:d7:8e:87:f4:9c:f6:ad.Are you sure you want to continue connecting (yes/no)? yesWarning: Permanently added ‘159.226.11.46’ (RSA) to the list of known hosts.zhang@10.10.1.7’s password:sending incremental file listwords.txt

sent 167 bytes received 31 bytes 10.15 bytes/sectotal size is 79 speedup is 0.40[root@test temp]#如果需要在源端建立一份镜像,只需要定期运行同样的rsync命令即可。它只会对更改过的文件进行复制。

用下面的方法将远程主机上的数据恢复到本地主机:

$rsync -av username@host:PATH destinatiom

举例:[root@test temp]# rsync -av zhang@10.10.1.7:/home/backups/temp/words.txt words.txtzhang@10.10.1.7’s password:receiving incremental file listwords.txt

sent 36 bytes received 198 bytes 31.20 bytes/sectotal size is 105 speedup is 0.45[root@test temp]# ls

rsync命令用SSH连接远程主机。通常会询问SSH连接的密码。这可以通过使用SSH密钥来实现自动化(无需用户输入密码),请确保远程主机上安装并运行着OpenSSH。

在传输过程中也可以对数据压缩以后传输,这时候需要用rsync的选型-z指定。

就路径格式而言,如果我们在source_path 末尾使用/,那么rsync会将source_path 尾端目录中的所有内容复制到目的端。如果没有使用/,rsync会将source_path尾端目录本身复制到目的端。

例如,下面的命令会复制test目录中的内容:

$rsync -av /home/test/ /home/backups

下面的命令则会将test目录复制到目的端:

$rsync -av /home/test /home/backups

如果在destination_path 末尾使用/,那么rsync会将来自源端的内容复制到目的端目录中。如果没有使用/,rsync会在目的端路径末尾创建一个同名目录,然后将源端的内容复制到这个目录backups中。

例如:

$rsync -av /home/test /home/backups/

该命令将源端的内容复制到已有目录backups中。

$rsync -av /home/test /home/vackups

该命令在目的端创建目录backups,并将源端(/home/test)的内容复制到此目录中。

================================================================

其他功能:

第一、在归档时用rsync排除部分文件

–exclude Pattern(表示正则表达式)

或者通过一个列表文件指定需要排除的文件

–exclude-from filepath

第二、在更新rsync备份时,删除不存在的文件

–delete

默认不会删除不存在的文件

第三、定期调度备份

$crontab -e

0 */10 * * * rsync -avz /home/code user@ipaddress:/home/backups

上面的crontab将rsync调度为每10小时运行一次。

*/10 在crontab语法中的小时位,/10指定每10小时执行一次备份。如果*/10出现在分钟位,那就是每10分钟执行一次备份。

举例(每隔一分钟将时间写入words.txt文件一次):

[root@test temp]#crontab -e

然后输入:1-59 * * * * date >> /home/lhb/temp/words.txt[root@test temp]# crontab -l1-59 * * * * date >> /home/lhb/temp/words.txt[root@test temp]#cat words.txt

Wed Sep 19 17:23:01 CST 2012Wed Sep 19 17:24:01 CST 2012Wed Sep 19 17:25:01 CST 2012Wed Sep 19 17:26:01 CST 2012Wed Sep 19 17:27:01 CST 2012Wed Sep 19 17:28:01 CST 2012

告诉自己,我这次失败了,重新开始吧!下次我会吸取教训,不让自己犯同样的错误的

Linux 用rsync备份系统快照

相关文章:

你感兴趣的文章:

标签云: