可以给 mirror 命令传入参数,控制同步的行为:
--reverse 上传到FTP上,也可以使用 -R
--delete: 如果远程目录下已经没有某个文件,而本地有,则删除这个文件。
--only-newer: 只下载新的文件。mirror的判定新文件是通过文件大小和修改时间来判定的,我们可以使用
--ignore-time,来屏蔽时间判定,这样mirror只通过大小来判断文件,同步速度加快。
--exculde: 不包含某个目录。
--verbose: 详细输出。
比如:
mirror --delete --only-newer --verbose work bak/
自动备份服务器work文件夹,到本地当前目录的 bak文件夹下,并且删除已经不存在的文件,并且只下载最新的文件,再给出详细的输出。
用 lftp 的 -e 选项,把命令直接接在 lftp 之后:
lftp -u username,password -e”mirror --delete --only-newer --verbose /work bak/” ftpIP
也可以用 at 选项指定备份的时间:
lftp at 01:00 -u username,password -e”mirror --delete --only-newer –verbose /work bak/” ftpIP
这样每天 1 点就会执行备份任务。
通过lftp的mirror模式(ncftp也有mirror模式)
从FTP服务器上备份到本地
A、命令行方式
1
2
3
4
5
6
7
| lftp -c "set ftp:list-options -a;
open ftp://user:password@your.ftp.com;
lcd ./web;
cd /web/public_html;
mirror --delete --use-cache --verbose --allow-chown
--allow-suid --no-umask --parallel=2 --exclude-glob .svn"
|
B、脚本方式
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
| #!/bin/bash
HOST="your.ftp.host.dom"
USER="username"
PASS="password"
LCD="/path/of/your/local/dir"
RCD="/path/of/your/remote/dir"
lftp -c "set ftp:list-options -a;
open ftp://$USER:$PASS@$HOST;
lcd $LCD;
cd $RCD;
mirror --delete \
--verbose \
--exclude-glob a-dir-to-exclude/ \
--exclude-glob a-file-to-exclude \
--exclude-glob a-file-group-to-exclude* \
--exclude-glob other-files-to-esclude"
|
从本地恢复到FTP服务器上
A、命令行方式
1
2
3
4
5
6
7
| lftp -c "set ftp:list-options -a;
open ftp://user:password@your.ftp.com;
lcd ./web;
cd /web/public_html;
mirror --reverse --delete --use-cache --verbose --allow-chown
--allow-suid --no-umask --parallel=2 --exclude-glob .svn"
|
B、脚本方式
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
| #!/bin/bash
HOST="your.ftp.host.dom"
USER="username"
PASS="password"
LCD="/path/of/your/local/dir"
RCD="/path/of/your/remote/dir"
lftp -c "set ftp:list-options -a;
open ftp://$USER:$PASS@$HOST;
lcd $LCD;
cd $RCD;
mirror --reverse \
--delete \
--verbose \
--exclude-glob a-dir-to-exclude/ \
--exclude-glob a-file-to-exclude \
--exclude-glob a-file-group-to-exclude* \
--exclude-glob other-files-to-esclude"
|
两种方式间主要的差别就是在–reverse选项上,有这个选项就是put files,反之则是get files。