Last active
March 1, 2025 06:13
-
-
Save wimhaanstra/834a6daef6f4832dbf63b39f9c5ad0b9 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# This script uses the following tools: | |
# - ccrypt | |
# - rclone | |
#!/bin/bash | |
# Array of directories you want to backup. It will loop through these directories and backup any | |
# directory inside it. Files will be ignored | |
directories_to_backup=("/storage/docker") | |
# Array of directory names you want to exclude, this uses just a simple string comparison | |
exclude_directories=("plex") | |
# Simple YYYYmmdd based foldername | |
time_stamp=$(date +%Y%m%d) | |
# If you want all files to be set to a certain owner, before backing up | |
# Otherwise make this an empty string | |
owner="sorted:docker" | |
# The location of your rclone config | |
rclone_config="/storage/docker/_scripts/rclone.conf" | |
# Folder where a timestamp based folder will be created, this will contain | |
# the encrypted archives as well. | |
backup_dir="/storage/backup/docker/${time_stamp}" | |
# File on my server, containing the encryption key for the created archives. | |
passkey="/storage/backup/docker/.passkey" | |
# Google drive rclone destination | |
gdrive="drive:/backup" | |
create_backup_dir () { | |
mkdir -p "${backup_dir}" | |
} | |
compress () { | |
echo "Compressing $1 => ${backup_dir}/$1.tgz" | |
tar -czf ${backup_dir}/$1.tgz $1 | |
} | |
encrypt () { | |
echo "Encrypting ${backup_dir}/$1.tgz => $1.tgz.cpt" | |
ccrypt -f -k ${passkey} ${backup_dir}/$1.tgz | |
} | |
clone () { | |
echo "Clone $2.tgz.cpt => $1" | |
rclone --config ${rclone_config} copy ${backup_dir}/$2.tgz.cpt $1 | |
} | |
set_owner () { | |
if [[ "$owner" == "" ]]; then | |
echo "Skipping owner" | |
else | |
echo "Setting owner for $1" | |
chown ${owner} $1 -R * | |
fi | |
} | |
backup_directory () { | |
cd $1 | |
for d in */ ; do | |
directory=$(basename $d) | |
if in_array $directory $exclude_directories; then | |
echo "Skipping $directory, excluded" | |
else | |
if [[ "$directory" == *".old" ]]; then | |
echo "Skipping $directory, marked as old" | |
else | |
set_owner $directory | |
compress $directory | |
encrypt $directory | |
clone $gdrive $directory | |
fi | |
fi | |
done | |
} | |
in_array() { | |
local dir="$1" | |
shift 1 | |
local array=("$@") | |
local value | |
for value in "${array[@]}"; do | |
[ "$value" = "$dir" ] && return 0 | |
done | |
return 1 | |
} | |
create_backup_dir | |
for i in "${directories_to_backup[@]}"; do | |
backup_directory "$i" ; | |
done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment