Make an Incremental Backup Script with Bash!

Herbertech
17 May 202116:25

Summary

TLDRIn this video, Herbert demonstrates how to create a simple Bash backup script that copies files from a source directory to a destination. The script checks if the files already exist in the destination and whether they are newer in the source. If so, it overwrites them; if not, it skips them. Herbert walks through the script step by step, explaining how to set up directories, declare variables, and use commands like `find` and `cp`. He also suggests automating the process with `cron` for scheduled backups. A practical guide for anyone looking to refresh their Bash scripting skills.

Takeaways

  • 😀 The video introduces a simple bash script for creating incremental backups by copying files from a source to a destination directory.
  • 😀 The presenter emphasizes the importance of using an editor like Visual Studio Code or Vim for writing bash scripts, depending on user preference.
  • 😀 A key concept covered is using a shebang (`#!/bin/bash`) to specify the interpreter for bash scripts.
  • 😀 The script ensures that files are only copied if they don't already exist in the destination or if the source file is newer than the destination file.
  • 😀 The script leverages a `for` loop to iterate through files in the source directory and check for their existence and modification times in the destination directory.
  • 😀 If a file exists and is not newer, it is skipped; if it exists and is newer, it is copied to the destination.
  • 😀 New files that don't exist in the destination directory are automatically copied over to the destination directory.
  • 😀 The script uses `cp -r` to copy files and directories recursively, ensuring nested files are also copied.
  • 😀 The presenter demonstrates the script in action by creating test files and directories, showing how the script handles new, existing, and updated files.
  • 😀 A useful tip is provided for scheduling the script using `cron` for automatic backups, with a link to a related tutorial for setting up cron jobs.
  • 😀 The video concludes with a call to action, encouraging viewers to comment, like, and subscribe for more bash scripting content.

Q & A

  • What is the purpose of the script Herbert is creating?

    -The script is designed to create a backup of a source directory and copy it over to a destination directory, checking for existing files and ensuring incremental backups are made. It also checks if the source files are newer than the destination files before copying.

  • What programming language is Herbert using for the script?

    -Herbert is using Bash scripting to create the backup script.

  • Why does Herbert prefer Visual Studio Code over Vim?

    -Herbert prefers Visual Studio Code because it's easier to use, especially on a machine with a graphical user interface. While he does use Vim at work, he finds Visual Studio Code more convenient for this task.

  • What does the shebang `#!/bin/bash` signify in the script?

    -The shebang `#!/bin/bash` tells the system to interpret the script using the Bash shell, ensuring the script runs in the correct environment.

  • How does the script check if a file already exists in the destination directory?

    -The script uses an `if` statement combined with `-e` (which checks if a file exists) to determine if a file from the source directory already exists in the destination directory.

  • What happens if a file in the source directory is newer than the one in the destination?

    -If a file in the source directory is newer than the corresponding file in the destination directory, the script copies the newer file over to the destination, as indicated by the message 'newer file detected.'

  • What does the script do if a file in the destination directory does not exist?

    -If a file does not exist in the destination directory, the script copies the file from the source directory to the destination, as indicated by the message 'file is being copied over to destination.'

  • How does the script handle directories and files within them?

    -The script uses the `find` command to list files recursively in the source directory, including subdirectories, and ensures that both files and directories are copied over to the destination directory, maintaining the structure.

  • Why does Herbert mention the need to make the script executable?

    -Herbert emphasizes the need to make the script executable using the `chmod +x` command. If this step is skipped, the script won't be able to run.

  • What additional feature does Herbert suggest for automating the backup process?

    -Herbert suggests using the `cron` tab to schedule the backup script to run automatically at regular intervals. He links to another video for more details on how to set up scheduled tasks using cron.

Outlines

plate

This section is available to paid users only. Please upgrade to access this part.

Upgrade Now

Mindmap

plate

This section is available to paid users only. Please upgrade to access this part.

Upgrade Now

Keywords

plate

This section is available to paid users only. Please upgrade to access this part.

Upgrade Now

Highlights

plate

This section is available to paid users only. Please upgrade to access this part.

Upgrade Now

Transcripts

plate

This section is available to paid users only. Please upgrade to access this part.

Upgrade Now
Rate This

5.0 / 5 (0 votes)

Related Tags
Bash ScriptingIncremental BackupLinux TutorialFile ManagementBackup AutomationLinux BasicsVisual Studio CodeCron JobsTech TutorialLinux UsersBackup Script