How To Write Bash Scripts In Linux - Complete Guide (Part 17 - Backup Script)

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 -v to check if rsync is installed. Output is redirected to /dev/null to suppress unnecessary messages.
  • If rsync isn't found, it informs users about installation via their package manager and exits with code 2.

Date Formatting for Backups

  • A variable named current_date captures formatted output from the date command (YYYY-MM-DD), making sorting easier across different locales.

Rsync Options Explained

  • A string variable rsync_options contains options passed to rsync:
  • 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-dir allows specifying where backup files go when originals are replaced, utilizing $2 as 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-run simulates 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.
Video description

Backups are one of the most important aspects of system administration, yet often overlooked—until it’s too late. In episode 17 of LearnLinuxTV’s Bash Scripting course, you’ll learn how to create a simple and effective backup script using Bash. This tutorial will show you how to automate file backups with real-world examples, helping you protect your data and gain confidence in your scripting skills. Don’t wait for disaster—learn how to back up your files with Bash today! *💬 CHECK OUT OUR FORUMS* Have a Linux question? Want to share a tip? Or maybe just hang out? ➡ Join here ➜ https://community.learnlinux.tv *👕 GRAB SOME LINUX SWAG!* - "Distro Hopper" T-Shirt ➜ https://learnlinux.link/distro-hopper - Backpack with Fedora logo ➜ https://learnlinux.link/fedora-backpack - "apt install coffee" T-Shirt ➜ https://learnlinux.link/apt-install-coffee - Linux Distro Stickers ➜ https://learnlinux.link/distro-stickers - "sudo Highway to Shell" T-Shirt ➜ https://learnlinux.link/sudo-highway - /usr/bin/shirt T-Shirt ➜ https://learnlinux.link/usr-bin-shirt - Debian T-Shirt ➜ https://learnlinux.link/debian-red-shirt - "Read a Book" T-Shirt ➜ https://learnlinux.link/read-a-book ➡ Find more here: ➜ https://merch.learnlinux.tv ━━━━━━━━━━━━━━━━━━━━━━ 🕐 Time Codes 00:00 - Pre-Intro 00:48 - Intro 01:16 - Misc Info and Notes 01:52 - Walkthrough and Explanation of the Sample Backup Bash Script 14:20 - Running the Backup Script (with --dry-run enabled) 17:18 - Running the Backup Script (wihout --dry-run) OTHER BASH SCRIPTING SERIES EPISODES - Class 01 ➜ Introduction: https://linux.video/bash1 - Class 02 ➜ Hello World: https://linux.video/bash2 - Class 03 ➜ Variables: https://linux.video/bash3 - Class 04 ➜ Math Functions: https://linux.video/bash4 - Class 05 ➜ If Statements: https://linux.video/bash5 - Class 06 ➜ Exit Codes: https://linux.video/bash6 - Class 07 ➜ While Loops: https://linux.video/bash7 - Class 08 ➜ Universal Update Script: https://linux.video/bash8 - Class 09 ➜ For Loops: https://linux.video/bash9 - Class 10 ➜ Where To Store Your Scripts: https://linux.video/bash10 - Class 11 ➜ Data Streams: https://linux.video/bash11 - Class 12 ➜ Functions: https://linux.video/bash12 - Class 13 ➜ Case Statements: https://linux.video/bash13 - Class 14 ➜ Scheduling Jobs (Part 1): https://linux.video/bash14 - Class 15 ➜ Scheduling Jobs (Part 2): https://linux.video/bash15 - Class 16 ➜ Arguments: https://linux.video/bash16 - Class 17 ➜ Building a Backup Script: https://linux.video/bash17 - Class 18 ➜ Closing: https://linux.video/bash18━━━━━━━━━━━━━━━━━━━━━━ *🎓 Learn Linux TV* Your source for Linux-related Fun and Learning! https://www.learnlinux.tv https://community.learnlinux.tv https://learnlinux.link/jay ━━━━━━━━━━━━━━━━━━━━━━ *🤝 Affiliate Notice* Some of the links mentioned in this description are affiliate links. If you purchase through them, it helps keep the content flowing. https://www.learnlinux.tv/content-ethics ━━━━━━━━━━━━━━━━━━━━━━ *⚠️ Use Content Responsibly* Learn Linux TV shares technical content intended to teach and help you, but it comes with no warranty. The channel is not liable for any damages from its use. Always ensure you have proper permissions, follow company policies, and comply with all applicable laws while working with technology. #devopstutorial #devopstraining #devopstrainingvideos