把/home目录下的文件挂载到新设备上(目录迁移)

1.备份/home/

[root@centos6 ~]# cp -ar /home/ /data/ ##-a保留全部属性,-r表示递归复制目录及其子目录内的所有内容


[root@centos6 ~]# ll /data/home/
总用量 8
drwx------. 28 cwj  cwj  4096 10月  5 2019 cwj
drwx------   4 mage mage 4096 4月  21 2019 mage
[root@centos6 ~]# ll  /home
总用量 8
drwx------. 28 cwj  cwj  4096 10月  5 2019 cwj
drwx------   4 mage mage 4096 4月  21 2019 mage
[root@centos6 ~]# 

2、利用新的磁盘划分出新的分区,并在分区上创建文件系统。

1、添加一块10G硬盘
在这里插入图片描述
2、识别硬盘

[root@centos6 ~]# echo "- - -" > /sys/class/scsi_host/host0/scan
[root@centos6 ~]# lsblk
NAME   MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
sda      8:0    0  200G  0 disk 
├─sda1   8:1    0    1G  0 part /boot
├─sda2   8:2    0 48.8G  0 part /
├─sda3   8:3    0 29.3G  0 part /data
├─sda4   8:4    0    1K  0 part 
└─sda5   8:5    0    2G  0 part [SWAP]
sr0     11:0    1  8.1G  0 rom  /mnt/cdrom
sr1     11:1    1  3.7G  0 rom  
sdb      8:16   0   10G  0 disk 

3、创建分区

[root@centos6 ~]# fdisk /dev/sdb
Device contains neither a valid DOS partition table, nor Sun, SGI or OSF disklabel
Building a new DOS disklabel with disk identifier 0x2276d648.
Changes will remain in memory only, until you decide to write them.
After that, of course, the previous content won't be recoverable.

Warning: invalid flag 0x0000 of partition table 4 will be corrected by w(rite)

WARNING: DOS-compatible mode is deprecated. It's strongly recommended to
         switch off the mode (command 'c') and change display units to
         sectors (command 'u').

Command (m for help): n
Command action
   e   extended
   p   primary partition (1-4)
p
Partition number (1-4): 1
First cylinder (1-1305, default 1): 
Using default value 1
Last cylinder, +cylinders or +size{K,M,G} (1-1305, default 1305): +2G

Command (m for help): w
The partition table has been altered!

Calling ioctl() to re-read partition table.
Syncing disks.
[root@centos6 ~]# lsblk
NAME   MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
sda      8:0    0  200G  0 disk 
├─sda1   8:1    0    1G  0 part /boot
├─sda2   8:2    0 48.8G  0 part /
├─sda3   8:3    0 29.3G  0 part /data
├─sda4   8:4    0    1K  0 part 
└─sda5   8:5    0    2G  0 part [SWAP]
sr0     11:0    1  8.1G  0 rom  /mnt/cdrom
sr1     11:1    1  3.7G  0 rom  
sdb      8:16   0   10G  0 disk 
└─sdb1   8:17   0    2G  0 part 

3、在分区上创建文件系统mkfs.ext4 /dev/sdb1 -L /home

[root@centos6 ~]# mkfs.ext4 /dev/sdb1 -L /home
mke2fs 1.41.12 (17-May-2010)
文件系统标签=/home
操作系统:Linux
块大小=4096 (log=2)
分块大小=4096 (log=2)
Stride=0 blocks, Stripe width=0 blocks
131648 inodes, 526120 blocks
26306 blocks (5.00%) reserved for the super user
第一个数据块=0
Maximum filesystem blocks=541065216
17 block groups
32768 blocks per group, 32768 fragments per group
7744 inodes per group
Superblock backups stored on blocks: 
	32768, 98304, 163840, 229376, 294912

正在写入inode表: 完成                            
Creating journal (16384 blocks): 完成
Writing superblocks and filesystem accounting information: 完成

This filesystem will be automatically checked every 20 mounts or
180 days, whichever comes first.  Use tune2fs -c or -i to override.
[root@centos6 ~]# blkid | grep /dev/sdb1
/dev/sdb1: LABEL="/home" UUID="fd06cd0f-0209-4621-827d-ecd42ecce1f9" TYPE="ext4"

4、创建挂载点,并把新分区挂载到新建的挂载点上。mkdir /mnt/home ;mount /dev/sdb1 /mnt/home(充当临时目录,用于把/home目录中的内容导入到新的磁盘上)

[root@centos6 ~]# mkdir /mnt/home
[root@centos6 ~]# mount /dev/sdb1 /mnt/home
[root@centos6 ~]# df -h
Filesystem      Size  Used Avail Use% Mounted on
/dev/sda2        48G  3.8G   42G   9% /
tmpfs           932M  4.0K  932M   1% /dev/shm
/dev/sda1       976M   40M  886M   5% /boot
/dev/sda3        29G   71M   28G   1% /data
/dev/sr0        8.1G  8.1G     0 100% /mnt/cdrom
/dev/sdb1       2.0G  3.1M  1.9G   1% /mnt/home

5、进入单用户模式init 1(避免其他用户访问,造成数据不一致,该命令会导致断网)

[root@centos6 ~]# init 1

6、把原来/home目录里的所有内容复制到新分区中cp -av /home/* /mnt/home(注意:这不会复制隐藏文件及隐藏文件夹,但是一般home目录下不会有隐藏文件)

在这里插入图片描述
在这里插入图片描述

7、删除原先的家目录rm -rf /home/* ,后面将会作为挂在点使用。

在这里插入图片描述

8、把/mnt/home目录挂载至原先的/home目录上。

vim /etc/fstab
读入命令的输出
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

9、卸载临时挂载点,并删除/mnt/home上的数据。umount /mnt/home;rm -rf /mnt/home

在这里插入图片描述

10、切换回图形模式init 5。

在这里插入图片描述
验证:
切换到cwj用户,新建测试文件。

[root@centos6 ~]# su cwj
[cwj@centos6 root]$ cd
[cwj@centos6 ~]$ pwd
/home/cwj
[cwj@centos6 ~]$ mkdir testfile
[cwj@centos6 ~]$ 

[root@centos6 ~]# ll /home
总用量 24
drwx------. 29 cwj  cwj   4096 4月  18 17:30 cwj
drwx------   2 root root 16384 4月  18 16:09 lost+found
drwx------   4 mage mage  4096 4月  21 2019 mage

注意:根分区无法迁移

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值