Skip to content

Instantly share code, notes, and snippets.

@PeterGabaldon
Last active May 27, 2021 16:39
Show Gist options
  • Save PeterGabaldon/843944642f7cc568963a69219c91db81 to your computer and use it in GitHub Desktop.
Save PeterGabaldon/843944642f7cc568963a69219c91db81 to your computer and use it in GitHub Desktop.
Backup to a partition identifying it by its UUID. Generate an exclude from backup file interactively.
#! /bin/bash
# Pedro Gabaldon Julia 2021
# https://petergabaldon.github.io/
### COLORS
NOCOLOR='\033[0m'
RED='\033[0;31m'
GREEN='\033[0;32m'
ORANGE='\033[0;33m'
BLUE='\033[0;34m'
PURPLE='\033[0;35m'
CYAN='\033[0;36m'
LIGHTGRAY='\033[0;37m'
DARKGRAY='\033[1;30m'
LIGHTRED='\033[1;31m'
LIGHTGREEN='\033[1;32m'
YELLOW='\033[1;33m'
LIGHTBLUE='\033[1;34m'
LIGHTPURPLE='\033[1;35m'
LIGHTCYAN='\033[1;36m'
WHITE='\033[1;37m'
###
check_package_installed() {
if ! /usr/bin/dpkg -s "${1}" > /dev/null 2>&1;
then
echo "${1} is not installed."
read -p "I can install it if you want :) (y/n): " opt
opt="${opt:-n}"
echo "Ok..."
if [ "${opt}" = "Y" -o "${opt}" = "y" ];
then
if ! DEBIAN_FRONTEND=noninteractive /usr/bin/apt-get update > /dev/null;
then
echo "Error installing ${1}"
exit 1
fi
if ! DEBIAN_FRONTEND=noninteractive /usr/bin/apt-get -y install "${1}" > /dev/null;
then
echo "Error installing ${1}"
exit 1
fi
echo "${1} was installed succesfully, continuing... :)"
fi
fi
}
function get_excluded_dirs() { # (string path)
list_of_dirs=$(find "${1}" -maxdepth 1 -type d -printf "%P\n")
local -a included_dirs
local -a excluded_dirs
if [ -n "${list_of_dirs}" ];
then
echo -e "\nAsking for files in ${1} (Enter s to skip asking at current directory)"
for dir in ${list_of_dirs}
do
read -p "Do you want to exclude ${dir}? (y/n): " opt
opt="${opt:-n}"
if [ "${opt}" = "S" -o "${opt}" = "s" ];
then
return 0
fi
if [ "${opt}" = "Y" -o "${opt}" = "y" ];
then
excluded_dirs+=(${dir})
else
included_dirs+=(${dir})
fi
done
for excluded_dir in "${excluded_dirs[@]}"
do
echo "${1}/${excluded_dir}" >> "${path}/.excluded_backups"
done
for included_dir in "${included_dirs[@]}"
do
get_excluded_dirs "${1}/${included_dir}"
done
fi
}
### Install figlet if it is not installed. Strange, but maybe figlet is not installed. Same with gpg
check_package_installed "figlet"
check_package_installed "gpg"
BACKUP_PARTITION_UUID="YOUR_BACKUP_PARTITION_UUID"
### Prepare terminal with tput
tput smcup
tput clear
###
echo -e "${LIGHTRED}"
figlet "Backup" 2> /dev/null
echo -e "${NOCOLOR}"
if [ $EUID -ne 0 ];
then
echo "Must be run as root :("
exit 1
fi
if ! find "/dev/disk/by-uuid/${BACKUP_PARTITION_UUID}" > /dev/null;
then
echo "Backup partition NOT FOUND!!! :("
exit 1
fi
if ! [ -e "/media/backups" ];
then
mkdir -p "/media/backups"
fi
read -e -p "Enter path to backup: " path
if ! [ -e "${path}" ];
then
echo "Path do not exist! :("
exit 1
fi
if ! [ -d "${path}" ];
then
echo "Path is not a directory! :("
exit 1
fi
path=$(cd "${path}";pwd)
if ! [ -e "${path}/.excluded_backups" ];
then
read -p "Do you want to create a exclude from backup file asking for directories interactively? (y/n): " opt
opt="${opt:-n}"
if [ "${opt}" = "Y" -o "${opt}" = "y" ];
then
touch "${path}/.excluded_backups"
chmod 664 "${path}/.excluded_backups"
echo -ne "${YELLOW}"
get_excluded_dirs "${path}"
echo -ne "${NOCOLOR}"
read -e -p "If you want to exclude some file enter its path: " exclude_file
while [ "${exclude_file}" ];
do
echo "${exclude_file}" >> "${path}/.excluded_backups"
read -e -p "If you want to exclude some other file enter its path: " exclude_file
done
fi
else
read -p "File with excluding from backup info already exists, do you want to create a New one asking for directories interactively or Preserve it? (n/p): " opt
opt="${opt:-p}"
if [ "${opt}" = "N" -o "${opt}" = "n" ];
then
echo -n > "${path}/.excluded_backups"
echo -ne "${YELLOW}"
get_excluded_dirs "${path}"
echo -ne "${NOCOLOR}"
read -e -p "If you want to exclude some file enter its path: " exclude_file
while [ "${exclude_file}" ];
do
echo "${exclude_file}" >> "${path}/.excluded_backups"
read -e -p "If you want to exclude some other file enter its path: " exclude_file
done
fi
fi
echo
if ! mount "/dev/disk/by-uuid/${BACKUP_PARTITION_UUID}" "/media/backups";
then
echo -e "${RED}Error mounting partition :(${NOCOLOR}"
exit 1
fi
backup_path=$(echo "$path" | tr "/" "_")
backup_path="/media/backups/$(date +'%d-%m-%Y')-${backup_path}-backup.tar.gz"
echo -ne "${CYAN}"
if [ -e "${path}/.excluded_backups" ];
then
tar -cvpzf "${backup_path}" -X "${path}/.excluded_backups" "${path}"
else
tar -cvpzf "${backup_path}" "${path}"
fi
echo -e "\n${GREEN}Now the backup will be encrypted. Please remember to use a strong password, Keepass is your friend. (If you do not have gpg installed it will not be encrypted)"
read -rsn1 -p "Press any key"
if gpg --output "${backup_path}.gpg" --symmetric "${backup_path}";
then
rm -f "${backup_path}"
fi
echo -ne "${NOCOLOR}"
umount "/media/backups"
echo -ne "${LIGHTGREEN}"
tput blink; figlet "Backup completed" 2> /dev/null; tput sgr0; read -rsn1 -p "Press any key"
tput rmcup # Restore screen
exit 0
@PeterGabaldon
Copy link
Author

I should write more comments...

@PeterGabaldon
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment