How To Write Bash Scripts In Linux - Complete Guide (Part 17 - Backup Script)
Learn Linux TV: Writing a Backup Script in Bash
Introduction to the Backup Script
- The host expresses gratitude for viewers tuning into Learn Linux TV, emphasizing the importance of community support and promoting their latest book on mastering Ubuntu Server.
- The episode is part of the "bad scripting series," focusing on creating a practical backup script using bash.
Overview of the Backup Script
- The backup script will be the most advanced one created in this series, integrating various concepts learned previously.
- The host plans to write out the script first and then explain its functionality step by step.
Key Components of the Script
- The script begins with a shebang and includes comments for clarity, although some may argue that certain comments are unnecessary.
- An if statement checks for exactly two arguments; if not provided, it prompts users with instructions and exits with code 1.
Checking Dependencies
- Another if statement uses
command -vto check ifrsyncis installed. Output is redirected to/dev/nullto suppress unnecessary messages.
- If
rsyncisn't found, it informs users about installation via their package manager and exits with code 2.
Date Formatting for Backups
- A variable named
current_datecaptures formatted output from the date command (YYYY-MM-DD), making sorting easier across different locales.
Rsync Options Explained
- A string variable
rsync_optionscontains options passed torsync:
- A: Archive mode retains metadata like permissions.
- V: Verbose mode shows ongoing operations.
- B: Prevent overwriting files in target directories by creating backups instead.
Handling File Replacements
- The use of
--backup-dirallows specifying where backup files go when originals are replaced, utilizing$2as the target directory argument.
Incremental Backups Management
- Files replaced during sync are moved into a directory named after the current date, facilitating easy retrieval of previous versions later on.
Synchronization Behavior
- Using
--delete, ensures that target directories mirror source directories accurately but requires caution as it can lead to data loss without proper safeguards.
Testing with Dry Run Option
- The option
--dry-runsimulates actions without executing them, crucial for testing scripts before actual execution to prevent accidental data loss.
Finalizing and Running the Script
- After writing and saving changes, there’s an emphasis on checking for errors before running live tests.
Error Handling During Execution
- Initial runs reveal issues due to typos; correcting these demonstrates common pitfalls in scripting practices.
Successful Execution Confirmation
- Upon successful execution without dry run options, confirmation comes through log outputs indicating real backups were made successfully.
Conclusion & Next Steps
- As this episode concludes, viewers are encouraged to provide feedback while being informed about upcoming content related to further learning opportunities in Linux scripting.