top of page

Linux Disk Space, Mounting, and Backups

Writer: compnomicscompnomics



Maintaining a healthy Linux system involves managing disk space, understanding file system mounting, and implementing regular backups. This post will guide you through essential commands and concepts to ensure your system runs smoothly.


Managing Disk Space:

  • df (Disk Free):

    • Displays disk space usage for file systems.

    • df -h (human-readable format) is commonly used.

    • Example: df -h

  • du (Disk Usage):

    • Displays disk space usage for files and directories.

    • du -sh (summarize and human-readable) is commonly used.

    • Example: du -sh /home/user


Mounting and Unmounting File Systems:

  • What is Mounting?

    • In Linux, a file system must be "mounted" to a directory (mount point) before it can be accessed.

    • Mounting makes the file system's contents available within the directory hierarchy.

    • This applies to physical drives, network shares, and even ISO images.

    • Example: if you have a usb drive, you must mount it to a directory like /mnt/usb to access the files contained on it.

  • Mounting:

    • sudo mount /dev/sdb1 /mnt/usb (mounts /dev/sdb1 to /mnt/usb).

    • You may need to create the mount point directory first: sudo mkdir /mnt/usb.

  • Unmounting:

    • sudo umount /mnt/usb (unmounts /mnt/usb).

    • It is important to unmount the file system before removing the device to prevent data corruption.

  • The /etc/fstab file is used to automatically mount file systems at boot time.


Taking Backups Using tar:

  • What is a Backup and Why is it Important?

    • A backup is a copy of your data, stored separately from the original.

    • Backups are crucial for:

      • Data recovery in case of hardware failure.

      • Restoring files after accidental deletion.

      • Protecting against data corruption or malware.

    • tar (tape archive) is a widely used command for creating archive files.

  • tar Command:

    • tar -cvf archive.tar /path/to/files (creates an archive).

      • c: create.

      • v: verbose (shows files being processed).

      • f: filename (specifies the archive filename).

    • tar -xvf archive.tar (extracts an archive).

      • x: extract.

    • tar -czvf archive.tar.gz /path/to/files (creates a compressed archive).

      • z: gzip compression.

    • tar -tzvf archive.tar.gz (lists the contents of a compressed archive).

      • t: list.

  • Example: tar -czvf mybackup.tar.gz /home/user/documents


Practice Set for tar Command Backup:

  1. Create a directory named "test_backup" and add some files to it.

mkdir test_backup
touch test_backup/file1.txt test_backup/file2.txt
echo "This is test data" > test_backup/file3.txt
  • Create a compressed archive of the "test_backup" directory named "backup.tar.gz".

tar -czvf backup.tar.gz test_backup
  • List the contents of the "backup.tar.gz" archive.

tar -tzvf backup.tar.gz
  • Create a new directory named "restore_backup".

mkdir restore_backup
  • Extract the contents of "backup.tar.gz" into the "restore_backup" directory.

tar -xzvf backup.tar.gz -C restore_backup
  • The -C flag is used to change directories before extracting.

  • Verify that the files were successfully extracted.

ls restore_backup
  • Create an uncompressed archive of the test_backup directory called uncompressed.tar

tar -cvf uncompressed.tar test_backup
  • Extract the contents of uncompressed.tar into a new directory called uncompressed_restore

mkdir uncompressed_restore
tar -xvf uncompressed.tar -C uncompressed_restore


 
 
 

Comments

Rated 0 out of 5 stars.
No ratings yet

Add a rating
bottom of page