Skip to content
Tutorials Center
My learning

Topics

Lesson 18 of 37 ยท Storage and Filesystems

Disk Usage, Quotas and Backups with rsync and tar

Find what fills a disk with df, du and ncdu, limit users with quotas, and build reliable backups with tar, rsync and snapshots following the 3-2-1 rule and restore tests.

Two storage emergencies are close to universal: a full disk and a lost file. This lesson shows how to find out what uses space, how to stop one user or directory from taking all of it, and how to build backups you have actually proved you can restore.

Finding what fills a disk

Start wide, then narrow.

Terminal
df -h                              # which filesystem is full?
df -i                              # or out of inodes?
sudo du -xh --max-depth=1 /var | sort -h | tail
sudo du -xh --max-depth=1 /var/log | sort -h | tail
sudo ncdu -x /                     # interactive browser, if installed
sudo find / -xdev -type f -size +500M -exec ls -lh {} +

-x (or -xdev) stays on one filesystem so you do not wander into others. Typical culprits: logs, package caches (apt clean, dnf clean all), journals (journalctl --vacuum-size=500M), old kernels, container images, core dumps and user downloads.

A classic puzzle: you delete a huge log but df still shows a full disk. A process still holds the deleted file open. Find it and restart the program, or truncate instead of deleting:

Terminal
sudo lsof +L1                      # open files with zero links
sudo truncate -s 0 /var/log/big.log

Quotas

Quotas limit disk use per user or group on a filesystem. They suit shared servers such as web hosts or shell servers.

Rough steps for ext4:

Terminal
sudo apt install quota                       # or: dnf install quota
# add usrquota,grpquota to the filesystem options in /etc/fstab, then:
sudo mount -o remount /home
sudo quotacheck -cug /home
sudo quotaon -v /home
sudo edquota -u dana                         # set soft and hard block/inode limits
sudo setquota -u dana 5000000 6000000 0 0 /home    # block soft/hard (1 KiB units), inode soft/hard
quota -u dana
sudo repquota -s /home
  • Soft limit: may be exceeded for a grace period.
  • Hard limit: an absolute stop.
  • XFS has its own project, user and group quotas enabled at mount time (uquota, gquota, pquota) and managed with xfs_quota.

Backups: principles

  • 3-2-1 rule: three copies, on two kinds of media, one off-site.
  • Test restores. A backup you have never restored is a hope, not a backup.
  • Automate with a timer or cron, and alert on failures (timers).
  • Protect backups: encrypt, restrict access, and keep some copies the production server cannot delete (ransomware).
  • Back up what you need to rebuild: /etc, application data, databases (dump them or use consistent snapshots), a package list, and your notes.
flowchart LR
    A["Production data"] --> B["Local backup"]
    A --> C["Second medium"]
    C --> D["Off-site copy"]
    D --> E["Restore test"]

Copies in several places, and a proof that they work.

tar

Terminal
sudo tar -czf /backup/etc-$(date +%F).tar.gz /etc
tar -tzf /backup/etc-2026-03-02.tar.gz | head        # list
sudo tar -xzf /backup/etc-2026-03-02.tar.gz -C /restore etc/ssh/sshd_config

Add --xattrs --acls when permissions metadata matters. Extracting as root restores ownership.

rsync

rsync copies only differences, preserves metadata and works over SSH.

Terminal
sudo rsync -aAXH --delete /srv/ /backup/srv/             # local mirror
rsync -az -e ssh /srv/ backup@vault:/backups/srv/        # remote
rsync -aAXHn --delete /srv/ /backup/srv/                 # -n = dry run

A trailing slash on the source matters: /srv/ copies the contents, /srv copies the directory itself into the destination. --delete makes the destination mirror the source, including deletions, so always dry-run it first.

For versioned history use --link-dest (hard-linked daily snapshots), or tools built for the job such as restic, borg and rsnapshot, which add deduplication, encryption and retention policies.

Databases and snapshots

Copying a database's files while it runs gives a corrupt backup. Use the database's dump tool (mysqldump, pg_dump), or take an LVM or filesystem snapshot after flushing (LVM snapshots) and back up the snapshot.

A stand-in: biggest directories

This model sorts made-up du output the way du | sort -h | tail does, and flags directories above a limit.

Python Stand-in: what is using the space?

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

A stand-in: retention policy

This model chooses which daily backups to keep: the last 7 days and the first backup of each month, deleting the rest.

Python Stand-in: which backups do we keep?

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Common mistakes

  • Backing up to the same disk as the data.
  • Never testing a restore.
  • Running rsync --delete with the source and destination swapped.
  • Copying live database files.
  • Leaving backup credentials writable by the server being backed up.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. What does the 3-2-1 backup rule recommend?

    It spreads risk across copies, media and locations.
  2. 2. You deleted a big log but df shows no freed space. Why?

    Space is freed only when the last open handle closes; use lsof +L1.
  3. 3. What does a trailing slash on an rsync source do?

    src/ copies contents; src copies the directory itself into the destination.
  4. 4. Which quota limit may be exceeded temporarily?

    A soft limit allows a grace period before enforcement.

Summary

  • Find space hogs with df, du -x and ncdu; remember inodes and deleted-but-open files.
  • Quotas cap users and groups with soft and hard limits.
  • Back up with tar, rsync or dedicated tools, follow 3-2-1 and test restores.
  • Dump databases or snapshot them; never copy live files.

Next: Network Interfaces, Addresses and Routes