Bird
Raised Fist0
Linux CLIscripting~10 mins

Backup strategies in Linux CLI - Step-by-Step Execution

Choose your learning style10 modes available
Concept Flow - Backup strategies
Identify data to backup
↓
Choose backup type
↓
Full backup
↓
Store backup
↓
Verify backup
↓
Schedule backups
↓
Restore test
This flow shows how to select data, choose backup type, store backups, verify them, schedule regular backups, and test restores.
Execution Sample
Linux CLI
rsync -av /home/user/data/ backup_full_2024/
rsync -av --link-dest=backup_full_2024/ /home/user/data/ backup_incremental_2024/
ls backup_full_2024 backup_incremental_2024/
This script creates a full backup, then an incremental backup using rsync, and lists the backup files.
Execution Table
StepCommandActionResult/Output
1rsync -av /home/user/data/ backup_full_2024/Create full backupbackup_full_2024/ created
2rsync -av --link-dest=backup_full_2024/ /home/user/data/ backup_incremental_2024/Create incremental backup referencing full backupFiles copied or linked, incremental backup created
3ls backup_full_2024 backup_incremental_2024/List backup filesbackup_full_2024 file1 file2 ...
4Verify backup integrityCheck backup files exist and are readableAll files verified
5Schedule backups with cronSet automatic backup timesCron job added
6Test restore from backupRestore files to test locationFiles restored successfully
7ExitBackup process completeNo errors
💡 Backup and verification steps complete successfully
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3Final
backup_full_2024/does not existcreatedexistsexistsexists
backup_incremental_2024/does not existdoes not existcreatedexistsexists
cron jobnot setnot setnot setsetset
restore testnot donenot donenot donenot donedone
Key Moments - 3 Insights
Why do we create a full backup before incremental backups?
The full backup contains all data needed. Incremental backups only save changes since the last full backup, so the full backup is the base. See execution_table steps 1 and 2.
What does the --link-dest option in rsync do?
It tells rsync to hard link unchanged files from the full backup to save space, making incremental backups efficient. This is shown in step 2.
Why is testing the restore important after backups?
Backups are useless if you cannot restore data. Testing ensures backups are valid and restorable, as shown in step 6.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the result after step 2?
AFull backup compressed file created
BIncremental backup created with files copied or linked
CBackup files listed
DCron job scheduled
💡 Hint
Check the 'Result/Output' column for step 2 in execution_table
At which step is the backup integrity verified?
AStep 3
BStep 5
CStep 4
DStep 6
💡 Hint
Look for 'Verify backup integrity' in the 'Command' column
If the full backup file is missing, what happens to the incremental backup?
AIncremental backup cannot be restored properly
BIncremental backup becomes a full backup
CIncremental backup still works independently
DBackup schedule stops
💡 Hint
Refer to key_moments about the importance of full backup as base
Concept Snapshot
Backup strategies:
- Start with a full backup (all data)
- Use incremental backups to save changes
- Store backups safely
- Verify backups regularly
- Schedule automatic backups
- Test restore to ensure data safety
Full Transcript
Backup strategies involve first identifying the data to save. Then, create a full backup that contains all files. After that, incremental backups save only changes since the last full backup, saving space and time. Tools like tar and rsync help create these backups. The --link-dest option in rsync links unchanged files to avoid duplication. After backups, verify files exist and are readable. Schedule backups automatically using cron jobs. Finally, test restoring files to confirm backups work. This process ensures data is safe and recoverable.

Practice

(1/5)
1. Which of the following best describes the main purpose of a backup in Linux?
easy
A. To delete old files to free up space
B. To install new software updates automatically
C. To speed up the computer by cleaning temporary files
D. To save copies of important files in a safe place

Solution

  1. Step 1: Understand what a backup is

    A backup is a copy of important data saved separately to protect against loss.
  2. Step 2: Match the description to the options

    Only To save copies of important files in a safe place describes saving copies in a safe place, which is the purpose of backups.
  3. Final Answer:

    To save copies of important files in a safe place -> Option D
  4. Quick Check:

    Backup = Save copies safely [OK]
Hint: Backups always mean saving copies, not deleting files [OK]
Common Mistakes:
  • Confusing backup with deleting files
  • Thinking backup speeds up the system
  • Mixing backup with software updates
2. Which command syntax correctly copies a directory and its contents to a backup folder?
easy
A. cp -r /home/user/docs /backup/docs
B. cp /home/user/docs /backup/docs
C. cp -d /home/user/docs /backup/docs
D. cp -x /home/user/docs /backup/docs

Solution

  1. Step 1: Identify the correct option to copy directories recursively

    The -r option in cp copies directories and their contents recursively.
  2. Step 2: Check other options for correctness

    The plain cp lacks -r, so it won't copy directories properly. The options with -d and -x use invalid or unrelated flags.
  3. Final Answer:

    cp -r /home/user/docs /backup/docs -> Option A
  4. Quick Check:

    Copy directory = cp -r [OK]
Hint: Use cp -r to copy folders with all files inside [OK]
Common Mistakes:
  • Forgetting the -r flag for directories
  • Using wrong flags like -d or -x
  • Assuming cp copies folders without options
3. What will be the output of this command if the source directory has files and the destination is empty?
rsync -av /home/user/data/ /backup/data/
medium
A. Only lists files without copying them
B. Copies all files and folders from /home/user/data to /backup/data preserving attributes
C. Deletes all files in /backup/data and leaves it empty
D. Shows an error because /backup/data does not exist

Solution

  1. Step 1: Understand rsync options -a and -v

    The -a option means archive mode (copy recursively and preserve attributes), and -v means verbose (show progress).
  2. Step 2: Analyze command behavior

    Since /backup/data exists and is empty, rsync copies all files and folders from source to destination preserving attributes.
  3. Final Answer:

    Copies all files and folders from /home/user/data to /backup/data preserving attributes -> Option B
  4. Quick Check:

    rsync -av copies files with details [OK]
Hint: rsync -av copies and shows details, no deletion by default [OK]
Common Mistakes:
  • Thinking rsync deletes files by default
  • Confusing verbose with dry-run
  • Assuming error if destination exists
4. You wrote this script to backup a folder:
#!/bin/bash
cp -r /home/user/docs /backup/docs
rm -r /home/user/docs/*

What is the main problem with this backup strategy?
medium
A. It deletes original files after copying, risking data loss
B. It does not copy hidden files
C. It uses the wrong command to copy files
D. It will fail because /backup/docs does not exist

Solution

  1. Step 1: Analyze the script commands

    The script copies /home/user/docs to /backup/docs, then deletes all files inside /home/user/docs.
  2. Step 2: Identify risk in deleting original files

    Deleting original files after copying is risky if the copy failed or is incomplete, causing data loss.
  3. Final Answer:

    It deletes original files after copying, risking data loss -> Option A
  4. Quick Check:

    Deleting originals after backup = risk [OK]
Hint: Never delete originals immediately after backup without verification [OK]
Common Mistakes:
  • Assuming cp misses hidden files (it copies them)
  • Thinking /backup/docs must exist (cp creates it)
  • Ignoring risk of deleting original data
5. You want to automate daily backups of /home/user/projects to /mnt/backup using tar. Which command correctly creates a compressed backup file with the current date in its name?
hard
A. tar -czf /home/user/projects /mnt/backup/projects-$(date +%F).tar.gz
B. tar -cf /mnt/backup/projects.tar.gz /home/user/projects
C. tar -czf /mnt/backup/projects-$(date +%F).tar.gz /home/user/projects
D. tar -xzf /mnt/backup/projects-$(date +%F).tar.gz /home/user/projects

Solution

  1. Step 1: Understand tar options for creating compressed archives

    -c creates an archive, -z compresses with gzip, and -f specifies the output file.
  2. Step 2: Check command correctness

    tar -czf /mnt/backup/projects-$(date +%F).tar.gz /home/user/projects correctly creates a gzip compressed archive named with the current date in /mnt/backup from /home/user/projects.
  3. Final Answer:

    tar -czf /mnt/backup/projects-$(date +%F).tar.gz /home/user/projects -> Option C
  4. Quick Check:

    tar -czf + date = compressed dated backup [OK]
Hint: Use tar -czf with $(date +%F) for dated compressed backups [OK]
Common Mistakes:
  • Using -xzf which extracts instead of creates
  • Swapping source and destination paths
  • Omitting compression flag -z
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard