CodeGym /Courses /Docker SELF /Formatting Disks, Mounting, Backups with `rsync`

Formatting Disks, Mounting, Backups with `rsync`

Docker SELF
Level 6 , Lesson 6
Available

1. Step 1: Formatting a New Disk

Welcome to the hands-on part of our journey through file systems! Today we’re bringing together everything we learned in previous lectures about formatting, mounting, and backups. Oh, and if you ever accidentally format your boss’s flash drive with all their documents, you’ll totally know how to fix it. Or at least you'll be prepared to pivot into a new career path.

Scenario

Imagine you’ve added a new disk to your computer, but right now it’s just sitting there, empty and unconfigured for your system. We’re going to format it and get it ready for action.

Procedure:

1.1 Check Connected Devices

First, we need to figure out where our new disk is. Let’s use the lsblk command:

lsblk

If everything’s connected correctly, you’ll see something like this:

NAME   MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
sda      8:0    0  100G  0 disk 
├─sda1   8:1    0   50G  0 part /
├─sda2   8:2    0   50G  0 part /home
sdb      8:16   0  500G  0 disk

Here, sdb is our new disk. It doesn’t have any partitions yet.

1.2 Create a File System on the Disk

Let’s assume the ext4 file system suits your needs. To create it, use the mkfs command:

sudo mkfs.ext4 /dev/sdb

After running this command, the system will be ready to work with your disk. Remember, formatting wipes all data on the device. So, if at this point you’ve realized this is your boss’s disk and you’re about to format it—stop. Right now.


2. Step 2: Mounting a New Disk

Now let's connect the freshly formatted disk to the file system.

2.1 Creating a Mount Point

We need a place where the disk will be connected. It's usually a folder. For instance, let's create a folder in /mnt:

sudo mkdir /mnt/newdisk

2.2 Mounting the Disk

Let's attach the disk to the system:

sudo mount /dev/sdb /mnt/newdisk

To check if everything is working, use the df command:

df -h

You should see /mnt/newdisk and your disk in the list.

2.3 Setting Up Auto-Mount

To avoid manually specifying the disk each time, let's add it to the /etc/fstab file. To do this, find the UUID of the device using blkid:

sudo blkid /dev/sdb

The output will be something like:

/dev/sdb: UUID="abcd-1234-efgh-5678" TYPE="ext4"

Add this line to /etc/fstab:

UUID=abcd-1234-efgh-5678 /mnt/newdisk ext4 defaults 0 2

Now the disk will auto-mount every time the system starts up.


3. Step 3: Archiving Data

Let’s say your disk has a folder /mnt/newdisk/data that you want to archive and compress before backing it up.

3.1 Creating an Archive with tar

We'll create an archive of the data folder:

tar -cvf data_backup.tar /mnt/newdisk/data

3.2 Compressing the Archive with gzip

Compress the created archive:

gzip data_backup.tar

Now you have a file data_backup.tar.gz. It's smaller and easier to move around.


4. Step 4: Backup using rsync

We're ready for backups. Let's use rsync to transfer data to another server or disk location.

4.1 Local Copy

If you want to backup the data folder to another directory, run:

rsync -av /mnt/newdisk/data /mnt/backup/

Keys:

  • -a enables archive mode, preserving file permissions and structure.
  • -v enables verbose output.

4.2 Copy to a Remote Server

If you need to transfer data to a server, use:

rsync -av /mnt/newdisk/data username@remote_server:/backup/

Don't forget to replace username with the username on the server and remote_server with the server address.


5. Step 5: Simple Backup Using scp

Sometimes it's easiest to just use scp. For example, if we wanna directly transfer our archive to a server:

scp data_backup.tar.gz username@remote_server:/backup/

This will copy the file data_backup.tar.gz to the remote server in the folder /backup.


6. Step 6: Check Everything Works Properly

Here's a quick plan of what we've done:

  1. We created a file system on the new disk.
  2. Mounted the disk and set up auto-mounting for it.
  3. Archived and compressed data from a folder.
  4. Created a backup using rsync and scp.

Now check if your copied data is accessible. Try extracting data_backup.tar.gz on the target server:

gunzip data_backup.tar.gz
tar -xvf data_backup.tar

Your data is back, safe and sound. You can breathe easy now.


7. Features and Possible Errors

  • If you see a message like device or resource busy when unmounting, make sure the device isn't being used by a process. Use fuser to check:

    fuser -m /mnt/newdisk
    
  • If you mistakenly configure /etc/fstab, your system might not boot. Use recovery mode or test your changes in advance:

    sudo mount -a
    
  • When backing up using rsync, don't forget the --delete flag if you want to sync directories by removing extra data on the target side.

Now you're armed with the knowledge to confidently manage disks, mount them, and keep your data safe with backups. And the best part — you can look all serious while explaining why there's a new hard drive on your desk and why you're formatting it. 😉

1
Task
Docker SELF, level 6, lesson 6
Locked
Determining the list of disks and their partitions
Determining the list of disks and their partitions
1
Task
Docker SELF, level 6, lesson 6
Locked
Creating a File System
Creating a File System
1
Task
Docker SELF, level 6, lesson 6
Locked
Mounting a new disk
Mounting a new disk
1
Task
Docker SELF, level 6, lesson 6
Locked
Backup data using `rsync`
Backup data using `rsync`
1
Survey/quiz
Working with Devices in Linux, level 6, lesson 6
Unavailable
Working with Devices in Linux
Working with Devices in Linux
Comments
TO VIEW ALL COMMENTS OR TO MAKE A COMMENT,
GO TO FULL VERSION