Automated Directory Backup Script in Bash: Compress, Timestamp, and Organize Your Files

Introduction

This script is designed to help you automate the backup process of a specified directory by compressing it into a .tar.gz file. Each backup file is timestamped with the current date and time for easy identification, and is moved to a designated backup folder. This method is especially useful for regularly scheduled backups, ensuring your files are safely archived and organized.

Code Explanation

#!/bin/bash

# Specify the directory to backup
SOURCE_DIR="my_directory"  # Replace with the directory you want to back up

# Specify the backup directory
BACKUP_DIR="backup"

# Create the backup directory if it doesn't exist
mkdir -p "$BACKUP_DIR"

# Create a timestamp for the backup file
TIMESTAMP=$(date +"%Y%m%d_%H%M%S")

# Define the backup file name, including directory name and timestamp
BACKUP_FILE="${SOURCE_DIR}_${TIMESTAMP}.tar.gz"

# Compress the directory into a tar file
echo "Compressing $SOURCE_DIR into $BACKUP_FILE..."
tar -czf "$BACKUP_FILE" "$SOURCE_DIR"
echo "Compression complete."

# Move the backup file to the backup directory
echo "Moving $BACKUP_FILE to $BACKUP_DIR..."
mv "$BACKUP_FILE" "$BACKUP_DIR"
echo "Backup moved to $BACKUP_DIR/$BACKUP_FILE."

echo "Backup process completed successfully!"

Code Walkthrough

  1. Directory Variables:

    • SOURCE_DIR is the name of the directory you want to back up (e.g., my_directory).

    • BACKUP_DIR is where the compressed backup files are stored. If this folder doesn’t exist, the script will create it.

  2. Timestamp Creation:

    • TIMESTAMP=$(date +"%Y%m%d_%H%M%S") generates a unique timestamp in YYYYMMDD_HHMMSS format, making it easy to sort and identify backups by date and time.
  3. Backup File Name:

    • BACKUP_FILE includes the source directory name and timestamp to ensure each backup file has a unique name.
  4. Compress Directory:

    • tar -czf "$BACKUP_FILE" "$SOURCE_DIR" compresses the source directory into a .tar.gz file with the defined name.
  5. Move Backup:

    • mv "$BACKUP_FILE" "$BACKUP_DIR" transfers the compressed file to the specified backup directory.

Usage

  1. Save the script as backup_script.sh.

  2. Make it executable:

     chmod +x backup_script.sh
    
  3. Run the script:

     ./backup_script.sh
    

Expected Output

Running the script should generate output similar to the following:

Compressing my_directory into my_directory_20231030_143512.tar.gz...
Compression complete.
Moving my_directory_20231030_143512.tar.gz to backup...
Backup moved to backup/my_directory_20231030_143512.tar.gz.
Backup process completed successfully!

Conclusion

This script simplifies the process of creating timestamped backups, making it easy to manage regular backups and keep your files safe. By automating compression and organizing backups into a specified directory, you can streamline the backup process and ensure that important data is preserved and easy to locate.