Ajouter backup_and_restore/archive_with_checksum.sh

This commit is contained in:
Philippe Favre
2024-11-10 19:43:28 +01:00
parent c61dca41ea
commit 56142b79c8

View File

@@ -0,0 +1,54 @@
#!/bin/bash
# This script creates a .tar.xz archive of a specified source directory, preserving file permissions,
# and names the archive using the source directory's name followed by the current date in DDMMYYYY format.
# Additionally, it generates a SHA-256 checksum file for the archive in the specified output directory.
# Usage:
# 1. Make the script executable: `chmod +x archive_with_checksum`
# 2. Run the script by providing the path to the source directory as the first argument
# and the path to the output directory as the second argument.
# Example: `./archive_with_checksum /path/to/source_directory /path/to/output_directory`
#
# The script will produce an archive named `source_directory_DDMMYYYY.tar.xz` and a checksum file
# named `source_directory_DDMMYYYY.tar.xz.sha256` in the specified output directory.
# Check if the correct number of arguments is provided
if [ "$#" -ne 2 ]; then
echo "Usage: $0 source_directory output_directory"
exit 1
fi
# Assign parameters
source_directory=$1
output_directory=$2
# Get the base name of the source directory (without the full path)
source_name=$(basename "$source_directory")
# Get the current date in DDMMYYYY format
current_date=$(date +%d%m%Y)
# Create the archive name with the date, based on the source directory's name
archive_name="${source_name}_${current_date}.tar.xz"
archive_path="${output_directory}/${archive_name}"
# Create the archive with file permissions preserved
tar -cJpf "$archive_path" "$source_directory"
# Confirm archive creation and create a checksum if successful
if [ $? -eq 0 ]; then
echo "Archive created: $archive_path"
# Generate SHA-256 checksum file for the archive in the output directory
checksum_file="${archive_path}.sha256"
sha256sum "$archive_path" > "$checksum_file"
if [ $? -eq 0 ]; then
echo "Checksum file created: $checksum_file"
else
echo "Error creating the checksum file."
fi
else
echo "Error creating the archive."
fi