原创Fedora用户和文件迁移到CentOS

参考文档:

http://www.cyberciti.biz/faq/howto-move-migrate-user-accounts-old-to-new-server/

FlexTk -- Linux File Copy/Sync Performance

http://www.flexense.com/documents/flextk_linux_copy_sync_performance.pdf

BBS讨论

http://lwn.net/Articles/400489/

http://www.1-script.com/forums/ssh/speed-difference-scp-vs-rcp-5163-.htm

比较scp,tar,rsync

http://www.crucialp.com/resources/tutorials/server-administration/how-to-copy-files-across-a-network-internet-in-unix-linux-redhat-debian-freebsd-scp-tar-rsync-secure-network-copy.php

关键词:transfer files between servers,linux migrate,data and user account migrate to other linux server

前言

环境:

主机A: hostname:fc17 (在其上配置rsh,xinetd),OS版本fedora 17,IP:192.168.0.118

主机B:hostname:centos ,版本centos 6.2 IP:192.168.0.119

要实现迁移用户账户和文件从主机A到B这个目标。

账户方面:需要保证账户正确,用户口令不变,登录正常。

文件方面:需要保证文件属性-时间,owner,group,权限等都不改变,软,硬链接不变。

大概方法:

建立fc17和centos2个虚拟机进行试验。在主机A fc17上建立一些模拟用的账户的文件。然后迁移到主机B CentOS上。

建立虚机和安装fedora,centos过程此处略。

1.主机A上查看当前Fedora用户

cat /etc/passwd

cut -d: -f1 /etc/passwd 只查看用户名

root
bin
daemon
adm
lp
sync
shutdown
halt
mail
uucp
operator
games
gopher
ftp
nobody
dbus
saslauth
avahi-autoipd
sshd
mailnull
smmsp

2.主机A建立模拟账户,备份账户信息,在主机B上还原

2.1如下语句批处理建立20个用户

groupadd -g    10000    grp00
groupadd -g    10010    grp10
groupadd -g    10020    grp20
groupadd -g    10030    grp30
groupadd -g    10040    grp40
groupadd -g    10050    grp50
groupadd -g    10060    grp60
groupadd -g    10070    grp70
groupadd -g    10080    grp80
groupadd -g    10090    grp90
groupadd -g    10100    grp100
groupadd -g    10110    grp110

useradd -u 10000 -g 10000 -d /home/user01 -m -s /bin/bash user01; echo "passwordpassword" | passwd --stdin user01
....
....

2.2备份账户信息

# 建立备份文件夹
mkdir /root/move/
# Setup UID filter limit
export UGIDLIMIT=500
# Now copy /etc/passwd accounts to /root/move/passwd.mig using awk to filter out system account (i.e. only copy user accounts)
awk -v LIMIT=$UGIDLIMIT -F: '($3>=LIMIT) && ($3!=65534)' /etc/passwd > /root/move/passwd.mig
# Copy /etc/group file:
awk -v LIMIT=$UGIDLIMIT -F: '($3>=LIMIT) && ($3!=65534)' /etc/group > /root/move/group.mig
# Copy /etc/shadow file:
awk -v LIMIT=$UGIDLIMIT -F: '($3>=LIMIT) && ($3!=65534) {print $1}' /etc/passwd | tee - |egrep -f - /etc/shadow > /root/move/shadow.mig
# Copy /etc/gshadow (rarely used):
cp /etc/gshadow /root/move/gshadow.mig
 

2.3 用后面章节讨论的各种方法拷贝备份文件到主机B,并准备软/硬链接测试文件。

例如使用scp

scp -r /root/move/* user@centos:/path/to/location

在/root/下建立一个intall.log文件。

在/root/move下建立install.log的软/硬链接文件:softlink.log hdlink.log

cd /root

touch install.log

cd /root/move

ln ../install.log hdlink.log       #创建install.log的一个硬连接文件hdlink.log

ln –s ../install.log softlink.log #创建install.log的一个软连接文件softlink.log

ls –li                                            #-i参数显示文件的inode节点信息

注意hdlink.log文件的inode号和install.log的inode号一致。引用数为2.

image

image

2.4 主机B上还原备份账户信息

cat passwd.mig >> /etc/passwd
cat group.mig >> /etc/group
cat shadow.mig >> /etc/shadow
/bin/cp gshadow.mig /etc/gshadow

3.scp 拷贝可以保留权限信息,但是无法保留文件属主和组,文件建立时间也无法保留。

如图:
原文件
image
 
拷贝到centOS后
image

SCP的其他限制:

-C 选项可以压缩文件,传输大文件的时候可以显著降低时间

缺省使用3DES加密,使用-c blowfish加密可以加快速度

SCP不适用于拷贝大量文件,每个文件会生成一个进程,既慢又耗资源。最多15个同步进程。

不能识别符合连接文件,仍旧会拷贝。

SCP也不会保留软链接:如图:

gshadow.mig的owner和group没有保留,softlink.log也没有保持软链接状态。

image

4.RCP

如果使用RCP则没有加密开销,速度可比scp快2倍,但是和scp一样,不识别符号链接

安装rcp,

yum install –y rsh

环境:

主机A: hostname:fc17 (在其上配置rsh,xinetd)

主机B:hostname:centos

测试过程(没有注明的则是在主机A上操作):

4.1.安装包

yum install –y rsh-server

yum install –y xinetd

4.2.开启服务

到/etc/pam.d/目录下,把rsh文件中的auth required /lib/security/pam_securetty.so

一行用“#”注释掉即可。(只有注释掉这一行,才能用root用户登录)

/sbin/chkconfig xinetd on (设定开机运行服务)

/sbin/chkconfig rsh on

service xinetd start          (启动xinetd服务)

netstat -tunlp|grep 514 (检查服务起来了)

service iptables stop (实验方便起见,停掉防火墙

sestatus  (确定当前SELinux的状态)

image

vi /etc/selinux/config

SELINUX=disable   (修改点,否则root从主机B上无法rsh到A上)

修改selinux,如何生效
  (1)、重启系统生效。
  (2)、执行下面命令生效。
  #setenforce 0

4.3.配置/etc/hosts.equiv

echo centos >>/etc/hosts.equiv

chmod 600 /etc/hosts.equiv

echo rsh >>/etc/securetty

4.4.如果是root用户,配置root用户下的.rhosts

echo centos root >>.rhosts

chmod 600 .rhosts

4.5.测试

su testuser1

本地测试:rsh fc17 true

su (切换成root)

rsh fc17 uname –a

image

root登录主机B

rsh fc17 uname –a

5.netcat

On the receiving end do:
# netcat -l -p 7000 | tar x

And on the sending end do:
# tar cf - * | netcat otherhost 7000

6.nc and tar

On the receiving end do:
# nc -l 7000 | tar -xpf -

And on the sending end do:
# tar -cf - * | nc otherhost 7000

receiver# nc -l 1234 | tar -xvf -
sender# tar -cvf - * | nc receiver 1234

1. The sender

The sender has to call netcat in server mode and pipe content into it. The next line tells tar to build a tarball and write it to standard output which is redirected via a pipe to netcat. Netcat is told to start in server mode (-l), listen on port 7878 (-p 7878) and shutdown itself after waiting 10 seconds after having seen an end of file in standard input (-q 10):

$ tar c directory | nc -q 10 -l -p 7878

2. The receiver

The receiver has to call netcat and tell him to connect to the remote machine and the correct port and redirects the standard output to a file. For convenience he also sets a timeout parameter (-w 10):

$ nc -w 10 remotehost 7878 > nameoftar.tar

实例:从centos 上传输到fc17,注意文件时间和权限,owner/group都保留下来了。

image

image

7. tar 和 ssh

tar cvf - dir/ | ssh server2 "cd /dest/dir && tar xvf –"   

7.1没有软链接和硬链接的情况:

如图范例:注意打包的时候tar要用相对路径,否则使用绝对路径那么解开也是绝对路径没法恢复到其他路径。
image

7.2 有软链接和硬链接的情况:(命令和7.1一样)

主机A上:

image

主机B上结果:软/硬链接都被复制

image

但是硬链接文件hdlink.log可以查看,而且内容一致。

但是软链接文件softlink.log无法查看,提示无此文件。

8.rsync

rsync -avz /folders/  user@targethost:/destination/

or with ssh :

Code:

rsync -avz -e ssh  /folders/  user@targethost:/destination/
命令参数说明:
-a archive mode
-r 递归
 
注意主机A和B都必须要安装rsync. yum install –y rsysnc.
否则会报错如下:

[root@centos move]# rsync -arvze ssh root@fc17:/root/move/ .
root@fc17's password:
bash: rsync: command not found
rsync: connection unexpectedly closed (0 bytes received so far) [receiver]
rsync error: error in rsync protocol data stream (code 12) at io.c(600) [receiver=3.0.6]

成功实例:
image
包含P(显示进度)参数,复制包含子文件夹实例:
image
比较文件属性和时间-只检查修改时间即可。

 ls -lc filename 列出文件的 ctime (最后更改时间)

  ls -lu filename 列出文件的 atime(最后存取时间)

  ls -l filename 列出文件的 mtime (最后修改时间)

image
 
image

9.Moving LVM images between machines without ssh

On the receiving end do:
# nc -l 7000 | dd of=/dev/mapper/vgfoo-lvbar bs=32768

And on the sending end do:
# dd if=/dev/mapper/vgbaz-lvquz bs=32768 | nc otherhost 7000

10.使用NFS和CP--(个人以为传输效率最好)

10.1安装必要软件:

主机B上

yum install –y portmap

主机A上

yum install –y portmap

yum install –y showmount

10.2设置

主机B上

vi /etc/exports,增加如下:

/home fc17(rw,no_root_squash)

chkconfig nfs on 

service nfs start

启动服务

rpcbind

rpcinfo –p (查看)

查看共享目录

[root@centos home]# exportfs
/home           fc17

主机A上

showmount –e centos

image

mount -t nfs4 centos:/home  /mnt

unalias cp (取消cp别名cp -i,否则会每次提示是否要覆盖)

cp -ardv /home/* /mnt

-a 存档,保留时间,owener,权限等

-r 递归

-d 保留链接

原文地址:https://www.cnblogs.com/jjkv3/p/3104109.html