Introduction / Why This Is Needed
Backup is your primary insurance against data loss due to disk failure, system update errors, or malware. Linux has powerful built-in tools that allow you to create reliable copies of important files quickly and cost-effectively. After completing this guide, you will be confident that your documents, configuration files, and projects are safe.
Requirements / Preparation
- Access Permissions: Copying system files or files from other users may require
sudoprivileges. For personal files, the current user's permissions are usually sufficient. - Free Space: Ensure the target media (external drive, another partition, network path) has enough space to store the data. A 20-30% buffer is recommended.
- Backup Media: Prepare an external hard drive, USB flash drive, or a dedicated partition on your internal drive. For long-term storage, using a physically separate medium is best.
- Installed Utilities: Most distributions ship with
tarandrsyncout of the box. For a graphical interface, you may need to install a package (e.g.,deja-dupin Ubuntu/Debian:sudo apt install deja-dup).
Step-by-Step Guide
Step 1: Determine What to Back Up and Where
Make a list of directories and files that are critically important. Typical candidates:
~/Documents,~/Pictures,~/Videos— personal files.~/projectsor~/code— project source code.~/.config— application configuration files (can contain many service files; it's better to be selective, e.g.,~/.config/Code,~/.config/gtk-3.0).- Lists of installed packages (for system recovery):
dpkg --get-selections > ~/package-list.txt(Debian/Ubuntu) orpacman -Qeq > ~/pkglist.txt(Arch).
Identify the mount point of your external media. It is typically found under /media/your_username/ or /mnt/.
Step 2: Create a Backup with tar
tar is the classic utility for creating archives. It combines many files into one and supports compression.
Basic example (without compression):
tar -cvf /media/backup/backup_home_$(date +%Y%m%d).tar ~/Documents ~/Pictures
-c— create an archive.-v— output the list of added files (verbosely).-f— specify the archive file name.$(date +%Y%m%d)— automatically appends the current date (e.g.,20260217) to the file name.
Example with compression (gzip):
tar -czvf /media/backup/backup_home_$(date +%Y%m%d).tar.gz ~/Documents ~/Pictures
-z— compress usinggzip.
Creating a full home directory backup (caution: may include many service files):
sudo tar -czvf /media/backup/full_home_backup_$(date +%Y%m%d).tar.gz --exclude='./Cache' --exclude='./.cache' --exclude='./.npm' /home/your_username/
Using --exclude allows you to skip caches and temporary files, saving space.
💡 Tip: For regular backups,
rsync(next step) is preferable because it only copies changes.taris better suited for one-off full archives.
Step 3: Configure Incremental Backups with rsync
rsync is a powerful directory synchronization tool. It copies only changed or new files, saving time and space with regular use.
Basic synchronization (preserving permissions and structure):
rsync -avh --delete ~/Documents/ /media/backup/Documents/
-a— archive mode (preserves permissions, timestamps, recurses).-v— verbose output.-h— "human-readable" sizes.--delete— deletes files in the target folder that no longer exist in the source (creates an exact copy). Use with caution!
Backup with exclusions (e.g., browser cache):
rsync -avh --delete --exclude='.cache' --exclude='.npm' --exclude='Downloads' /home/your_username/ /media/backup/full_home/
Copying over SSH (to a remote server):
rsync -avh -e ssh ~/Documents/ user@remote-server:/path/to/backup/
Ensure SSH access is configured on the remote server.
Step 4: Use a Graphical Utility (Optional)
For those who prefer a GUI, Deja Dup (called "Backup" in Ubuntu) is an excellent choice.
- Install it:
sudo apt install deja-dup(Ubuntu/Debian) or find it in your distribution's software center. - Launch "Backup" from the menu.
- Check "Automatically remember password" (if you want automated backups).
- In the "Storage" section, choose a location (local folder, external drive, network folder).
- In the "Folders" tab, add the directories you want to back up.
- In the "Exclude" tab, specify folders that should not be copied (e.g.,
~/Downloads,~/.cache). - Configure a schedule in the "Schedule" tab (e.g., daily).
- Click "Back Up Now" for the first run.
Step 5: Set Up Automation (for tar/rsync)
Create a script for regular execution.
- Create the file
~/scripts/backup.sh:#!/bin/bash # Simple backup script using rsync SOURCE_DIR="/home/your_username/Documents" BACKUP_DIR="/media/backup/Documents" LOG_FILE="/var/log/backup.log" echo "=== Backup started: $(date) ===" >> $LOG_FILE rsync -avh --delete $SOURCE_DIR/ $BACKUP_DIR/ >> $LOG_FILE 2>&1 echo "=== Backup finished: $(date) ===" >> $LOG_FILE - Make it executable:
chmod +x ~/scripts/backup.sh. - Add a job to
cron:- Run
crontab -e. - Add a line for a daily backup at 2:00 AM:
0 2 * * * /home/your_username/scripts/backup.sh - Save and exit.
- Run
Verifying the Result
- For
tar: Ensure the archive file was created and has a reasonable size. Check its contents without extracting:
Try extracting a single file to a temporary folder for verification:tar -tzf /media/backup/backup_home_20260217.tar.gz | head -20mkdir /tmp/test_restore tar -xzf /media/backup/backup_home_20260217.tar.gz -C /tmp/test_restore Documents/important.txt cat /tmp/test_restore/Documents/important.txt - For
rsync: Compare the source and target folders. You can runrsyncwith the-nflag (dry-run) to see which files would be copied/deleted on the next run:
Check that the folder structure and key files exist inrsync -avhn --delete ~/Documents/ /media/backup/Documents//media/backup/Documents/. - Logs: If you used automation, check the log file (
/var/log/backup.logor your specified path) for errors.
Potential Issues
- "Permission denied" error: You are trying to copy files your user lacks permissions for. Use
sudofor thetar/rsynccommand, but be careful not to copy system files into the backup that could conflict during restoration. It's best to copy only files from your home directory or those you explicitly have access to. - "No space left on device": The target media is full. Free up space or use a different medium. Check space with
df -h. rsyncdeletes important files (--delete): The--deleteflag is dangerous if the target folder already contains data not present in the source (e.g., old backups). Always test the command with-n(dry-run) before the first real run. For storing multiple backup versions, it's better not to use--deleteor to store each date in a separate folder.- Media doesn't mount automatically: If using an external drive, ensure it's securely connected. For automatic mounting at boot, study
/etc/fstab. You can add mounting checks to your backup script, but this complicates the task. - Backup takes too long: Use
rsyncfor incremental copies. For the first full backup,tarmight be faster as it creates a single stream. For subsequent runs,rsyncis preferable.