Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use mkfs -t ext4 to keep data on existing volume but delete on new? [closed]

I am attaching block volume in OCI to a new or existing instance using a script (below). However, if the volume already has a file system type already assigned I will lose all my data!

Is there a way only to run the command sudo mkfs -t ext4 /dev/oracleoci/oraclevdb only if it's not already formatted?

Is there a way to run line 1 below, only if the attached volume is not already formatted?

sudo mkfs -t ext4 /dev/oracleoci/oraclevdb
sudo mkdir /data
sudo mount /dev/oracleoci/oraclevdd /data
df -h

The issue is every time a new instance is created using an existing volume, I lose all my data. However, I want to keep the behaviour for new instances were attaching a new volume.

So something like...

if condition x
   sudo mkfs -t ext4 /dev/oracleoci/oraclevdb
else
 do nothing

I'm running Oracle Linux 8.

like image 593
MarkK Avatar asked Sep 02 '25 16:09

MarkK


1 Answers

Just check if you can mount it.

if ! sudo mount /dev/oracleoci/oraclevdd /data; then
     if ! sudo mkfs -t ext4 /dev/oracleoci/oraclevdb; then
          echo "och nooo, formatting fialed"
     fi
     if ! sudo mount /dev/oracleoci/oraclevdd /data; then
          echo "Och nooooo, can't mount after formatting, that's odd"
     fi
fi
like image 92
KamilCuk Avatar answered Sep 04 '25 06:09

KamilCuk