免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
12下一页
最近访问板块 发新帖
查看: 10580 | 回复: 14
打印 上一主题 下一主题

[FreeBSD] FreeBSD 数据迁移方法(ZT) [复制链接]

论坛徽章:
2
丑牛
日期:2013-09-29 09:47:222015七夕节徽章
日期:2015-08-21 11:06:17
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2005-11-02 09:20 |只看该作者 |倒序浏览
前两天哪位兄弟在问复制硬盘的方法,以前没有做过,不过印象中是看到过这么一篇文章的,刚又搜了搜,转过来吧,希望对其他兄弟有帮助。

葱头(Deland Wu) <delandwu@163.com>

相信一些朋友也曾经想过如何快捷安全迁移数据,迁移数据可能有多种原因,一种是想增加一块硬盘,把原来一些空间不够的分区迁移过来;另一种是硬盘复制,旧的硬盘容量可能太小了,又或者已经出现了问题,想用新的硬盘代替。葱头就分别举例说明怎样迁移数据,具体方法可能和你的硬盘的实际情况有所不同,这里只是作一个指引。

无论是那种方法,都必须先将新硬盘装上并让系统正确识别。为了不用设硬盘跳线(硬盘缺省为Master),这里举例安装一个新的IDE硬盘到IDE1接口,即与旧硬盘使用不同的数据线,系统识别为ad2;如果你想和旧硬盘接在同一数据线线上,即IDE0接口,则必须将跳线设为client,系统识别为ad1。
1、关机,将新硬盘装上,重新开机。
2、开机后查看是否系统是否已经识别到新硬盘了:

hpserver# dmesg | grep ad2
成功的话会显示
ad2: 9765MB <Maxtor 91024U4> [19841/16/63] at ata1-master UDMA33
等信息,否则你要检查是否接错线,或者硬件兼容问题。

新硬盘已经安装成功,下面举例说一下两种数据迁移的方法。

一、增加一个硬盘,将原来硬盘/var数据转移到新硬盘

在系统已经建立之后,FreeBSD不能像PQMagic那样自由调整分区空间而不破坏分区数据,所以,如果/var空间已经不够,需要增加一块硬盘增加空间,原来的数据也必须迁移过来。
2、对新硬盘进行分区
可以使用sysinstall或者命令行方式分区。虽然fdisk有更多灵活性,但我建议还是采用sysinstall较好。分区完之后再用sysinstall里面的Label建立文件系统相关文件系统。
3、将文件系统挂载到/mnt

hpserver# mount /dev/ad1s1e /mnt
4、将数据迁移,这里也有两种方法,一种是使用tar,另一种是使用dump,我分别介绍,大家可以根据需要而选择备份方式。
(1) 使用tar备份并还原数据。
hpserver# cd /var
hpserver# tar cf - * | (cd /mnt; tar xf -)
(2) 使用dump备份并用restore还原数据。就我个人而言,我推荐使用这种方法,因为我曾经试过用tar备份var目录的时候,出现了错误并自动停止,我分析了一下原因,一些正在使用的文件或者临时文件可能不能备份,但dump不是以文件为单位,而是而block为单位的,应该不存在这样的情况,所以这是我推荐使用dump和restore的原因。

hpserver# cd /mnt
hpserver# dump -f- /var | restore -f- -r
在过程中和结束会出现类似以下信息:
DUMP: Date of this level 0 dump: Wed Jan 14 23:44:35 2004
DUMP: Date of last level 0 dump: the epoch
DUMP: Dumping /dev/ad0s1e (/var) to standard output
DUMP: mapping (Pass I) [regular files]
DUMP: mapping (Pass II) [directories]
DUMP: estimated 305 tape blocks.
DUMP: dumping (Pass III) [directories]
DUMP: dumping (Pass IV) [regular files]
DUMP: DUMP: 361 tape blocks
DUMP: finished in less than a second
DUMP: DUMP IS DONE
5、有一点提醒的是,你要确保备份后没有数据写入原来的/var,否则,这些数据没有备份到新分区中。结束后别忘了检查一下结果,并卸载/mnt。

hpserver# ls /mnt
hpserver# umount /mnt
6、编辑/etc/fstab文件,将原来指向/var的文件系统改为/dev/ad1s1e,重新启动就可生效。

二、硬盘复制

如果旧的系统盘出现了一些毛病,或者容量不够,你想更换一个新硬盘,那可以把旧硬盘数据复制到新硬盘,然后把旧硬盘摘下。按照上面的步骤,我们假定已经将硬盘安装好。
这里有一个小tips,就是先记下旧硬盘文件系统所对应的mount点,新硬盘分区次序按照旧的一样,这样免去我们下面容易混淆的步骤,我们查看/etc/fstab对照就知道了。不然搞了几下,自己都忘记了ad2s1e到底是用来挂载那个目录的。
1、使用sysinstall把新硬盘分区,设置好MBR,并一一对照原来的次序创建文件系统,假设原来的文件系统/dev/ad0s1g 对应/usr,那你新硬盘/dev/ad2s1g就用来做/usr,原来的文件系统/dev/ad0s1e 对应/var,那你新硬盘/dev/ad2s1e就用来做/var。按M分别清空所有文件系统的mount点,因为我们还没有建立临时的mount的目录,不清空会和系统原来的mount冲突的。

2、创建临时的目录用来作临时mount点并挂载这些文件系统。

hpserver# cd /mnt
hpserver# mkdir root
hpserver# mkdir usr
hpserver# mkdir var
用cat /etc/fstab查看原来的文件系统的对应关系,我们只要看最后面的一个字母就可以了,这样避免我们忘记了什么对应什么,这就是我刚才为什么让新硬盘的文件系统的名称对应以前的原因。
hpserver# mount /dev/ad2s1a /mnt/root
hpserver# mount /dev/ad2s1g /mnt/usr
hpserver# mount /dev/ad2s1e /mnt/var
3、复制硬盘最好用dump,尽量不用tar,因为你备份根分区的时候,如果不加参数排除一些目录,它会把全部东西备份到你新的分区的,这肯定不是我们想看到的。

hpserver# cd /mnt/root
hpserver# dump -f- / | restore -f- -r
hpserver# cd /mnt/var
hpserver# dump -f- /var | restore -f- -r
hpserver# cd /mnt/usr
hpserver# dump -f- /usr | restore -f- -r
4、到这里为止,数据备份和还原已经结束,检查一下有没有错漏,然后分别umount这些分区。

hpserver# umount /mnt/root
hpserver# umount /mnt/usr
hpserver# umount /mnt/var
5、最后,关机,摘掉原来的硬盘,把新硬盘挂到旧硬盘原来的位置,这步不要省略了,如果你不换位置,系统还是认为新硬盘是ad2,由于你的/etc/fstab里面是对应ad0的,所以启动会找不到/usr和/var等文件系统。启动看看,如果没有什么异常,那恭喜你,数据迁移已经成功,否则,请再看看有什么操作错误或者忘记做哪一步了。无论怎样,都别那么着急把旧硬盘内容清空,最好让新的系统运行几天观察有什么异常再动手。

切记,数据迁移的是一件比较危险的事情,操作过程中不能有差错,否则可能导致你原来的数据丢失,所以必须非常谨慎。

论坛徽章:
1
寅虎
日期:2013-09-29 23:15:15
2 [报告]
发表于 2005-11-02 09:22 |只看该作者
未试过,或许以后有用!!

论坛徽章:
2
丑牛
日期:2013-09-29 09:47:222015七夕节徽章
日期:2015-08-21 11:06:17
3 [报告]
发表于 2005-11-02 09:33 |只看该作者
没有试过,这个比用dd好些,dd限制太多了

论坛徽章:
1
技术图书徽章
日期:2013-12-05 23:25:45
4 [报告]
发表于 2005-11-02 09:55 |只看该作者
http://www.freebsd.org.cn/snap/d ... ooks/faq/disks.html

9.2. How do I move my system over to my huge new disk?

The best way is to reinstall the OS on the new disk, then move the user data over. This is highly recommended if you have been tracking -STABLE for more than one release, or have updated a release instead of installing a new one. You can install booteasy on both disks with boot0cfg(, and dual boot them until you are happy with the new configuration. Skip the next paragraph to find out how to move the data after doing this.

Should you decide not to do a fresh install, you need to partition and label the new disk with either /stand/sysinstall, or fdisk( and disklabel(. You should also install booteasy on both disks with boot0cfg(, so that you can dual boot to the old or new system after the copying is done. See the formatting-media article for details on this process.

Now you have the new disk set up, and are ready to move the data. Unfortunately, you cannot just blindly copy the data. Things like device files (in /dev), flags, and links tend to screw that up. You need to use tools that understand these things, which means dump(. Although it is suggested that you move the data in single user mode, it is not required.

You should never use anything but dump( and restore( to move the root filesystem. The tar(1) command may work - then again, it may not. You should also use dump( and restore( if you are moving a single partition to another empty partition. The sequence of steps to use dump to move a partitions data to a new partition is:

   1.

      newfs the new partition.
   2.

      mount it on a temporary mount point.
   3.

      cd to that directory.
   4.

      dump the old partition, piping output to the new one.

For example, if you are going to move root to /dev/ad1s1a, with /mnt as the temporary mount point, it is:

# newfs /dev/ad1s1a
# mount /dev/ad1s1a /mnt
# cd /mnt
# dump 0af - / | restore xf -

Rearranging your partitions with dump takes a bit more work. To merge a partition like /var into its parent, create the new partition large enough for both, move the parent partition as described above, then move the child partition into the empty directory that the first move created:

# newfs /dev/ad1s1a
# mount /dev/ad1s1a /mnt
# cd /mnt
# dump 0af - / | restore xf -
# cd var
# dump 0af - /var | restore xf -

To split a directory from its parent, say putting /var on its own partition when it was not before, create both partitions, then mount the child partition on the appropriate directory in the temporary mount point, then move the old single partition:

# newfs /dev/ad1s1a
# newfs /dev/ad1s1d
# mount /dev/ad1s1a /mnt
# mkdir /mnt/var
# mount /dev/ad1s1d /mnt/var
# cd /mnt
# dump 0af - / | restore xf -

You might prefer cpio(1), pax(1), tar(1) to dump( for user data. At the time of this writing, these are known to lose file flag information, so use them with caution.

论坛徽章:
0
5 [报告]
发表于 2007-05-18 16:05 |只看该作者
支持!!!!!!!!

论坛徽章:
0
6 [报告]
发表于 2007-10-29 01:35 |只看该作者
好文!!!!

论坛徽章:
0
7 [报告]
发表于 2007-10-29 02:46 |只看该作者
已经看过了,似乎是永远的Unix上的文章。

论坛徽章:
0
8 [报告]
发表于 2008-05-22 13:13 |只看该作者
ddddddddddddd

论坛徽章:
0
9 [报告]
发表于 2008-06-03 14:06 |只看该作者
確實是用dump | restore 比較方便,我也是用此法更換過一個硬盤。

论坛徽章:
0
10 [报告]
发表于 2008-10-22 14:56 |只看该作者
用此方法克隆了一套系统。成功。
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP