3

I have a 4TB hard drive. I mounted that hard drive in /var, to be able to store and make backups of a production server, and I came up with the idea of creating a RAID 1. For that I requested another 4TB hard drive although not the same make and model, but I think it will work...

I would like to create a RAID 1 without losing data already stored. But the information I have found is not as complete as one would like...

Zanna
  • 70,465

1 Answers1

4

Fast view of what we are going to do:

  1. If new disk hasn't partition created, create partition
  2. Stop all process that are using /var
  3. Remove /var from /etc/fstab, to stop mount at start server
  4. Restart server
  5. Create RAID 1(md0) in new disk/partition(/dev/sdc1), and format the new partition(/dev/md0) with ext4
  6. Clone information from old partition(/dev/sdb1) to new partition(/dev/md0)
  7. Then add old partition(/dev/sdb1) to RAID 1(md0)
  8. Add RAID 1(md0) to /etc/fstab to mount at start server
  9. Restart

Step by step

Note: Original disk: /dev/sdb partition /dev/sdb1, New disk: /dev/sdc

Create a new partition in new disk:
If you hasn't create a partition in new disk use gdisk since fdisk can not do a partition larger than 2TB, after create new partition(/dev/sdc1) you can check with lsblk

gdisk /dev/sdc
# Type letter n
# Then enter
# Then enter
# Then enter
# Then enter
# Type letter w to write GPT data...

Format(ext4) the new partition(/dev/sdc1) with mkfs.ext4
We need to format the new partition to mount RAID 1 in it, you can check the new format in the partition(/dev/sdc1) with lsblk

mkfs.ext4 /dev/sdc1

Remove /dev/sdb1 from mount at start server to stop using files
We want to copy all files from old disk/partition(/dev/sdb1) to new disk/partition(/dev/md0 that is under sdc1), so first, we need to free files from process, we need to remove /dev/sdb1 from mount at /var, you can't umount /dev/sdb1 because Linux and others process use some files in /var.

nano /etc/fstab
# comment the line with # at start of the line like this:
# #UUID=24f081e2-fe9d-4ec1-81a6-c5c2d71d6c9b      /var    ext4    defaults        0       2
# to prevent /dev/sdb1 mount at /var

Restart server
We need to reboot to take effect...

reboot now

Adding new disk/partition to RAID 1
We need to add the new partition(/dev/sdc1) to mdadm to create RAID 1, format it with ext4

mdadm --create --verbose /dev/md0 --force --level=1 --raid-devices=1 /dev/sdc1
# type y to continue

after create md0, we need to format with ext4

mkfs.ext4 /dev/md0

use lsblk -f to check partitions and FSTYPE

Copy data from old disk/partition to new disk/partition
We need to copy old data from old partition /dev/sdb1 to new partition /dev/md0, first create two folders to mount the two partitions and then clone data with rsync or cp...

cd /mnt
# go to path /mnt, I prefer this path because we are going to mount(mnt), you can create it wherever you want, and mount it

mkdir md0 var

create two folder md0 for /dev/sdc1

And var for /dev/sdb1

mount /dev/md0 md0

mount /dev/sdc1 in md0

mount /dev/sdb1 var

mount /dev/sdb1 in var

Before this command check very well before, because it has attribute -delete, it deletes files in the destination(md0) that are not found in the source(var/)

check "Copy entire file system hierarchy from one drive to another" and "rsync how to do a checksum on rsync" links

rsync -axcHAWXS -delete --numeric-ids --info=progress2 var/ md0/

Copy data from old to new partition

I added 2 attributes here c and -delete

c: for checksum

-delete: to delete files in new disk/partition that not exist in source or old disk/partition

NOTE IF DATA IS LARGE AND YOU WANT TO SLEEP OR SOMETHING USE nohup & exit TO RUN IN BACKGROUND, AND CHECK "nohup.out" FOR CHANGES, LIKE "tail -f nohup.out" OR "htop" SEARCH FOR "rsync"

nohup rsync -axcHAWXS -delete --numeric-ids --info=progress2 var/ md0/ & exit

umount partitions
we need to umount partitions

umount /dev/sdb1
umount /dev/md0

Add old disk/partition to RAID 1
We need to add the old disk/partition(/dev/sdb1) to RAID 1(md0), check status with cat /proc/mdstat or mdadm --detail /dev/md0

sudo mdadm --grow /dev/md0 --raid-devices=2 --add /dev/sdb1
# check progress with cat /proc/mdstat
# or sudo mdadm --detail /dev/md0 
# For progress 
# Number   Major   Minor   RaidDevice State
#    0       8       33        0      active sync   /dev/sdc1
#    1       8       17        1      spare rebuilding   /dev/sdb1
# Check "spare rebuilding" and "Rebuild Status"

Mount RAID 1(md0) to /var
We need to do some changes to mdadm.conf and we need /var folder

mount /dev/md0 /var
# mounting md0 to /var, check that exist /var forlder if not, create it with mkdir /var

Add RAID 1 to /etc/mdadm/mdadm.conf
We need to add the new array RAID 1(md0) to /etc/mdadm/mdadm.conf

mdadm --detail --scan >> /etc/mdadm/mdadm.conf
# Add output of  mdadm --detail --scan
# To /etc/mdadm/mdadm.conf
# You can edit it manually

update-initramfs -u

To take effect the new configuration

Add RAID 1 to /etc/fstab to mount at start of server
We need to add /dev/md0 to /etc/fstab, we want to mount it at start of the server, first we need the UUID, we can use lsblk -f or blkid

blkid
# get /dev/md0 UUID

nano /etc/fstab

At the end add this with you UUID

UUID=0518f527-4019-4a51-b321-7ad266e1df64 /var ext4 defaults 0 2

Change the UUID with your UUID

check errors with

findmnt --verify

Restart server and check results

reboot now

Links that will help you:

How to Create a New Ext4 File System (Partition) in Linux
Copy entire file system hierarchy from one drive to another
rsync how to do a checksum on rsync
Remove mdadm raid1 without loosing data
How to Create Partitions in Linux Setting up RAID 1 on 14.04 with an existing drive Using mdadm --examine to write mdadm.conf My RAID 1 always renames itself to /dev/md127 after rebooting | DEBIAN 10 Mount drive in linux and set auto-mount at boot