Reorganize temp files and lock files
Temp files/dirs all share a common prefix, so we can delete them all effectively Lock file is managed globally for bkctld-rm. No need for a lock file for bkctld-inc anymore since there is a check on the inc directory before starting.
This commit is contained in:
parent
f66d832d3a
commit
32f242fe9c
|
@ -8,65 +8,33 @@
|
||||||
LIBDIR="$(dirname $0)" && . "${LIBDIR}/includes"
|
LIBDIR="$(dirname $0)" && . "${LIBDIR}/includes"
|
||||||
|
|
||||||
create_inc_btrfs() {
|
create_inc_btrfs() {
|
||||||
jail_name=$1
|
jail_name=${1:?}
|
||||||
inc_name=$2
|
inc_name=${2:?}
|
||||||
|
|
||||||
jail_path=$(jail_path "${jail_name}")
|
jail_path=$(jail_path "${jail_name}")
|
||||||
inc_path=$(inc_path "${jail_name}" "${inc_name}")
|
inc_path=$(inc_path "${jail_name}" "${inc_name}")
|
||||||
|
|
||||||
# The lock file prevents from starting a new copy when one is already being done
|
if dry_run; then
|
||||||
lock_file="${LOCKDIR}/inc-${jail_name}-${inc_name}.lock"
|
echo "[dry-run] btrfs subvolume snapshot of ${jail_path} to ${inc_path}"
|
||||||
if [ -f "${lock_file}" ]; then
|
|
||||||
warning "${jail_name}: skipping '${inc_name}', it is already being created."
|
|
||||||
else
|
else
|
||||||
(
|
mkdir --parents "$(dirname "${inc_path}")"
|
||||||
start=$(current_time)
|
# create a btrfs readonly snapshot from the jail
|
||||||
mkdir --parents "${LOCKDIR}" && touch "${lock_file}"
|
/bin/btrfs subvolume snapshot -r "${jail_path}" "${inc_path}" | debug
|
||||||
# shellcheck disable=SC2064
|
|
||||||
trap "rm -f ${lock_file}" 0
|
|
||||||
|
|
||||||
if dry_run; then
|
|
||||||
echo "[dry-run] btrfs subvolume snapshot of ${jail_path} to ${inc_path}"
|
|
||||||
else
|
|
||||||
mkdir --parents "$(dirname "${inc_path}")"
|
|
||||||
# create a btrfs readonly snapshot from the jail
|
|
||||||
/bin/btrfs subvolume snapshot -r "${jail_path}" "${inc_path}" | debug
|
|
||||||
fi
|
|
||||||
|
|
||||||
end=$(current_time)
|
|
||||||
notice "${jail_name}: inc '${inc_name}' has been created [${start}/${end}]"
|
|
||||||
)
|
|
||||||
fi
|
fi
|
||||||
}
|
}
|
||||||
create_inc_ext4() {
|
create_inc_ext4() {
|
||||||
jail_name=$1
|
jail_name=${1:?}
|
||||||
inc_name=$2
|
inc_name=${2:?}
|
||||||
|
|
||||||
jail_path=$(jail_path "${jail_name}")
|
jail_path=$(jail_path "${jail_name}")
|
||||||
inc_path=$(inc_path "${jail_name}" "${inc_name}")
|
inc_path=$(inc_path "${jail_name}" "${inc_name}")
|
||||||
|
|
||||||
# The lock file prevents from starting a new copy when one is already being done
|
if dry_run; then
|
||||||
lock_file="${LOCKDIR}/inc-${jail_name}-${inc_name}.lock"
|
echo "[dry-run] copy of ${jail_path} to ${inc_path}"
|
||||||
if [ -f "${lock_file}" ]; then
|
|
||||||
warning "${jail_name}: skipping '${inc_name}', it is already being created."
|
|
||||||
else
|
else
|
||||||
(
|
mkdir --parents "$(dirname "${inc_path}")"
|
||||||
start=$(current_time)
|
# create a copy of the jail with hard links
|
||||||
mkdir --parents "${LOCKDIR}" && touch "${lock_file}"
|
cp --archive --link --one-file-system "${jail_path}/" "${inc_path}"
|
||||||
# shellcheck disable=SC2064
|
|
||||||
trap "rm -f ${lock_file}" 0
|
|
||||||
|
|
||||||
if dry_run; then
|
|
||||||
echo "[dry-run] copy of ${jail_path} to ${inc_path}"
|
|
||||||
else
|
|
||||||
mkdir --parents "$(dirname "${inc_path}")"
|
|
||||||
# create a copy of the jail with hard links
|
|
||||||
cp --archive --link --one-file-system "${jail_path}/" "${inc_path}"
|
|
||||||
fi
|
|
||||||
|
|
||||||
end=$(current_time)
|
|
||||||
notice "${jail_name}: in '${inc_name}' has been created [${start}/${end}]"
|
|
||||||
)
|
|
||||||
fi
|
fi
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -79,17 +47,22 @@ for jail_name in $(jails_list); do
|
||||||
|
|
||||||
# If no incs policy is found, we don't create incs
|
# If no incs policy is found, we don't create incs
|
||||||
if [ -n "${incs_policy_file}" ]; then
|
if [ -n "${incs_policy_file}" ]; then
|
||||||
# If not incs directory is found, we don't create incs
|
# If no incs directory is found, we don't create incs
|
||||||
if [ ! -d "${inc_path}" ]; then
|
if [ ! -d "${inc_path}" ]; then
|
||||||
|
start=$(current_time)
|
||||||
|
|
||||||
if is_btrfs "${jail_path}"; then
|
if is_btrfs "${jail_path}"; then
|
||||||
create_inc_btrfs "${jail_name}" "${inc_name}"
|
create_inc_btrfs "${jail_name}" "${inc_name}"
|
||||||
else
|
else
|
||||||
create_inc_ext4 "${jail_name}" "${inc_name}"
|
create_inc_ext4 "${jail_name}" "${inc_name}"
|
||||||
fi
|
fi
|
||||||
|
|
||||||
|
end=$(current_time)
|
||||||
|
notice "${jail_name}: \`${inc_name}' has been created [${start}/${end}]"
|
||||||
else
|
else
|
||||||
warning "${jail_name}: skipping ${inc_name}, it already exists."
|
warning "${jail_name}: skipping \`${inc_name}', it already exists."
|
||||||
fi
|
fi
|
||||||
else
|
else
|
||||||
warning "${jail_name}: skipping ${inc_name}, incs policy not found."
|
notice "${jail_name}: skipping \`${inc_name}', incs policy not found."
|
||||||
fi
|
fi
|
||||||
done
|
done
|
||||||
|
|
117
lib/bkctld-rm
117
lib/bkctld-rm
|
@ -7,32 +7,9 @@
|
||||||
# shellcheck source=./includes
|
# shellcheck source=./includes
|
||||||
LIBDIR="$(dirname $0)" && . "${LIBDIR}/includes"
|
LIBDIR="$(dirname $0)" && . "${LIBDIR}/includes"
|
||||||
|
|
||||||
delete_inc_btrfs() {
|
kill_or_clean_lockfile() {
|
||||||
jail_name=$1
|
lock_file=${1:-}
|
||||||
inc_name=$2
|
|
||||||
|
|
||||||
inc_path=$(inc_path "${jail_name}" "${inc_name}")
|
|
||||||
|
|
||||||
start=$(current_time)
|
|
||||||
|
|
||||||
if dry_run; then
|
|
||||||
echo "[dry-run] delete btrfs subvolume ${inc_path}"
|
|
||||||
else
|
|
||||||
/bin/btrfs subvolume delete "${inc_path}" | debug
|
|
||||||
fi
|
|
||||||
|
|
||||||
end=$(current_time)
|
|
||||||
notice "${jail_name}: inc '${inc_name}' has been deleted [${start}/${end}]"
|
|
||||||
}
|
|
||||||
delete_inc_ext4() {
|
|
||||||
jail_name=$1
|
|
||||||
inc_name=$2
|
|
||||||
|
|
||||||
inc_path=$(inc_path "${jail_name}" "${inc_name}")
|
|
||||||
|
|
||||||
start=$(current_time)
|
|
||||||
|
|
||||||
lock_file="${LOCKDIR}/rm-global.lock"
|
|
||||||
if [ -f "${lock_file}" ]; then
|
if [ -f "${lock_file}" ]; then
|
||||||
# Get Process ID from the lock file
|
# Get Process ID from the lock file
|
||||||
pid=$(cat "${lock_file}")
|
pid=$(cat "${lock_file}")
|
||||||
|
@ -52,59 +29,97 @@ delete_inc_ext4() {
|
||||||
# Remove the lock file
|
# Remove the lock file
|
||||||
rm -f ${lock_file}
|
rm -f ${lock_file}
|
||||||
fi
|
fi
|
||||||
|
}
|
||||||
|
incs_to_delete() {
|
||||||
|
jail_name=${1:?}
|
||||||
|
incs_policy_file=${2:?}
|
||||||
|
|
||||||
mkdir --parents "${LOCKDIR}" && echo $$ > ${lock_file} || error "Failed to acquire lock file '${lock_file}'"
|
incs_policy_keep_file=$(new_tmp_file "${jail_name}.incs_policy_keep")
|
||||||
empty=$(mktemp -d --suffix ".${$}" --tmpdir bkctld.XXXXX)
|
incs_list_file=$(new_tmp_file "${jail_name}.incs_list")
|
||||||
# shellcheck disable=SC2064
|
|
||||||
trap "rm -f ${lock_file}; rmdir ${empty}" 0
|
# loop for each line in jail configuration
|
||||||
|
for incs_policy_line in $(grep "^\+" ${incs_policy_file}); do
|
||||||
|
# inc date in ISO format
|
||||||
|
incs_policy_date=$(relative_date ${incs_policy_line})
|
||||||
|
echo ${incs_policy_date} >> "${incs_policy_keep_file}"
|
||||||
|
done
|
||||||
|
for inc_name in $(incs_list "${jail_name}"); do
|
||||||
|
echo "${inc_name}" >> ${incs_list_file}
|
||||||
|
done
|
||||||
|
|
||||||
|
# shellcheck disable=SC2046
|
||||||
|
incs_to_delete=$(grep -v -f "${incs_policy_keep_file}" "${incs_list_file}")
|
||||||
|
|
||||||
|
rm -f "${incs_policy_keep_file}" "${incs_list_file}"
|
||||||
|
|
||||||
|
echo ${incs_to_delete}
|
||||||
|
}
|
||||||
|
delete_inc_btrfs() {
|
||||||
|
jail_name=$1
|
||||||
|
inc_name=$2
|
||||||
|
|
||||||
|
inc_path=$(inc_path "${jail_name}" "${inc_name}")
|
||||||
|
|
||||||
if dry_run; then
|
if dry_run; then
|
||||||
echo "[dry-run] delete ${inc_path} with rsync from ${empty}"
|
echo "[dry-run] delete btrfs subvolume ${inc_path}"
|
||||||
else
|
else
|
||||||
|
/bin/btrfs subvolume delete "${inc_path}" | debug
|
||||||
|
fi
|
||||||
|
}
|
||||||
|
delete_inc_ext4() {
|
||||||
|
jail_name=$1
|
||||||
|
inc_name=$2
|
||||||
|
|
||||||
|
inc_path=$(inc_path "${jail_name}" "${inc_name}")
|
||||||
|
|
||||||
|
if dry_run; then
|
||||||
|
echo "[dry-run] delete ${inc_path} with rsync from empty directory"
|
||||||
|
else
|
||||||
|
empty=$(new_tmp_dir "empty")
|
||||||
rsync --archive --delete "${empty}/" "${inc_path}/"
|
rsync --archive --delete "${empty}/" "${inc_path}/"
|
||||||
rmdir "${inc_path}/"
|
rmdir "${inc_path}/"
|
||||||
|
rmdir "${empty}"
|
||||||
fi
|
fi
|
||||||
# Remove the lock file
|
|
||||||
rm -f ${lock_file}
|
|
||||||
|
|
||||||
end=$(current_time)
|
|
||||||
notice "${jail_name}: inc '${inc_name}' has been deleted [${start}/${end}]"
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
lock_file="${LOCKDIR}/rm-global.lock"
|
||||||
|
# shellcheck disable=SC2064
|
||||||
|
trap "rm -f ${lock_file}; cleanup_tmp;" 0
|
||||||
|
|
||||||
|
kill_or_clean_lockfile "${lock_file}"
|
||||||
|
new_lock_file "${lock_file}"
|
||||||
|
|
||||||
for jail_name in $(jails_list); do
|
for jail_name in $(jails_list); do
|
||||||
incs_policy_file=$(current_jail_incs_policy_file ${jail_name})
|
incs_policy_file=$(current_jail_incs_policy_file ${jail_name})
|
||||||
|
# If no incs policy is found, we don't remove incs
|
||||||
# If not incs policy if found, we don't remove incs
|
|
||||||
if [ -n "${incs_policy_file}" ]; then
|
if [ -n "${incs_policy_file}" ]; then
|
||||||
incs_policy_keep_file="$(mktemp)"
|
|
||||||
incs_list_file="$(mktemp)"
|
|
||||||
# shellcheck disable=SC2064
|
|
||||||
trap "rm -f ${incs_policy_keep_file} ${incs_list_file}" 0
|
|
||||||
|
|
||||||
# loop for each line in jail configuration
|
|
||||||
for incs_policy_line in $(grep "^\+" ${incs_policy_file}); do
|
|
||||||
# inc date in ISO format
|
|
||||||
incs_policy_date=$(relative_date ${incs_policy_line})
|
|
||||||
echo ${incs_policy_date} >> "${incs_policy_keep_file}"
|
|
||||||
done
|
|
||||||
for inc_name in $(incs_list "${jail_name}"); do
|
|
||||||
echo "${inc_name}" >> ${incs_list_file}
|
|
||||||
done
|
|
||||||
# shellcheck disable=SC2046
|
# shellcheck disable=SC2046
|
||||||
incs_to_delete=$(grep -v -f "${incs_policy_keep_file}" "${incs_list_file}")
|
incs_to_delete=$(incs_to_delete "${jail_name}" "${incs_policy_file}")
|
||||||
|
|
||||||
if [ -n "${incs_to_delete}" ]; then
|
if [ -n "${incs_to_delete}" ]; then
|
||||||
debug "${jail_name}: incs to be deleted : $(echo "${incs_to_delete}" | tr '\n', ',' | sed 's/,$//')."
|
debug "${jail_name}: incs to be deleted : $(echo "${incs_to_delete}" | tr '\n', ',' | sed 's/,$//')."
|
||||||
for inc_name in ${incs_to_delete}; do
|
for inc_name in ${incs_to_delete}; do
|
||||||
|
start=$(current_time)
|
||||||
|
|
||||||
inc_path=$(inc_path "${jail_name}" "${inc_name}")
|
inc_path=$(inc_path "${jail_name}" "${inc_name}")
|
||||||
if is_btrfs "${inc_path}"; then
|
if is_btrfs "${inc_path}"; then
|
||||||
delete_inc_btrfs "${jail_name}" "${inc_name}"
|
delete_inc_btrfs "${jail_name}" "${inc_name}"
|
||||||
else
|
else
|
||||||
delete_inc_ext4 "${jail_name}" "${inc_name}"
|
delete_inc_ext4 "${jail_name}" "${inc_name}"
|
||||||
fi
|
fi
|
||||||
|
|
||||||
|
end=$(current_time)
|
||||||
|
notice "${jail_name}: inc \`${inc_name}' has been deleted [${start}/${end}]"
|
||||||
done
|
done
|
||||||
else
|
else
|
||||||
notice "${jail_name}: no inc to be deleted."
|
notice "${jail_name}: no inc to be deleted."
|
||||||
fi
|
fi
|
||||||
|
else
|
||||||
|
notice "${jail_name}: skipping jail because incs policy is missing."
|
||||||
fi
|
fi
|
||||||
done
|
done
|
||||||
|
|
||||||
|
# Remove the lock file and cleanup tmp files
|
||||||
|
rm -f "${lock_file}"
|
||||||
|
cleanup_tmp
|
||||||
|
|
19
lib/includes
19
lib/includes
|
@ -177,6 +177,25 @@ relative_date() {
|
||||||
|
|
||||||
echo ${past_date}
|
echo ${past_date}
|
||||||
}
|
}
|
||||||
|
new_tmp_file() {
|
||||||
|
name=${1:-}
|
||||||
|
|
||||||
|
mktemp --tmpdir=/tmp "bkctld.${$}.${name}.XXXXX"
|
||||||
|
}
|
||||||
|
new_tmp_dir() {
|
||||||
|
name=${1:-}
|
||||||
|
|
||||||
|
mktemp --directory --tmpdir=/tmp "bkctld.${$}.${name}.XXXXX"
|
||||||
|
}
|
||||||
|
cleanup_tmp() {
|
||||||
|
find /tmp -name "bkctld.${$}.*" -delete
|
||||||
|
}
|
||||||
|
new_lock_file() {
|
||||||
|
lock_file=${1:-}
|
||||||
|
lock_dir=$(dirname "${lock_file}")
|
||||||
|
|
||||||
|
mkdir --parents "${lock_dir}" && echo $$ > ${lock_file} || error "Failed to acquire lock file '${lock_file}'"
|
||||||
|
}
|
||||||
|
|
||||||
setup_jail_chroot() {
|
setup_jail_chroot() {
|
||||||
jail_name=${1:?}
|
jail_name=${1:?}
|
||||||
|
|
Loading…
Reference in a new issue