Skip to content

Instantly share code, notes, and snippets.

@sorted-bits
Last active July 25, 2024 06:05
Show Gist options
  • Save sorted-bits/e66f8893f174b581b942dad1a505d1d4 to your computer and use it in GitHub Desktop.
Save sorted-bits/e66f8893f174b581b942dad1a505d1d4 to your computer and use it in GitHub Desktop.
backup.sh
#!/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")
# 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"
# Remote configured in rclone. In this case 'onedrive' is my OneDrive remote name
# and the `/docker` is the folder I want the files to be cloned into.
remote1="onedrive:/docker/${time_stamp}"
# Second remove configured in rclone. In this case it's Google Drive
remote2="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 [[ "$directory" == *".old" ]]; then
echo "Skipping $directory, marked as old"
else
set_owner $directory
compress $directory
encrypt $directory
clone $remote1 $directory
clone $remote2 $directory
fi
done
}
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