initial commit
This commit is contained in:
commit
3597383a5a
3 changed files with 187 additions and 0 deletions
72
src/backbitdir
Executable file
72
src/backbitdir
Executable 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}"
|
Loading…
Add table
Add a link
Reference in a new issue