Skip to content

Instantly share code, notes, and snippets.

@alexwenzel
Last active February 12, 2024 19:32
Show Gist options
  • Save alexwenzel/c36dc7a5e494f65029bcea0bec18feab to your computer and use it in GitHub Desktop.
Save alexwenzel/c36dc7a5e494f65029bcea0bec18feab to your computer and use it in GitHub Desktop.
create a directory backup and omit specific files and folders
#!/bin/bash
# add this script to crontab
# crontab -e
# 0 0 * * * /path/to/compress.sh /path/to/directory /path/to/output
# List of files and directories to omit
OMIT_LIST=(
"vendor/"
"node_modules/"
)
compress_directory_recursive() {
local dir=$1
local output_file=$2
local timestamp=$(date "+%Y-%m-%d_%H-%M-%S")
output_file="${output_file}_${timestamp}.tar.xz"
local temp_dir=$(mktemp -d -t tmp.XXXXXXXXXX)
trap 'rm -rf "$temp_dir"' EXIT
rsync -a --exclude-from=<(printf "%s\n" "${OMIT_LIST[@]}") "$dir" "$temp_dir"
tar -cJf "$output_file" -C "$temp_dir" .
}
# Usage: ./compress.sh <directory> <output_file>
compress_directory_recursive "$1" "$2"
echo "Compression job completed successfully at $(date)" >> compress.log
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment