reconfigured to use bashup.sh for common code

This commit is contained in:
Matthew Saunders Brown 2021-04-22 15:03:50 -07:00
parent 4983ff00ba
commit 2878300e82
9 changed files with 164 additions and 477 deletions

View File

@ -25,14 +25,14 @@ cd /usr/local/src/
wget https://git.stack-source.com/msb/bashup/archive/master.tar.gz -O bashup.tar.gz wget https://git.stack-source.com/msb/bashup/archive/master.tar.gz -O bashup.tar.gz
tar zxvf bashup.tar.gz tar zxvf bashup.tar.gz
cd bashup cd bashup
cp bashup-*.sh /usr/local/sbin/ cp bashup*.sh /usr/local/sbin/
chmod 755 /usr/local/sbin/bashup-*.sh chmod 755 /usr/local/sbin/bashup*.sh
chown root:root /usr/local/sbin/bashup-*.sh chown root:root /usr/local/sbin/bashup*.sh
nano /usr/local/etc/bashup.cnf nano /usr/local/sbing/bashup.cnf
crontab -e crontab -e
``` ```
The "nano /usr/local/etc/bashup.cnf" command is optional. Do this if you need to override any of the settings found at the top of the various bashup scripts. The "nano /usr/local/sbin/bashup.cnf" command is optional. Do this if you need to override any of the configurable settings found at the top of the script.
For the crontab add an entry for each of the "backup" scripts that you'd like to run. For example, to back up files at 3:01 am every day add this crontab: For the crontab add an entry for each of the "backup" scripts that you'd like to run. For example, to back up files at 3:01 am every day add this crontab:

View File

@ -4,99 +4,11 @@
# https://git.stack-source.com/msb/bashup # https://git.stack-source.com/msb/bashup
# MIT License Copyright (c) 2021 Matthew Saunders Brown # MIT License Copyright (c) 2021 Matthew Saunders Brown
# retention vars # load include file
retention_years=0; source $(dirname $0)/bashup.sh
retention_months=3;
retention_weeks=5;
retention_days=7;
# backup storage directory
backup_storage_dir='/mnt/backups';
# directories to be backed up
backup_dirs=('/etc' '/home' '/srv' '/root' '/usr/local' '/var/www');
# check for local config, which can be used to override any of the above bashup::set_existing_backups
if [[ -f /usr/local/etc/bashup.cnf ]]; then bashup::set_retention_array
source /usr/local/etc/bashup.cnf
fi
# require root
if [ "${EUID}" -ne 0 ]; then
echo "This script must be run as root"
exit 1
fi
if [ ! -d $backup_storage_dir ]; then
echo "ERROR: Backup storage dir ($backup_storage_dir) does not exist."
exit 1
fi
# check if backup_storage_dir is a mount in fstab
grep -qs " $backup_storage_dir " /etc/fstab
if [ $? -eq 0 ]; then
# check if backup_storage_dir is already mounted
grep -qs " $backup_storage_dir " /proc/mounts
if [ $? -ne 0 ]; then
# attempt to mount backups
mount $backup_storage_dir
# re-check for backups mount
grep -qs " $backup_storage_dir " /proc/mounts
if [ $? -ne 0 ]; then
echo "ERROR: failed to mount $backup_storage_dir"
exit 1
fi
fi
fi
# set more vars based on above settings
today=$(date +%Y%m%d)
existing_backups=($(ls $backup_storage_dir|grep -v lost+found))
# remove today from existing backups, if it exists. we do other checks later to avoid re-doing backups
if [[ " ${existing_backups[@]} " =~ " ${today} " ]]; then
unset 'existing_backups[-1]';
fi
# create retention array
declare -a retention_array
# set retention days
if [ $retention_days -gt 0 ]; then
i="0"
while [ $i -lt $retention_days ]; do
DATE=`date --date="$i day ago" +%Y%m%d`
retention_array[$DATE]="$DATE"
i=$[$i+1]
done
fi
# set retention weeks
if [ $retention_weeks -gt 0 ]; then
i="0"
while [ $i -lt $retention_weeks ]; do
i=$[$i+1]
DATE=`date --date="sunday - $i week" +%Y%m%d`
retention_array[$DATE]="$DATE"
done
fi
# set retention months
if [ $retention_months -gt 0 ]; then
i="0"
while [ $i -lt $retention_months ]; do
DATE=`date --date="$i month ago" +%Y%m01`
retention_array[$DATE]="$DATE"
i=$[$i+1]
done
fi
# set retention years
if [ $retention_years -gt 0 ]; then
i="0"
while [ $i -lt $retention_years ]; do
DATE=`date --date="$i year ago" +%Y0101`
retention_array[$DATE]="$DATE"
i=$[$i+1]
done
fi
# create backup for today # create backup for today
if [[ " ${retention_array[@]} " =~ " ${today} " ]]; then if [[ " ${retention_array[@]} " =~ " ${today} " ]]; then
@ -180,11 +92,7 @@ for existing_backup in "${existing_backups[@]}"; do
done done
# check if backup_storage_dir is mounted and unmount if so bashup::unmount_storage_dir
grep -qs " $backup_storage_dir " /proc/mounts
if [ $? -eq 0 ]; then
umount $backup_storage_dir
fi
exit 0 exit 0

View File

@ -4,103 +4,11 @@
# https://git.stack-source.com/msb/bashup # https://git.stack-source.com/msb/bashup
# MIT License Copyright (c) 2021 Matthew Saunders Brown # MIT License Copyright (c) 2021 Matthew Saunders Brown
# retention vars # load include file
retention_years=0; source $(dirname $0)/bashup.sh
retention_months=3;
retention_weeks=5;
retention_days=7;
# backup storage directory
backup_storage_dir='/mnt/backups';
# mysql config file that contains 'host' 'user' 'password' vars
defaults_extra_file='/etc/mysql/debian.cnf';
# list of databases to skip
exclusions=('information_schema' 'performance_schema' 'sys' 'wsrep');
# check for local config, which can be used to override any of the above bashup::set_existing_backups
if [[ -f /usr/local/etc/bashup.cnf ]]; then bashup::set_retention_array
source /usr/local/etc/bashup.cnf
fi
# require root
if [ "${EUID}" -ne 0 ]; then
echo "This script must be run as root"
exit 1
fi
if [ ! -d $backup_storage_dir ]; then
echo "ERROR: Backup storage dir ($backup_storage_dir) does not exist."
exit 1
fi
# check if backup_storage_dir is a mount in fstab
grep -qs " $backup_storage_dir " /etc/fstab
if [ $? -eq 0 ]; then
# check if backup_storage_dir is already mounted
grep -qs " $backup_storage_dir " /proc/mounts
if [ $? -ne 0 ]; then
# attempt to mount backups
mount $backup_storage_dir
# re-check for backups mount
grep -qs " $backup_storage_dir " /proc/mounts
if [ $? -ne 0 ]; then
echo "ERROR: failed to mount $backup_storage_dir"
exit 1
fi
fi
fi
# get todays date (backup dir name)
today=$(date +%Y%m%d)
# get list of existing backups
existing_backups=($(ls $backup_storage_dir|grep -v lost+found))
# remove today from existing backups, if it exists. we do other checks later to avoid re-doing backups
if [[ " ${existing_backups[@]} " =~ " ${today} " ]]; then
unset 'existing_backups[-1]';
fi
# create retention array
declare -a retention_array
# set retention days
if [ $retention_days -gt 0 ]; then
i="0"
while [ $i -lt $retention_days ]; do
DATE=`date --date="$i day ago" +%Y%m%d`
retention_array[$DATE]="$DATE"
i=$[$i+1]
done
fi
# set retention weeks
if [ $retention_weeks -gt 0 ]; then
i="0"
while [ $i -lt $retention_weeks ]; do
i=$[$i+1]
DATE=`date --date="sunday - $i week" +%Y%m%d`
retention_array[$DATE]="$DATE"
done
fi
# set retention months
if [ $retention_months -gt 0 ]; then
i="0"
while [ $i -lt $retention_months ]; do
DATE=`date --date="$i month ago" +%Y%m01`
retention_array[$DATE]="$DATE"
i=$[$i+1]
done
fi
# set retention years
if [ $retention_years -gt 0 ]; then
i="0"
while [ $i -lt $retention_years ]; do
DATE=`date --date="$i year ago" +%Y0101`
retention_array[$DATE]="$DATE"
i=$[$i+1]
done
fi
# create backup for today # create backup for today
if [[ " ${retention_array[@]} " =~ " ${today} " ]]; then if [[ " ${retention_array[@]} " =~ " ${today} " ]]; then
@ -162,10 +70,6 @@ for existing_backup in "${existing_backups[@]}"; do
done done
# check if backup_storage_dir is mounted and unmount if so bashup::unmount_storage_dir
grep -qs " $backup_storage_dir " /proc/mounts
if [ $? -eq 0 ]; then
umount $backup_storage_dir
fi
exit 0; exit 0

View File

@ -4,99 +4,11 @@
# https://git.stack-source.com/msb/bashup # https://git.stack-source.com/msb/bashup
# MIT License Copyright (c) 2021 Matthew Saunders Brown # MIT License Copyright (c) 2021 Matthew Saunders Brown
# retention vars # load include file
retention_years=0; source $(dirname $0)/bashup.sh
retention_months=3;
retention_weeks=5;
retention_days=7;
# backup storage directory
backup_storage_dir='/mnt/backups';
# check for local config, which can be used to override any of the above bashup::set_existing_backups
if [[ -f /usr/local/etc/bashup.cnf ]]; then bashup::set_retention_array
source /usr/local/etc/bashup.cnf
fi
# require root
if [ "${EUID}" -ne 0 ]; then
echo "This script must be run as root"
exit 1
fi
if [ ! -d $backup_storage_dir ]; then
echo "ERROR: Backup storage dir ($backup_storage_dir) does not exist."
exit 1
fi
# check if backup_storage_dir is a mount in fstab
grep -qs " $backup_storage_dir " /etc/fstab
if [ $? -eq 0 ]; then
# check if backup_storage_dir is already mounted
grep -qs " $backup_storage_dir " /proc/mounts
if [ $? -ne 0 ]; then
# attempt to mount backups
mount $backup_storage_dir
# re-check for backups mount
grep -qs " $backup_storage_dir " /proc/mounts
if [ $? -ne 0 ]; then
echo "ERROR: failed to mount $backup_storage_dir"
exit 1
fi
fi
fi
# get todays date (backup dir name)
today=$(date +%Y%m%d)
# get list of existing backups
existing_backups=($(ls $backup_storage_dir|grep -v lost+found))
# remove today from existing backups, if it exists. we do other checks later to avoid re-doing backups
if [[ " ${existing_backups[@]} " =~ " ${today} " ]]; then
unset 'existing_backups[-1]';
fi
# create retention array
declare -a retention_array
# set retention days
if [ $retention_days -gt 0 ]; then
i="0"
while [ $i -lt $retention_days ]; do
DATE=`date --date="$i day ago" +%Y%m%d`
retention_array[$DATE]="$DATE"
i=$[$i+1]
done
fi
# set retention weeks
if [ $retention_weeks -gt 0 ]; then
i="0"
while [ $i -lt $retention_weeks ]; do
i=$[$i+1]
DATE=`date --date="sunday - $i week" +%Y%m%d`
retention_array[$DATE]="$DATE"
done
fi
# set retention months
if [ $retention_months -gt 0 ]; then
i="0"
while [ $i -lt $retention_months ]; do
DATE=`date --date="$i month ago" +%Y%m01`
retention_array[$DATE]="$DATE"
i=$[$i+1]
done
fi
# set retention years
if [ $retention_years -gt 0 ]; then
i="0"
while [ $i -lt $retention_years ]; do
DATE=`date --date="$i year ago" +%Y0101`
retention_array[$DATE]="$DATE"
i=$[$i+1]
done
fi
# create backup for today # create backup for today
if [[ " ${retention_array[@]} " =~ " ${today} " ]]; then if [[ " ${retention_array[@]} " =~ " ${today} " ]]; then
@ -151,10 +63,6 @@ for existing_backup in "${existing_backups[@]}"; do
done done
# check if backup_storage_dir is mounted and unmount if so bashup::unmount_storage_dir
grep -qs " $backup_storage_dir " /proc/mounts
if [ $? -eq 0 ]; then
umount $backup_storage_dir
fi
exit 0; exit 0

View File

@ -4,21 +4,8 @@
# https://git.stack-source.com/msb/bashup # https://git.stack-source.com/msb/bashup
# MIT License Copyright (c) 2021 Matthew Saunders Brown # MIT License Copyright (c) 2021 Matthew Saunders Brown
# backups are stored here # load include file
backup_storage_dir='/mnt/backups'; source $(dirname $0)/bashup.sh
# directories that are backed up as part of files
backup_dirs=('/etc' '/home' '/srv' '/root' '/usr/local' '/var/www');
# check for local config, which can be used to override any of the above
if [[ -f /usr/local/etc/bashup.cnf ]]; then
source /usr/local/etc/bashup.cnf
fi
# require root
if [ "${EUID}" -ne 0 ]; then
echo "This script must be run as root"
exit 1
fi
help() help()
{ {
@ -64,29 +51,6 @@ while getopts "b:c:h" opt; do
esac esac
done done
if [ ! -d $backup_storage_dir ]; then
echo "ERROR: Backup storage dir ($backup_storage_dir) does not exist."
exit 1
fi
# check if backup_storage_dir is a mount in fstab
grep -qs " $backup_storage_dir " /etc/fstab
if [ $? -eq 0 ]; then
# check if backup_storage_dir is already mounted
grep -qs " $backup_storage_dir " /proc/mounts
if [ $? -ne 0 ]; then
# attempt to mount backups
mount $backup_storage_dir
# re-check for backups mount
grep -qs " $backup_storage_dir " /proc/mounts
if [ $? -ne 0 ]; then
echo "ERROR: failed to mount $backup_storage_dir"
exit 1
fi
fi
fi
if [ ! -z "$backup" ]; then if [ ! -z "$backup" ]; then
if [ -d "$backup_storage_dir/$backup" ]; then if [ -d "$backup_storage_dir/$backup" ]; then
if [ ! -z "$category" ]; then if [ ! -z "$category" ]; then
@ -131,10 +95,6 @@ else
ls -1 $backup_storage_dir | grep -v lost+found ls -1 $backup_storage_dir | grep -v lost+found
fi fi
# check if backup_storage_dir is mounted and unmount if so bashup::unmount_storage_dir
grep -qs " $backup_storage_dir " /proc/mounts
if [ $? -eq 0 ]; then
umount $backup_storage_dir
fi
exit 0 exit 0

View File

@ -4,21 +4,8 @@
# https://git.stack-source.com/msb/bashup # https://git.stack-source.com/msb/bashup
# MIT License Copyright (c) 2021 Matthew Saunders Brown # MIT License Copyright (c) 2021 Matthew Saunders Brown
# backup storage directory # load include file
backup_storage_dir='/mnt/backups'; source $(dirname $0)/bashup.sh
# directories to be backed up
backup_dirs=('/etc' '/home' '/srv' '/root' '/usr/local' '/var/www');
# check for local config, which can be used to override any of the above
if [[ -f /usr/local/etc/bashup.cnf ]]; then
source /usr/local/etc/bashup.cnf
fi
# require root
if [ "${EUID}" -ne 0 ]; then
echo "This script must be run as root"
exit 1
fi
help() help()
{ {
@ -61,30 +48,7 @@ while getopts "b:p:h" opt; do
esac esac
done done
if [ ! -d $backup_storage_dir ]; then bashup::set_existing_backups
echo "ERROR: Backup storage dir ($backup_storage_dir) does not exist."
exit 1
fi
# check if backup_storage_dir is a mount in fstab
grep -qs " $backup_storage_dir " /etc/fstab
if [ $? -eq 0 ]; then
# check if backup_storage_dir is already mounted
grep -qs " $backup_storage_dir " /proc/mounts
if [ $? -ne 0 ]; then
# attempt to mount backups
mount $backup_storage_dir
# re-check for backups mount
grep -qs " $backup_storage_dir " /proc/mounts
if [ $? -ne 0 ]; then
echo "ERROR: failed to mount $backup_storage_dir"
exit 1
fi
fi
fi
# get list of backups (dates)
existing_backups=($(ls $backup_storage_dir|grep -v lost+found))
# prompt if backup was not set on command line # prompt if backup was not set on command line
if [ -z "$backup" ] ; then if [ -z "$backup" ] ; then
@ -164,10 +128,6 @@ fi
"To restore $pathtorestore from $backup run this command:" "To restore $pathtorestore from $backup run this command:"
echo "/usr/bin/rsync -vn --archive --numeric-ids --one-file-system --delete $pathtobackup $pathtorestore" echo "/usr/bin/rsync -vn --archive --numeric-ids --one-file-system --delete $pathtobackup $pathtorestore"
# check if backup_storage_dir is mounted and unmount if so bashup::unmount_storage_dir
/usr/bin/grep -qs " $backup_storage_dir " /proc/mounts
if [ $? -eq 0 ]; then
/usr/bin/umount $backup_storage_dir
fi
exit 0 exit 0

View File

@ -4,21 +4,8 @@
# https://git.stack-source.com/msb/bashup # https://git.stack-source.com/msb/bashup
# MIT License Copyright (c) 2021 Matthew Saunders Brown # MIT License Copyright (c) 2021 Matthew Saunders Brown
# backup directory # load include file
backup_storage_dir='/mnt/backups'; source $(dirname $0)/bashup.sh
# config file that contains [client] config with 'host' 'user' 'password' settings
defaults_extra_file='/etc/mysql/debian.cnf';
# check for local config, which can be used to override any of the above
if [[ -f /usr/local/etc/bashup.cnf ]]; then
source /usr/local/etc/bashup.cnf
fi
# require root
if [ "${EUID}" -ne 0 ]; then
echo "This script must be run as root"
exit 1
fi
help() help()
{ {
@ -61,30 +48,7 @@ while getopts "b:d:h" opt; do
esac esac
done done
if [ ! -d $backup_storage_dir ]; then bashup::set_existing_backups
echo "ERROR: Backup storage dir ($backup_storage_dir) does not exist."
exit 1
fi
# check if backup_storage_dir is a mount in fstab
grep -qs " $backup_storage_dir " /etc/fstab
if [ $? -eq 0 ]; then
# check if backup_storage_dir is already mounted
grep -qs " $backup_storage_dir " /proc/mounts
if [ $? -ne 0 ]; then
# attempt to mount backups
mount $backup_storage_dir
# re-check for backups mount
grep -qs " $backup_storage_dir " /proc/mounts
if [ $? -ne 0 ]; then
echo "ERROR: failed to mount $backup_storage_dir"
exit 1
fi
fi
fi
# get list of backups (dates)
existing_backups=($(ls $backup_storage_dir|grep -v lost+found))
# prompt if backup was not set on command line # prompt if backup was not set on command line
if [ -z "$backup" ] ; then if [ -z "$backup" ] ; then
@ -149,10 +113,6 @@ else
exit 1 exit 1
fi fi
# check if backup_storage_dir is mounted and unmount if so bashup::unmount_storage_dir
grep -qs " $backup_storage_dir " /proc/mounts
if [ $? -eq 0 ]; then
umount $backup_storage_dir
fi
exit exit 0

View File

@ -4,19 +4,8 @@
# https://git.stack-source.com/msb/bashup # https://git.stack-source.com/msb/bashup
# MIT License Copyright (c) 2021 Matthew Saunders Brown # MIT License Copyright (c) 2021 Matthew Saunders Brown
# backup directory # load include file
backup_storage_dir='/mnt/backups'; source $(dirname $0)/bashup.sh
# check for local config, which can be used to override any of the above
if [[ -f /usr/local/etc/bashup.cnf ]]; then
source /usr/local/etc/bashup.cnf
fi
# require root
if [ "${EUID}" -ne 0 ]; then
echo "This script must be run as root"
exit 1
fi
help() help()
{ {
@ -59,30 +48,7 @@ while getopts "b:z:h" opt; do
esac esac
done done
if [ ! -d $backup_storage_dir ]; then bashup::set_existing_backups
echo "ERROR: Backup storage dir ($backup_storage_dir) does not exist."
exit 1
fi
# check if backup_storage_dir is a mount in fstab
grep -qs " $backup_storage_dir " /etc/fstab
if [ $? -eq 0 ]; then
# check if backup_storage_dir is already mounted
grep -qs " $backup_storage_dir " /proc/mounts
if [ $? -ne 0 ]; then
# attempt to mount backups
mount $backup_storage_dir
# re-check for backups mount
grep -qs " $backup_storage_dir " /proc/mounts
if [ $? -ne 0 ]; then
echo "ERROR: failed to mount $backup_storage_dir"
exit 1
fi
fi
fi
# get list of backups (dates)
existing_backups=($(ls $backup_storage_dir|grep -v lost+found))
# prompt if backup was not set on command line # prompt if backup was not set on command line
if [ -z "$backup" ] ; then if [ -z "$backup" ] ; then
@ -147,10 +113,6 @@ else
exit 1 exit 1
fi fi
# check if backup_storage_dir is mounted and unmount if so bashup::unmount_storage_dir
grep -qs " $backup_storage_dir " /proc/mounts
if [ $? -eq 0 ]; then
umount $backup_storage_dir
fi
exit exit 0

125
bashup.sh Executable file
View File

@ -0,0 +1,125 @@
#!/bin/bash
#
# Bashup - A set of bash scripts for managing backups.
# https://git.stack-source.com/msb/bashup
# MIT License Copyright (c) 2021 Matthew Saunders Brown
#
# begin configurable vars
#
# retention vars
retention_years=0;
retention_months=3;
retention_weeks=5;
retention_days=7;
# backup storage directory
backup_storage_dir='/mnt/backups';
# directories to be backed up by files
backup_dirs=('/etc' '/home' '/srv' '/root' '/usr/local' '/var/www');
# mysql config file that contains 'host' 'user' 'password' vars
defaults_extra_file='/etc/mysql/debian.cnf';
# list of mysql databases to skip
exclusions=('information_schema' 'performance_schema' 'sys' 'wsrep');
#
# end configurable vars
#
# must be root, attempt sudo if need be
if [ "${EUID}" -ne 0 ]; then
exec sudo -u root --shell /bin/bash $0 $@
fi
# check for backup storage directory and mount if need be
if [ -d $backup_storage_dir ]; then
# check if backup_storage_dir is a mount in fstab
grep -qs " $backup_storage_dir " /etc/fstab
if [ $? -eq 0 ]; then
# check if backup_storage_dir is already mounted
grep -qs " $backup_storage_dir " /proc/mounts
if [ $? -ne 0 ]; then
# attempt to mount backups
mount $backup_storage_dir
# re-check for backups mount
grep -qs " $backup_storage_dir " /proc/mounts
if [ $? -ne 0 ]; then
echo "ERROR: failed to mount $backup_storage_dir"
exit 1
fi
fi
fi
else
echo "ERROR: Backup storage dir ($backup_storage_dir) does not exist."
exit 1
fi
# get todays date (backup dir name)
today=$(date +%Y%m%d)
# set existing_backups array
existing_backups=($(ls $backup_storage_dir|grep -v lost+found))
# if script is a *-backup-* script remove today from existing backups, if it exists. we do other checks avoid re-doing backups
if [[ "$0" == *"-backup-"* ]];then
if [[ " ${existing_backups[@]} " =~ " ${today} " ]]; then
unset 'existing_backups[-1]';
fi
fi
function bashup::set_retention_array () {
declare -a retention_array
# set retention days
if [ $retention_days -gt 0 ]; then
i="0"
while [ $i -lt $retention_days ]; do
DATE=`date --date="$i day ago" +%Y%m%d`
retention_array[$DATE]="$DATE"
i=$[$i+1]
done
fi
# set retention weeks
if [ $retention_weeks -gt 0 ]; then
i="0"
while [ $i -lt $retention_weeks ]; do
i=$[$i+1]
DATE=`date --date="sunday - $i week" +%Y%m%d`
retention_array[$DATE]="$DATE"
done
fi
# set retention months
if [ $retention_months -gt 0 ]; then
i="0"
while [ $i -lt $retention_months ]; do
DATE=`date --date="$i month ago" +%Y%m01`
retention_array[$DATE]="$DATE"
i=$[$i+1]
done
fi
# set retention years
if [ $retention_years -gt 0 ]; then
i="0"
while [ $i -lt $retention_years ]; do
DATE=`date --date="$i year ago" +%Y0101`
retention_array[$DATE]="$DATE"
i=$[$i+1]
done
fi
}
function bashup::unmount_storage_dir () {
# check if backup_storage_dir is mounted and unmount if so
grep -qs " $backup_storage_dir " /proc/mounts
if [ $? -eq 0 ]; then
umount $backup_storage_dir
fi
}