0
0
Linux CLIscripting~5 mins

Backup strategies in Linux CLI - Commands & Configuration

Choose your learning style9 modes available
Introduction
Backing up files means making copies of important data so you don't lose it if something goes wrong. Backup strategies help you decide what to copy, when, and where to keep those copies safely.
When you want to save your work files regularly to avoid losing them after a computer crash
When you need to keep a copy of your website files before making changes
When you want to archive old project files to free up space but still keep them safe
When you want to copy your photos to an external drive for safekeeping
When you want to automate daily backups of your documents folder
Commands
This command creates a compressed archive of the /home/username folder and names it with the current date. It helps you save all your home files in one backup file.
Terminal
tar -czf backup-home-$(date +%Y-%m-%d).tar.gz /home/username
Expected OutputExpected
No output (command runs silently)
-c - Create a new archive
-z - Compress the archive using gzip
-f - Specify the filename of the archive
This command copies all files from /home/username to /mnt/backup/username, syncing changes and deleting files in the backup that no longer exist in the source. It keeps your backup folder up to date.
Terminal
rsync -av --delete /home/username/ /mnt/backup/username/
Expected OutputExpected
sending incremental file list ./ file1.txt file2.txt sent 12345 bytes received 678 bytes total size 23456
-a - Archive mode to copy files recursively and preserve attributes
-v - Show detailed output of the copying process
--delete - Delete files in the destination that are not in the source
This command lists your current scheduled tasks. You use it to check if your backup jobs are set to run automatically.
Terminal
crontab -l
Expected OutputExpected
0 2 * * * tar -czf /mnt/backup/backup-home-$(date +\%Y-\%m-\%d).tar.gz /home/username
This command opens the cron editor where you can add or change scheduled backup commands to run automatically at set times.
Terminal
crontab -e
Expected OutputExpected
No output (command runs silently)
Key Concept

If you remember nothing else from backup strategies, remember: always keep a recent copy of your important files in a separate location to protect against data loss.

Common Mistakes
Not using the --delete flag with rsync for backups
Old files deleted from the source remain in the backup, causing confusion and wasted space
Use rsync with the --delete flag to keep the backup folder synchronized exactly with the source
Running tar without compression
Backup files become very large and use unnecessary disk space
Use the -z flag with tar to compress the backup archive and save space
Not scheduling backups with cron
Backups may be forgotten and not done regularly, risking data loss
Use cron to automate backups at regular intervals without manual effort
Summary
Use tar with compression to create backup archives of important folders.
Use rsync with archive and delete flags to keep backup folders synchronized.
Automate backups by scheduling commands with cron to run regularly.