rs-backup-suite/server/usr/sbin/rs-rotate-cron

64 lines
2.4 KiB
Bash
Executable File

#!/bin/sh
##
# Copyright (C) 2013-2014 Janek Bevendorff
# Website: http://www.refining-linux.org/
#
# Create daily, weekly or monthly snapshots from manual push backups
#
# The MIT License (MIT)
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
##
if [ "$1" == "" ]; then
. rs-version
echo "Usage: $(basename $0) <backup level>"
exit
fi
. /etc/rs-backup/server-config
for home_dir in "${BACKUP_ROOT}"/*; do
if [ -d "${home_dir}/${FILES_DIR}" ] && [ -e "${home_dir}/rsnapshot.conf" ]; then
# Since we're relying on the client to push changes, there isn't
# necessarily a new increment pushed yet.
# Only rotate if there are really new increments to prevent
# successive backup deletion
config=$(cat "${BACKUP_ROOT}/etc/rsnapshot.global.conf")
# Get number of preceding increments
config=$(echo "${config}" | grep -P '^retain\t')
config=$(echo "${config}" | grep -oPz "retain\t+(\w+)\t+(\d+)\nretain\s+${1}\t+" | sed -n 1p)
preceding_name=$(echo "${config}" | awk '{ print $2 }')
preceding_number=$(($(echo "${config}" | awk ' { print $3 }') - 1))
# Continue if no proper preceding increment could be found
if [ "${preceding_name}" == "" ] ||
[ ! -d "${home_dir}/${FILES_DIR}/${preceding_name}.${preceding_number}" ]; then
echo "Not rotating ${1}"
continue
fi
cd "${home_dir}/${FILES_DIR}"
owner=$(ls -ld "${home_dir}/${FILES_DIR}" | awk '{ print $3 }')
su - "${owner}" -c "rsnapshot -c '${home_dir}/rsnapshot.conf' '$1'"
fi
done