gen2stage4/gen2sync

219 lines
5.4 KiB
Text
Raw Normal View History

2023-05-23 19:08:22 -04:00
#!/usr/bin/env bash
# set flag variables to null/default
2024-08-03 15:37:28 -04:00
optExcludeBoot=false
optExcludeConfidential=false
optExcludeLost=true
optQuiet=false
optUserExclude=()
function showHelp() {
echo "Usage:"
echo "$(basename "$0") [-b -c -l -q] [-s || -t <target>] [-e <exclude>...] [-i <include>...] <dest> [-- [rsync-opts]]"
echo "Position Arguments:"
echo " <dest> destination path to copy system files to"
echo " [rsync-opts] additional options to pass to rsync command"
echo
echo "Options:"
echo " -b excludes boot directory"
echo " -c excludes some confidential files (currently only .bash_history and connman network lists)"
echo " -l includes lost+found directory"
echo " -q activates quiet mode (no confirmation)"
echo " -s makes copy of current system"
echo " -t <path> makes copy of system located at the <path>"
echo " -e <exclude> an additional exclude directory (one dir one -e, do not use it with *)"
echo " -i <include> an additional include. This has higher precedence than -e, -t, and -s"
echo " -h display this help message."
if [[ "$1" -ge 0 ]]; then
exit "$1"
else
exit 0
fi
}
function errorMsg() {
local rc=0
if [[ "$1" -gt 0 ]]; then
rc="$1"
shift
fi
echo "$*" >&2
if [[ "$rc" -gt 0 ]]; then
exit "$rc"
fi
}
2023-05-23 19:08:22 -04:00
# reads options:
2024-08-03 15:37:28 -04:00
while [[ $# -gt 0 ]]; do
while getopts ":t:e:i:sqcblh" flag; do
case "$flag" in
t) sourcePath="$OPTARG";;
s) sourcePath="/";;
q) optQuiet=true;;
c) optExcludeConfidential=true;;
b) optExcludeBoot=true;;
l) optExcludeLost=false;;
e) optUserExclude+=("$OPTARG");;
i) optUserInclude+=("$OPTARG");;
h) showHelp 0;;
\?) errorMsg 1 "Invalid option: -$OPTARG";;
:) errorMsg 1 "Option -$OPTARG requires an argument.";;
esac
done || exit 1
[[ $OPTIND -gt $# ]] && break # reached the end of parameters
shift $((OPTIND - 1)) # Free processed options so far
OPTIND=1 # we must reset OPTIND
if [[ -z "$targetPath" ]]; then
targetPath=$1
else
rsyncArgs[${#rsyncArgs[*]}]=$1
fi
#args[${#args[*]}]=$1 # save first non-option argument (a.k.a. positional argument)
shift # remove saved arg
2023-05-23 19:08:22 -04:00
done
2024-08-03 15:37:28 -04:00
# checks if run as root:
if [[ "$(id -u)" -ne 0 ]]; then
echo "$(basename "$0"): must run as root"
exit 250
fi
2024-08-03 15:37:28 -04:00
if [[ -z "$sourcePath" ]]; then
echo "$(basename "$0"): no source path specified"
2023-05-23 19:08:22 -04:00
exit 1
2024-08-03 15:37:28 -04:00
elif [[ ! -d "$sourcePath" ]]; then
echo "$(basename "$0"): no source path does not exist"
2023-05-23 19:08:22 -04:00
exit 1
2024-08-03 15:37:28 -04:00
elif [[ "$sourcePath" != */ ]]; then
# make sure source path ends with slash
sourcePath="${sourcePath}/"
2023-05-23 19:08:22 -04:00
fi
2024-08-03 15:37:28 -04:00
if [[ -z "$targetPath" ]]; then
echo "$(basename "$0"): no destination path specified"
exit 1
elif [[ ! -d "$targetPath" ]]; then
echo "$(basename "$0"): destination path (\`$targetPath\`) does not exist"
exit 1
elif [[ "$targetPath" != */ ]]; then
# make sure destination path ends with slash
targetPath="${targetPath}/"
2023-05-23 19:08:22 -04:00
fi
# Excludes:
2024-08-03 15:37:28 -04:00
syncExcludes=(
"dev/*"
"var/tmp/*"
"media/*"
"mnt/*/*"
"proc/*"
"run/*"
"sys/*"
"tmp/*"
"var/lock/*"
"var/log/*"
"var/run/*"
"var/lib/docker/*"
"var/lib/containers/*"
"var/lib/machines/*"
"var/lib/libvirt/*"
"var/lib/lxd/*"
"home/*/*"
)
#EXCLUDES_DEFAULT_PORTAGE=(
syncExcludesPortage=(
"var/db/repos/*/*"
"var/cache/distfiles/*"
"var/cache/binpkgs/*"
"usr/portage/*"
2023-05-23 19:08:22 -04:00
)
2024-08-03 15:37:28 -04:00
syncIncludes=(
"dev/console"
"dev/null"
"var/db/pkg/*"
2023-05-23 19:08:22 -04:00
)
2024-08-03 15:37:28 -04:00
syncExcludes=("${syncExcludes[@]/#/"$sourcePath"}")
syncIncludes=("${syncIncludes[@]/#/"$sourcePath"}")
2023-05-23 19:08:22 -04:00
2024-08-03 15:37:28 -04:00
if [[ "$sourcePath" == '/' ]]; then
if command -v portageq &>/dev/null; then
portageRepos=$(portageq get_repos /)
for i in ${portageRepos}; do
repoPath=$(portageq get_repo_path / "${i}")
syncExcludes+=("${repoPath}/*")
2023-05-23 19:08:22 -04:00
done
2024-08-03 15:37:28 -04:00
syncExcludes+=("$(portageq distdir)/*")
syncExcludes+=("$(portageq pkgdir)/*")
2023-05-23 19:08:22 -04:00
else
2024-08-03 15:37:28 -04:00
syncExcludes+=("${syncExcludesPortage[@]/#/"/"}")
2023-05-23 19:08:22 -04:00
fi
else
2024-08-03 15:37:28 -04:00
syncExcludes+=("${syncExcludesPortage[@]/#/"$sourcePath"}")
2023-05-23 19:08:22 -04:00
fi
2024-08-03 15:37:28 -04:00
if $optExcludeConfidential; then
syncExcludes+=("${sourcePath}home/*/.bash_history")
syncExcludes+=("${sourcePath}root/.bash_history")
syncExcludes+=("${sourcePath}var/lib/connman/*")
2023-05-23 19:18:51 -04:00
fi
2024-08-03 15:37:28 -04:00
if $optExcludeBoot; then
syncExcludes+=("${sourcePath}boot/*")
2023-05-23 19:08:22 -04:00
fi
2024-08-03 15:37:28 -04:00
if $optExcludeLost; then
syncExcludes+=("lost+found")
2023-05-23 19:08:22 -04:00
fi
2024-08-03 15:37:28 -04:00
syncExcludes+=("${optUserExclude[@]}")
syncIncludes+=("${optUserInclude[@]}")
# Generic rsync options:
rsyncOptions=(
2023-05-23 19:08:22 -04:00
-avxHAXS
--numeric-ids
2023-05-23 19:27:19 -04:00
"--info=progress2"
2024-08-03 15:37:28 -04:00
)
rsyncOptions+=("${rsyncArgs[@]}")
2023-05-23 19:08:22 -04:00
# if not in quiet mode, this message will be displayed:
2024-08-03 15:37:28 -04:00
if ! $optQuiet; then
2023-05-23 19:08:22 -04:00
echo "Are you sure that you want to copy system files located under"
2024-08-03 15:37:28 -04:00
echo "$sourcePath"
2023-05-23 19:08:22 -04:00
echo "to the following directory"
2024-08-03 15:37:28 -04:00
echo "$targetPath"
2023-05-23 19:08:22 -04:00
echo
echo "WARNING: since all data is copied by default the user should exclude all"
echo "security- or privacy-related files and directories, which are not"
echo "already excluded, manually per cmdline."
2024-08-03 15:37:28 -04:00
echo "example: \$ $(basename "$0") -s -e \"/etc/ssh/ssh_host*\" <destination>"
2023-05-23 19:08:22 -04:00
echo
echo "COMMAND LINE PREVIEW:"
2024-08-03 15:37:28 -04:00
echo 'rsync' "${rsyncOptions[@]}" "${syncIncludes[@]/#/--include=}" "${syncExcludes[@]/#/--exclude=}" "$sourcePath" "$targetPath"
2023-05-23 19:08:22 -04:00
echo
echo -n 'Type "yes" to continue or anything else to quit: '
2024-08-03 15:37:28 -04:00
read -r promptAgree
if [[ "${promptAgree,,}" == "yes" ]]; then
optQuiet=true
fi
2023-05-23 19:08:22 -04:00
fi
# start stage4 creation:
2024-08-03 15:37:28 -04:00
if $optQuiet; then
#echo "Would've worked"
rsync "${rsyncOptions[@]}" "${syncExcludes[@]/#/--exclude=}" "$sourcePath" "$targetPath"
2023-05-23 19:08:22 -04:00
fi