Rescuing old external hard drives

hard disk drive From Azamat Esenaliev Pexels

The Backstory

Years ago, I bought some kind of network connected back up drive, RAID array, two drives of 500Gb each. It was painfully slow as a drive and I hated it. It also failed (the box, not the drives). I bought some kind of non-branded housing for the two drives. At least one of them seems to work, but it’s always a faff. However, I have accumulated enough data that (a) I need to upgrade the HDD in my computer or buy a newer one (I have 500Gb) or (b) I should keep my back up data on a back up drive and (c) I could possibly use an external drive as my PostgreSQL data store. I don’t count my personal drive as my “production” drive, I just use it for development. When it’s all working, the “production” drive is on a server.

Anyway, one thing at a time.

Diagnosing the drive

If I power up the drive(s), plug them into the USB and run lsblk I get the following:

NAME      MAJ:MIN RM   SIZE RO TYPE  MOUNTPOINTS
loop0       7:0    0  50.8M  1 loop  /snap/snapd/25202
loop1       7:1    0  73.9M  1 loop  /snap/core22/2082
loop2       7:2    0     4K  1 loop  /snap/bare/5
loop3       7:3    0  19.7M  1 loop  /snap/blanket/49
loop4       7:4    0  91.7M  1 loop  /snap/gtk-common-themes/1535
loop5       7:5    0   516M  1 loop  /snap/gnome-42-2204/202
loop6       7:6    0  73.9M  1 loop  /snap/core22/2111
sda         8:0    0 476.9G  0 disk
├─sda1      8:1    0   300M  0 part  /boot/efi
├─sda2      8:2    0 459.6G  0 part  /
└─sda3      8:3    0    17G  0 part  [SWAP]
sdb         8:16   0 465.8G  0 disk
├─sdb1      8:17   0     1K  0 part
├─sdb2      8:18   0 464.8G  0 part
│ └─md126   9:126  0 464.8G  0 raid1 /media/phewson/d13f5bfc-9f48-403c-9e2a-fa262dfd63fd
├─sdb5      8:21   0 125.4M  0 part
│ └─md123   9:123  0     0B  0 md
├─sdb6      8:22   0   7.8M  0 part
├─sdb7      8:23   0   7.8M  0 part
│ └─md124   9:124  0   7.8M  0 raid1
├─sdb8      8:24   0 172.5M  0 part
│ └─md127   9:127  0 172.4M  0 raid1
├─sdb9      8:25   0 658.9M  0 part
│ └─md125   9:125  0 658.8M  0 raid1 /media/phewson/717f9df9-e057-497a-8a94-3beb13f44de7
└─sdb10     8:26   0   7.8M  0 part
sr0        11:0    1 217.3M  0 rom

So I can see the raid on various parts of sdb.

If I run mdadm it’s aware that some parts of sdb have been partitioned for RAID

sudo mdadm --examine /dev/sdb
/dev/sdb:
   MBR Magic : aa55
Partition[0] :      2008062 sectors at           63 (type 05)
Partition[1] :    974759940 sectors at      2008125 (type 83)

I had a look at lsof, which wasn’t very helpful:

sudo lsof /dev/sdb2 lsof:
WARNING: can't stat a few things

as well as mdstat which was a lot more informative:

cat /proc/mdstat
Personalities : [linear] [multipath] [raid0] [raid1] [raid6] [raid5] [raid4] [raid10]
md123 : inactive sdb5[0](S) 128384 blocks
md124 : active (auto-read-only) raid1 sdb7[0] 7936 blocks [2/1] [U_]
md125 : active raid1 sdb9[0] 674624 blocks [2/1] [U_]
md126 : active raid1 sdb2[0] 487379904 blocks [2/1] [U_]
md127 : active (auto-read-only) raid1 sdb8[0] 176576 blocks [2/1] [U_]
unused devices: <none>

Just out of curiousity, I had a look at some parts of sdb

sudo blkid /dev/sdb1
/dev/sdb1: PTTYPE="dos" PARTUUID="2a5d7770-01"
sudo blkid /dev/sdb2
/dev/sdb2: UUID="910d1eca-6227-2501-5909-be635de80a26" TYPE="linux_raid_member" PARTUUID="2a5d7770-02"
sudo blkid /dev/sdb9
/dev/sdb9: UUID="ac13f357-66f6-48ba-14ec-38c93cd6cc71" TYPE="linux_raid_member" PARTUUID="2a5d7770-09"

I tried mounting this as RAID, but it wouldn’t play. I think that might be my fault as I have used it for backups since the drives were moved to this new box.

Mounting and permissions

However, I could mount and remount the drive to enable read write, and changed the permission so I could be the one doing the writing.

sudo mkdir -p /mnt/base
sudo mount /dev/md126 /mnt/base
sudo mount -o remount,rw /dev/md126 /mnt/base
sudo chown -R phewson:phewson datastore/

(maybe that last should have been sudo chown -R phewson:phewson /mnt/base )

The last thing I did was to create a symlink so that it’s easier to find the data once the files are mounted.

ln -s /mnt/base/share/datastore ~/DATA

Future plans

I might arrange this to mount automatically on booting (I’m not sure I always want this powered up though at the moment, so I’m not doing this yet). I can get the UUID

sudo blkid /dev/md126
/dev/md126: UUID="d13f5bfc-9f48-403c-9e2a-fa262dfd63fd" BLOCK_SIZE="512" TYPE="xfs"

and so if I added this line to `/etc/fstab

UUID=d13f5bfc-9f48-403c-9e2a-fa262dfd63fd /mnt/base xfs defaults 0 2

it could be mounted at boot time. Whether or not I do this will depend on how much I worry about the wasted energy of always having the drive on versus getting frustrated reminding myself how to run the mounting routine.

Another thing I haven’t looked at yet, but I could remove the unused part of the RAID system

sudo mdadm --stop /dev/md123
sudo mdadm --remove /dev/md123

(these are only safe if the array really is unused, I was in the middle of copying so left this well alone)

For my home PC (it’s a bit small, and I really want to run my hobby projects on it, I don’t want it taken over with work), I’m thinking I could run PostgreSQL using the external drive as it’s datastore.

sudo mkdir -p /mnt/base/postgres-data
sudo chown -R 999:999 /mnt/base/postgres-data

(because I’m running postgres from docker, hence user 999). Then adjusting the dockerfile to use this volume could involve something like this (in my compose.yml).

docker run -d \
  --name pg-external \
  -e POSTGRES_PASSWORD=yourpassword \
  -v /mnt/base/postgres-data:/var/lib/postgresql/data \
  postgres:latest

However, that is a project for another day, as is generally tidying up these external drives. I do think I’d get best use of them if I repartitioned them in a manner of my choosing. I don’t even need RAID, I could just rsync from one drive to the other now and again.