Files
cloudron-box/src/scripts/mvvolume.sh
T

47 lines
1.3 KiB
Bash
Raw Normal View History

2018-12-20 14:33:29 -08:00
#!/bin/bash
set -eu -o pipefail
if [[ ${EUID} -ne 0 ]]; then
echo "This script should be run as root." > /dev/stderr
exit 1
fi
if [[ $# -eq 0 ]]; then
echo "No arguments supplied"
exit 1
fi
if [[ "$1" == "--check" ]]; then
echo "OK"
exit 0
fi
source_dir="$1"
target_dir="$2"
if [[ "${BOX_ENV}" == "test" ]]; then
# be careful not to nuke some random directory when testing
2019-09-09 16:37:59 -07:00
[[ "${source_dir}" != *"/.cloudron_test/"* ]] && exit 1
[[ "${target_dir}" != *"/.cloudron_test/"* ]] && exit 1
2018-12-20 14:33:29 -08:00
fi
2025-06-14 21:14:59 +02:00
mkdir -p "${target_dir}"
2022-06-09 17:49:33 -07:00
source_stat=$(stat --format='%d,%i' "${source_dir}")
target_stat=$(stat --format='%d,%i' "${target_dir}")
# test sameness across bind mounts
if [[ "${source_stat}" == "${target_stat}" ]]; then
echo "Source dir and target dir are the same"
exit 0
fi
2018-12-20 14:33:29 -08:00
# copy and remove - this way if the copy fails, the original is intact
2022-06-08 12:24:11 -07:00
# the find logic is so that move to a one level subdir works (and we also move hidden files)
2018-12-20 14:33:29 -08:00
find "${source_dir}" -maxdepth 1 -mindepth 1 -not -wholename "${target_dir}" -exec cp -ar '{}' "${target_dir}" \;
find "${source_dir}" -maxdepth 1 -mindepth 1 -not -wholename "${target_dir}" -exec rm -rf '{}' \;
2019-01-18 14:48:31 -08:00
# this will fail if target is a subdir or if source is a mountpoint
2018-12-20 14:33:29 -08:00
rmdir "${source_dir}" || true