initial commit

This commit is contained in:
Carlos Galindo 2019-10-07 11:42:28 +02:00
commit 3597383a5a
Signed by: kauron
GPG key ID: 83E68706DEE119A3
3 changed files with 187 additions and 0 deletions

40
src/backbit Executable file
View file

@ -0,0 +1,40 @@
#!/bin/bash
if [ -z $1 -o ! -d $1 ]
then
confDir=/etc/backbit
else
confDir=$1
fi
logDir=/var/log/backbit
if [ ! -d $logDir ]
then
mkdir -p $logDir
chmod 700 $logDir
fi
if [ -x $confDir/before.sh ]
then
. $confDir/before.sh
fi
for $conf in $(ls $confDir/db/*)
do
( source $conf
$(which backbitdb) > $logDir/$dbName.log
)
done
for $conf in $(ls $confDir/dir/*)
do
( source $conf
$(which backbitdir) > $logDir/$name.log
)
done
if [ -x $confDir/after.sh ]
then
. $confDir/after.sh
fi

75
src/backbitdb Executable file
View file

@ -0,0 +1,75 @@
#!/bin/bash
# Backup a sql database, args come from the environment
# $1, if it exists, contains a list of declarations
if [ -e $1 ]; then
source $1
fi
if [ -z "${outDir}" -o -z "${dbName}" -o -z "${dbPassword}" -o -z "${dbUser}" \
-o -z "${maxBackups}" -o -z "${service}" ]
then
echo "ERROR: Missing data, you need to declare the following:"
echo "\toutDir: the directory where the backup will be saved"
echo "\tdbName, dbUser, dbPassword: database settings"
echo "\tservice: the service to be stopped (webserver)"
echo "\tmaxBackups: the number of backups to keep (0 = infinite)"
exit 1
fi
if [ "$(id -u)" != "0" ]
then
echo "ERROR: this script has to be run as root!"
exit 1
fi
dateDir="${outDir}/$(date +"%Y%m%d_%H%M%S")"
if [ ! -d "${dateDir}" ]
then
mkdir -p "${dateDir}"
else
echo "ERROR: the backup directory ${dateDir} already exists!"
exit 1
fi
echo "Begin backup for sql db ${dbName}, at ${dateDir}"
echo -n "Stopping service ${service}..."
systemctl stop "${service}"
echo " OK"
trap 'echo -n "Starting service ${service}..."; systemctl start "${service}"; echo " OK"; exit 2' 1 2 3 9 15
echo -n "Backing up ${dbName} database..."
mysqldump --single-transaction -h localhost -u "${dbUser}" -p"${dbPassword}" "${dbName}" > "${dateDir}/${dbName}.sql"
echo " OK"
echo -n "Compressing backed up database..."
pushd "${dateDir}" > /dev/null
xz -9 "${dbName}.sql"
sha256sum * > sha256sums
popd > /dev/null
echo " OK"
echo -n "Starting service ${service}..."
systemctl start "${service}"
echo " OK"
if (( ${maxBackups} != 0 ))
then
nrOfBackups=$(ls -l ${outDir} | grep -c ^d)
if (( ${nrOfBackups} > ${maxBackups} ))
then
echo "Removing old backups..."
ls -t ${outDir} | tail -$(( nrOfBackups - maxBackups )) | while read dirToRemove; do
echo -n "Deleting ${dirToRemove}..."
rm -r ${outDir}/${dirToRemove}
echo " OK"
done
fi
fi
echo "End of SQL backup for ${dbName}, at ${dateDir}"

72
src/backbitdir Executable file
View file

@ -0,0 +1,72 @@
#!/bin/bash
# Backup a directory, args come from the environment
# $1, if it exists, contains a list of declarations
if [ -e $1 ]; then
source $1
fi
if [ -z "${outDir}" -o -z "${dir}" -o -z "${maxBackups}" -o -z "${service}" \
-o -z "${name}" ]
then
echo "ERROR: Missing data, you need to declare the following:"
echo "\toutDir: the directory where the backup will be saved"
echo "\tdir: the directory to back up"
echo "\tname: the name of the backup"
echo "\tservice: the service to be stopped (webserver)"
echo "\tmaxBackups: the number of backups to keep (0 = infinite)"
exit 1
fi
if [ "$(id -u)" != "0" ]
then
echo "ERROR: this script has to be run as root!"
exit 1
fi
dateDir="${outDir}/$(date +"%Y%m%d_%H%M%S")"
if [ ! -d "${dateDir}" ]
then
mkdir -p "${dateDir}"
else
echo "ERROR: the backup directory ${dateDir} already exists!"
exit 1
fi
echo "Begin backup for ${name}, at ${dateDir}"
echo -n "Stopping service ${service}..."
systemctl stop "${service}"
echo " OK"
trap 'echo -n "Starting service ${service}..."; systemctl start "${service}"; echo " OK"; exit 2' 1 2 3 9 15
echo -n "Backing up ${name}..."
tar --preserve-permissions --absolute-names --create --file "${dateDir}/backup.tar.xz" $dir
pushd "${dateDir}" > /dev/null
sha256sum * > sha256sums
popd > /dev/null
echo " OK"
echo -n "Starting service ${service}..."
systemctl start "${service}"
echo " OK"
if (( ${maxBackups} != 0 ))
then
nrOfBackups=$(ls -l ${outDir} | grep -c ^d)
if (( ${nrOfBackups} > ${maxBackups} ))
then
echo "Removing old backups..."
ls -t ${outDir} | tail -$(( nrOfBackups - maxBackups )) | while read dirToRemove; do
echo -n "Deleting ${dirToRemove}..."
rm -r ${outDir}/${dirToRemove}
echo " OK"
done
fi
fi
echo "End of backup for ${name}, at ${dateDir}"