Script to create differential archives between two directories for lightweight versioning

Bash script to create differential tar.gz archives between two directories for lightweight, incremental versioning of new or modified files.

Brief Introduction

Script to create differential archives between two directories for lightweight versioning (bash)
DESCRIPTION:
This bash script allows you to create a compressed archive containing only files that are modified or new compared to a reference directory, facilitating a lightweight and incremental versioning system.

ADDITIONAL NOTES:
The script is designed for Unix/Linux environments with bash and standard GNU tools installed. It does not handle file deletions (only additions/modifications). For full versioning, dedicated systems like git are recommended.

EXPLANATIONS:
- The 'file_checksum' function calculates the SHA256 hash of a file to compare its content.
- A list of files present in 'new_dir' is generated and compared with those in 'base_dir'.
- If a file is absent in 'base_dir' or differs in checksum, it is added to the list of files to archive.
- At the end, a tar.gz archive is created containing only modified or new files, preserving the directory structure.
- The script includes checks for directory existence and clear error messages.

Example Code: bash

#!/bin/bash

# Script to create a differential archive between two directories
# Lightweight versioning: archives only new or modified files compared to the base

# Function to calculate SHA256 checksum of a file
file_checksum() {
    sha256sum "$1" | awk '{print $1}'
}

# Parameter check
if [ "$#" -ne 3 ]; then
    echo "Usage: $0 <base_directory> <new_directory> <output_archive.tar.gz>"
    exit 1
fi

base_dir="$1"
new_dir="$2"
output_archive="$3"

# Verify that directories exist
if [ ! -d "$base_dir" ]; then
    echo "Error: base directory '$base_dir' not found." >&2
    exit 2
fi

if [ ! -d "$new_dir" ]; then
    echo "Error: new directory '$new_dir' not found." >&2
    exit 3
fi

# Temporary file for list of modified or new files
tmp_file_list=$(mktemp)

# Loop over all files in the new directory
# find with -type f to consider only regular files
find "$new_dir" -type f | while read -r new_file; do
    # Relative path of the file with respect to new_dir
    rel_path="${new_file#$new_dir/}"
    base_file="$base_dir/$rel_path"

    if [ ! -f "$base_file" ]; then
        # New file, add it
        echo "$rel_path" >> "$tmp_file_list"
    else
        # File exists, compare checksum
        checksum_new=$(file_checksum "$new_file")
        checksum_base=$(file_checksum "$base_file")
        if [ "$checksum_new" != "$checksum_base" ]; then
            # Modified file
            echo "$rel_path" >> "$tmp_file_list"
        fi
    fi

done

# If no modified or new files, notify and exit
if [ ! -s "$tmp_file_list" ]; then
    echo "No new or modified files found. Archive not created."
    rm "$tmp_file_list"
    exit 0
fi

# Create tar.gz archive with the listed files
# -C new_dir to maintain relative structure
# --files-from reads the list of files to include

tar -czf "$output_archive" -C "$new_dir" --files-from="$tmp_file_list"

if [ $? -eq 0 ]; then
    echo "Differential archive created successfully: $output_archive"
else
    echo "Error during archive creation." >&2
fi

# Cleanup
rm "$tmp_file_list"

exit 0
×
×
×
Do you want to regenerate the answer?
×
Export chat
Choose export format
Generazione PDF in corso…
Allegati
×
You are about to close the chat and switch to the image generator. If you are not logged in, you will lose our chat. Do you confirm?
You are viewing a shared chat in temporary mode. It will not be saved.
×
Saved prompts
×
Private note
×
Label
×
Search all chats
×
Your insights
Analyzing…
×
AI Preferences
Assistant name 30
Answer length
AI model
Structured answers
Include examples
Use emojis
Guide me, don't solve Hints and questions instead of a ready answer
More settings →
×
Share this chat
Anyone opening this link can view the chat or add it to their profile as their own chat.
Note: the chat attachments will be shared too. Anyone who adds it will receive a copy of the files in their own folder.
Shared chat
Someone shared a chat with you. Do you want to just view it or add it to your chats?
Note: the chat attachments will be shared too. Anyone who adds it will receive a copy of the files in their own folder.
×

Saved messages

Loading...

×

Chat History

example-programs · CHAT HISTORY

Loading...

AI Preferences

×
  • BasicQuick and essential answers for study
  • MediumHigher quality for study and programming
  • AdvancedComplex reasoning and detailed analysis
Explain Steps

What do you need?

Hardware: No hardware components required.

Software:
  • bash: standard scripting shell on Unix/Linux systems.
  • tar: to create compressed archives (tar.gz).
  • diff: to compare directories and files.
  • find: to list files recursively.
  • gzip: for compression (integrated in tar).
Configuration:
  • The user must specify two directories: 'base_dir' (reference version) and 'new_dir' (updated version).
  • The output is a .tar.gz archive containing only new or modified files compared to 'base_dir'.
  • The script handles hidden files and subdirectories.
  • Execution permissions are required on the script.

Similar Topics

Script to batch rename files in a folder
Bash script to batch rename files in a folder by adding a custom prefix and sequential numbers, ideal for organizing files efficiently.
Script to download files with curl and check the result
Bash script to download files using curl, verify success, and handle errors with clear messages for reliable command-line file transfers.
Bash Script to Search Text in Multiple Files and Show Context
Bash script to search text in multiple files with context lines, enabling quick and recursive searches in specified directories using grep.
Script to compare two folders and show file differences
Bash script to compare two folders, highlighting unique and modified files using standard Unix commands like diff, find, sort, and comm.
Script to rename photos based on modification date
Bash script to rename photos by modification date, adding counters to avoid duplicates, for easy chronological photo organization on Linux or macOS.
Script to check if a list of hosts is reachable (ping)
Bash script to check host reachability using ping command. Reads hosts from hosts.txt and reports if each is reachable with a single ping packet.
Bash Script to Merge Multiple Text Files into a Single File with Header
Bash script to merge multiple text files into one with a custom header showing date and number of files merged, handling missing files gracefully.
Script to check for a process and restart it
Bash script to monitor a process and automatically restart it if not running, ensuring continuous operation on Linux/Unix systems with cron support.