From 3597383a5ac44e7431c4dc8db3172059bf36c814 Mon Sep 17 00:00:00 2001 From: Carlos Galindo Date: Mon, 7 Oct 2019 11:42:28 +0200 Subject: [PATCH] initial commit --- src/backbit | 40 +++++++++++++++++++++++++++ src/backbitdb | 75 ++++++++++++++++++++++++++++++++++++++++++++++++++ src/backbitdir | 72 ++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 187 insertions(+) create mode 100755 src/backbit create mode 100755 src/backbitdb create mode 100755 src/backbitdir diff --git a/src/backbit b/src/backbit new file mode 100755 index 0000000..338b08b --- /dev/null +++ b/src/backbit @@ -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 diff --git a/src/backbitdb b/src/backbitdb new file mode 100755 index 0000000..8be3afc --- /dev/null +++ b/src/backbitdb @@ -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}" diff --git a/src/backbitdir b/src/backbitdir new file mode 100755 index 0000000..5b789b5 --- /dev/null +++ b/src/backbitdir @@ -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}"