0
0
Bash Scriptingscripting~10 mins

Backup automation script in Bash Scripting - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Backup automation script
Start Script
Check Source Folder Exists
Yes
Create Backup Folder if Missing
Copy Files from Source to Backup
Confirm Backup Success
End Script
The script starts by checking the source folder, creates backup folder if needed, copies files, then confirms success before ending.
Execution Sample
Bash Scripting
#!/bin/bash
SRC="/home/user/docs"
DEST="/home/user/backup"
if [ -d "$SRC" ]; then
    mkdir -p "$DEST"
    cp -r "$SRC"/* "$DEST"
    if [ $? -eq 0 ]; then
        echo "Backup success"
    fi
fi
This script checks if the source folder exists. If it does, it creates the backup folder if missing, copies all files recursively from the source to the backup folder, and confirms success by checking the exit status.
Execution Table
StepActionCondition/EvaluationResult/Output
1Check if source folder exists[ -d /home/user/docs ]True, proceed
2Create backup folder if missingmkdir -p /home/user/backupBackup folder ready
3Copy files from source to backupcp -r /home/user/docs/* /home/user/backupFiles copied
4Check copy success$? -eq 0Success message printed
5End scriptN/AScript ends
💡 Script ends after copying files and confirming success
Variable Tracker
VariableStartAfter Step 2After Step 3Final
SRC/home/user/docs/home/user/docs/home/user/docs/home/user/docs
DEST/home/user/backup/home/user/backup/home/user/backup/home/user/backup
Key Moments - 3 Insights
Why do we use mkdir -p before copying files?
mkdir -p ensures the backup folder exists before copying. Without it, copying would fail if the folder is missing (see Step 2 in execution_table).
What happens if the source folder does not exist?
The script checks if the source folder exists at Step 1. If it doesn't, the script should stop or handle the error to avoid copying from a missing folder.
How do we know if the copy was successful?
After copying, the script checks the exit status $? at Step 4. A zero means success, so it prints a success message.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the result of Step 2?
AFiles copied
BBackup folder ready
CSource folder missing
DScript ends
💡 Hint
Check the 'Result/Output' column for Step 2 in the execution_table
At which step does the script confirm the copy was successful?
AStep 4
BStep 3
CStep 1
DStep 5
💡 Hint
Look for the step checking exit status $? in the execution_table
If the source folder did not exist, what would happen at Step 1?
ACondition is True, proceed
BBackup folder is created
CCondition is False, script should stop
DFiles are copied anyway
💡 Hint
Refer to the Condition/Evaluation column for Step 1 in the execution_table
Concept Snapshot
Backup automation script in bash:
- Define source (SRC) and destination (DEST) folders
- Use mkdir -p to create backup folder if missing
- Copy files with cp -r from SRC to DEST
- Check copy success with exit status ($?)
- Script ends after confirming backup
Full Transcript
This backup automation script starts by checking if the source folder exists. If it does, it creates the backup folder if missing using mkdir -p. Then it copies all files from the source to the backup folder using cp -r. After copying, it checks if the copy was successful by examining the exit status. Finally, the script ends. Variables SRC and DEST hold the folder paths and remain constant. Key points include ensuring the backup folder exists before copying and checking the copy success to avoid silent failures.