[20170220]快速拷贝文件在linux磁盘之间

简介: [20170220]快速拷贝文件在linux磁盘之间.txt --上个星期五要将1.3T的文件(每个都很大)从1个磁盘移到另外的磁盘,测试发现cp 根本无法忍受.

[20170220]快速拷贝文件在linux磁盘之间.txt

--上个星期五要将1.3T的文件(每个都很大)从1个磁盘移到另外的磁盘,测试发现cp 根本无法忍受.几乎要8个小时问题感觉出在文件系统
--的cache上,google看了一些连接:

1.首先cp慢的主要原因我感觉应该出现在文件系统缓存上,这个时候使用缓存没有必要,因为仅仅拷贝一次,使用缓存有点多余.
  如果通过dstat观察可以发现如下,开始一段很好,读写可以达到200M,写入更快到400M.但是一般15秒上下就停滞下来.等待4x秒,
  有开始加快.

  通过free观察free内存越来越少.最后基本保持不动.

2.我看了许多这方面的文档:
--//一个最简单的方法就是dd 在参数iflag=direct,oflag=direct参数,绕过缓存,但是这样的缺点就是要编程,实际上也不是很麻烦.
--//注有一些文档提供nocache参数:
http://www.gnu.org/software/coreutils/manual/html_node/dd-invocation.html

'nocache'

Request to discard the system data cache for a file. When count=0 all cached data for the file is specified, otherwise
the cache is dropped for the processed portion of the file. Also when count=0, failure to discard the cache is diagnosed
and reflected in the exit status.

Note data that is not already persisted to storage will not be discarded from cache, so note the use of the "sync"
options in the examples below, which are used to maximize the effectiveness of the 'nocache' flag.

Here are some usage examples:

# Advise to drop cache for whole file
dd if=ifile iflag=nocache count=0

# Ensure drop cache for the whole file
dd of=ofile oflag=nocache conv=notrunc,fdatasync count=0

# Drop cache for part of file
dd if=ifile iflag=nocache skip=10 count=10 of=/dev/null

# Stream data using just the read-ahead cache.
# See also the 'direct' flag.
dd if=ifile of=ofile iflag=nocache oflag=nocache,sync
--//不过我使用的dd版本没有nocache参数.
--//在google的过程中,发现这个链接https://github.com/Feh/nocache:

nocache - minimize filesystem caching effects

The nocache tool tries to minimize the effect an application has on the Linux file system cache. This is done by
intercepting the open and close system calls and calling posix_fadvise with the POSIX_FADV_DONTNEED parameter. Because
the library remembers which pages (ie., 4K-blocks of the file) were already in file system cache when the file was
opened, these will not be marked as "don't need", because other applications might need that, although they are not
actively used (think: hot standby).

Use case: backup processes that should not interfere with the present state of the cache.

--//决定在自己的测试环境先测试看看.而且debian已经集成了这个包,感觉应该很可靠.

3.下载安装很简单,就是一般安装的3步曲:
解压=>make=>make install ( 注有一些要先执行./configure,这里不需要.)

4.测试:
--首先清除该文件缓存
# cachedel datafile/system01.bdf
# cachedel /u01/system01.bdf
# time /bin/cp system01.bdf /u01/system01.bdf
real    0m51.536s
user    0m0.272s
sys     0m18.136s

# cachestats datafile/system01.bdf
pages in cache: 1024002/1024002 (100.0%)  [filesize=4096008.0K, pagesize=4K]
--//可以发现拷贝的文件缓存100%,备份出来的文件也一样. 大约4096/52=78M/秒.

--完成后free的显示:
# free
             total       used       free     shared    buffers     cached
Mem:     132261196  131797060     464136          0     591348  123554608
-/+ buffers/cache:    7651104  124610092
Swap:     31455264     780220   30675044

--//可以发现free内存剩余很小.如果你再重复上面的命令/bin/cp system01.bdf /u01/system01.bdf,可以发现飞快,因为文件已经缓
--//存了.

5.测试使用nocache:
# cachedel datafile/system01.bdf
# cachedel /u01/system01.bdf
# time nocache /bin/cp system01.bdf /u01/system01.bdf
real    0m50.444s
user    0m0.217s
sys     0m18.698s
--//时间相差不大.对比上面,因为文件没有缓存.

--//在拷贝的过程中执行:
# cachestats datafile/system01.bdf
pages in cache: 172291/1024002 (16.8%)  [filesize=4096008.0K, pagesize=4K]
# cachestats datafile/system01.bdf
pages in cache: 190499/1024002 (18.6%)  [filesize=4096008.0K, pagesize=4K]
....
# cachestats datafile/system01.bdf
pages in cache: 340483/1024002 (33.3%)  [filesize=4096008.0K, pagesize=4K]
# cachestats datafile/system01.bdf
pages in cache: 365411/1024002 (35.7%)  [filesize=4096008.0K, pagesize=4K]
...
# cachestats datafile/system01.bdf
pages in cache: 24/1024002 (0.0%)  [filesize=4096008.0K, pagesize=4K]

--//可以发现cache不断增加,到结束时清除cache.观察备份的文件也一样.
--//也可以看出nocache的作用实际上就是结束后清除缓存.

# cachestats /u01/system01.bdf
pages in cache: 6/1024002 (0.0%)  [filesize=4096008.0K, pagesize=4K]

--完成后free的显示:
# free
             total       used       free     shared    buffers     cached
Mem:     132261196  121362884   10898312          0     597632  113115280
-/+ buffers/cache:    7649972  124611224
Swap:     31455264     780220   30675044
--//可以发现free依旧保持很多内存.

6.最好测试使用dd看看:
# cachedel datafile/system01.bdf
# cachedel /u01/system01.bdf
--//注有些访问提到iflag=nocache oflag=nocache参数,我这个版本不支持这个参数.感觉好像使用这个参数会快一些.

# time dd if=system01.bdf of=/u01/system01.bdf bs=1024M iflag=direct oflag=direct
3+1 records in
3+1 records out
4194312192 bytes (4.2 GB) copied, 80.2868 seconds, 52.2 MB/s
real    1m20.603s
user    0m0.000s
sys     1m2.941s
--//这个有一点慢.

# cachestats /u01/system01.bdf
pages in cache: 0/1024002 (0.0%)  [filesize=4096008.0K, pagesize=4K]
# cachestats datafile/system01.bdf
pages in cache: 18/1024002 (0.0%)  [filesize=4096008.0K, pagesize=4K]
--//可以发现没有缓存.

# free
             total       used       free     shared    buffers     cached
Mem:     132261196  120296788   11964408          0     586588  112113960
-/+ buffers/cache:    7596240  124664956
Swap:     31455264     786416   30668848

7.一些补充:
--//googel还可以发现rsync有参数,好像官方的版本不支持这个参数.
--drop-cache           that works local
--remote-drop-cache    that works on remote

--//网上也可以找到tar的方式,我的测试效果一样,文件也会缓存.
# tar cf - . | ( cd dest ; tar xvf - )
# tar cf - datafile | tar xvf - -C /mnt

8.再补充网络拷贝:
--我一般借助tar+ssh+pigz模式,例子:

#tar cf - oracle -I pigz | ssh oracle@ip_address tar xvf - -I pigz -C /u01/app/
--//如果不支持-I参数,使用--use-compress-program ,pigz要另外安装,另外有文章提高lz4压缩工具,有机会另行测试.
--//感觉这个使用nocache也可以获得好的效果,不再测试,有机会再测试吧^_^.
--//相关讨论:
http://intermediatesql.com/linux/scrap-the-scp-how-to-copy-data-fast-using-pigz-and-nc/
http://www.gnu.org/software/coreutils/manual/html_node/dd-invocation.html

后记:最后确定是存储设置有问题,以交给同事解决,具体细节以后有机会写出来.

目录
相关文章
|
4月前
|
Linux 开发工具
7种比较Linux中文本文件的最佳工具
7种比较Linux中文本文件的最佳工具
7种比较Linux中文本文件的最佳工具
|
2月前
|
存储 数据管理 Linux
区分Linux中.tar文件与.tar.gz文件的不同。
总之,".tar"文件提供了一种方便的文件整理方式,其归档但不压缩的特点适用于快速打包和解压,而".tar.gz"文件通过额外的压缩步骤,尽管处理时间更长,但可以减小文件尺寸,更适合于需要节约存储空间或进行文件传输的场景。用户在选择时应根据具体需求,考虑两种格式各自的优劣。
378 13
|
3月前
|
安全 Linux
Linux赋予文件000权限的恢复技巧
以上这些步骤就像是打开一扇锁住的门,步骤看似简单,但是背后却有着严格的逻辑和规则。切记,在任何时候,变更文件权限都要考虑安全性,不要无谓地放宽权限,那样可能
126 16
|
3月前
|
存储 Linux 数据处理
深入剖析Linux中一切即文件的哲学和重定向的机制
在计算机的奇妙世界中,Linux的这套哲学和机制减少了不同类型资源的处理方式,简化了抽象的概念,并蕴藏着强大的灵活性。就像变戏法一样,轻轻松松地在文件、程序与设备之间转换数据流,标准输入、输出、错误流就在指尖舞动,程序的交互和数据处理因此变得既高效又富有乐趣。
54 4
|
4月前
|
Linux
【Linux】 Linux文件I/O常见操作技巧
以上就是Linux文件I/O操作的一些技巧,接纳它们,让它们成为你在Linux世界中的得力伙伴,工作会变得轻松许多。不过记住,技巧的运用也需要根据实际情况灵活掌握,毕竟,最适合的才是最好的。
122 28
|
4月前
|
Ubuntu Linux
"unzip"命令解析:Linux下如何处理压缩文件。
总的来说,`unzip`命令是Linux系统下一款实用而方便的ZIP格式文件处理工具。本文通过简明扼要的方式,详细介绍了在各类Linux发行版上安装 `unzip`的方法,以及如何使用 `unzip`命令进行解压、查看和测试ZIP文件。希望本文章能为用户带来实际帮助,提高日常操作的效率。
585 12
|
4月前
|
Linux
在线对Linux进行磁盘扩容的技术指南。
综上所述,Linux磁盘扩容的过程,重要的不仅是技术,更是对每一步骤的深刻理解和投入的爱心。只要手握正确的工具,我们不仅能满足"孩子"的成长需求,还能享受其中的乐趣和成就。
296 10
|
3月前
|
Linux
linux文件重命名命令
本指南介绍Linux文件重命名方法,包括单文件操作的`mv`命令和批量处理的`rename`命令。`mv`可简单更改文件名并保留扩展名,如`mv old_file.txt new_name.txt`;`rename`支持正则表达式,适用于复杂批量操作,如`rename 's/2023/2024/' *.log`。提供实用技巧如大小写转换、数字序列处理等,并提醒覆盖风险与版本差异,建议使用`-n`参数预览效果。
|
Linux
百度搜索:蓝易云【Linux中如何对文件进行压缩和解压缩?】
这些是在Linux中进行文件压缩和解压缩的常见方法。根据您的需求和具体情况,可能会使用其他压缩工具和选项。您可以通过查阅相应命令的帮助文档来获取更多详细信息。
161 1
|
NoSQL Java Linux
Linux常用命令(文件目录操作、拷贝移动、打包压缩、文本编辑、查找)
Linux常用命令(文件目录操作、拷贝移动、打包压缩、文本编辑、查找)