2022-10-02 15:02:23 +00:00
|
|
|
#!/usr/bin/env bash
|
2023-02-07 17:15:22 +00:00
|
|
|
|
2024-01-01 17:13:05 +00:00
|
|
|
# Copyright (c) 2021-2024 tteck
|
2023-02-07 17:15:22 +00:00
|
|
|
# Author: tteck (tteckster)
|
|
|
|
# License: MIT
|
|
|
|
# https://github.com/tteck/Proxmox/raw/main/LICENSE
|
|
|
|
|
2022-12-18 11:29:56 +00:00
|
|
|
clear
|
|
|
|
if ! command -v pveversion >/dev/null 2>&1; then echo -e "⚠️ Run from the Proxmox Shell"; exit; fi
|
2022-10-02 15:02:23 +00:00
|
|
|
while true; do
|
2022-10-30 01:04:45 +00:00
|
|
|
read -p "Use to copy all data from a Home Assistant Core LXC to a Home Assistant Container LXC. Proceed(y/n)?" yn
|
|
|
|
case $yn in
|
|
|
|
[Yy]*) break ;;
|
|
|
|
[Nn]*) exit ;;
|
|
|
|
*) echo "Please answer yes or no." ;;
|
|
|
|
esac
|
2022-10-02 15:02:23 +00:00
|
|
|
done
|
|
|
|
set -o errexit
|
|
|
|
set -o errtrace
|
|
|
|
set -o nounset
|
|
|
|
set -o pipefail
|
|
|
|
shopt -s expand_aliases
|
|
|
|
alias die='EXIT=$? LINE=$LINENO error_exit'
|
|
|
|
trap die ERR
|
|
|
|
trap cleanup EXIT
|
|
|
|
|
|
|
|
function error_exit() {
|
|
|
|
trap - ERR
|
|
|
|
local DEFAULT='Unknown failure occured.'
|
|
|
|
local REASON="\e[97m${1:-$DEFAULT}\e[39m"
|
|
|
|
local FLAG="\e[91m[ERROR] \e[93m$EXIT@$LINE"
|
|
|
|
msg "$FLAG $REASON"
|
|
|
|
exit $EXIT
|
|
|
|
}
|
|
|
|
function warn() {
|
|
|
|
local REASON="\e[97m$1\e[39m"
|
|
|
|
local FLAG="\e[93m[WARNING]\e[39m"
|
|
|
|
msg "$FLAG $REASON"
|
|
|
|
}
|
|
|
|
function info() {
|
|
|
|
local REASON="$1"
|
|
|
|
local FLAG="\e[36m[INFO]\e[39m"
|
|
|
|
msg "$FLAG $REASON"
|
|
|
|
}
|
|
|
|
function msg() {
|
|
|
|
local TEXT="$1"
|
|
|
|
echo -e "$TEXT"
|
|
|
|
}
|
|
|
|
function cleanup() {
|
|
|
|
[ -d "${CTID_FROM_PATH:-}" ] && pct unmount $CTID_FROM
|
|
|
|
[ -d "${CTID_TO_PATH:-}" ] && pct unmount $CTID_TO
|
|
|
|
popd >/dev/null
|
|
|
|
rm -rf $TEMP_DIR
|
|
|
|
}
|
|
|
|
TEMP_DIR=$(mktemp -d)
|
|
|
|
pushd $TEMP_DIR >/dev/null
|
|
|
|
|
|
|
|
TITLE="Home Assistant LXC Data Copy"
|
|
|
|
while read -r line; do
|
|
|
|
TAG=$(echo "$line" | awk '{print $1}')
|
|
|
|
ITEM=$(echo "$line" | awk '{print substr($0,36)}')
|
|
|
|
OFFSET=2
|
|
|
|
if [[ $((${#ITEM} + $OFFSET)) -gt ${MSG_MAX_LENGTH:-} ]]; then
|
|
|
|
MSG_MAX_LENGTH=$((${#ITEM} + $OFFSET))
|
|
|
|
fi
|
2022-10-30 01:04:45 +00:00
|
|
|
CTID_MENU+=("$TAG" "$ITEM " "OFF")
|
2022-10-02 15:02:23 +00:00
|
|
|
done < <(pct list | awk 'NR>1')
|
|
|
|
while [ -z "${CTID_FROM:+x}" ]; do
|
2023-09-09 09:13:17 +00:00
|
|
|
CTID_FROM=$(whiptail --backtitle "Proxmox VE Helper Scripts" --title "$TITLE" --radiolist \
|
2022-10-30 01:04:45 +00:00
|
|
|
"\nWhich HA Core LXC would you like to copy FROM?\n" \
|
|
|
|
16 $(($MSG_MAX_LENGTH + 23)) 6 \
|
|
|
|
"${CTID_MENU[@]}" 3>&1 1>&2 2>&3) || exit
|
2022-10-02 15:02:23 +00:00
|
|
|
done
|
|
|
|
while [ -z "${CTID_TO:+x}" ]; do
|
2023-09-09 09:13:17 +00:00
|
|
|
CTID_TO=$(whiptail --backtitle "Proxmox VE Helper Scripts" --title "$TITLE" --radiolist \
|
2022-10-30 01:04:45 +00:00
|
|
|
"\nWhich HA Container LXC would you like to copy TO?\n" \
|
|
|
|
16 $(($MSG_MAX_LENGTH + 23)) 6 \
|
|
|
|
"${CTID_MENU[@]}" 3>&1 1>&2 2>&3) || exit
|
2022-10-02 15:02:23 +00:00
|
|
|
done
|
|
|
|
for i in ${!CTID_MENU[@]}; do
|
2022-10-30 01:04:45 +00:00
|
|
|
[ "${CTID_MENU[$i]}" == "$CTID_FROM" ] &&
|
|
|
|
CTID_FROM_HOSTNAME=$(sed 's/[[:space:]]*$//' <<<${CTID_MENU[$i + 1]})
|
|
|
|
[ "${CTID_MENU[$i]}" == "$CTID_TO" ] &&
|
|
|
|
CTID_TO_HOSTNAME=$(sed 's/[[:space:]]*$//' <<<${CTID_MENU[$i + 1]})
|
2022-10-02 15:02:23 +00:00
|
|
|
done
|
2023-09-09 09:13:17 +00:00
|
|
|
whiptail --backtitle "Proxmox VE Helper Scripts" --defaultno --title "$TITLE" --yesno \
|
2022-10-30 01:04:45 +00:00
|
|
|
"Are you sure you want to copy data between the following LXCs?
|
2022-10-02 15:02:23 +00:00
|
|
|
$CTID_FROM (${CTID_FROM_HOSTNAME}) -> $CTID_TO (${CTID_TO_HOSTNAME})
|
|
|
|
Version: 2022.10.02" 13 50 || exit
|
|
|
|
info "Home Assistant Data from '$CTID_FROM' to '$CTID_TO'"
|
|
|
|
if [ $(pct status $CTID_TO | sed 's/.* //') == 'running' ]; then
|
|
|
|
msg "Stopping '$CTID_TO'..."
|
|
|
|
pct stop $CTID_TO
|
|
|
|
fi
|
|
|
|
msg "Mounting Container Disks..."
|
|
|
|
DOCKER_PATH=/var/lib/docker/volumes/hass_config/_data
|
|
|
|
CORE_PATH=/root/.homeassistant
|
2022-10-30 01:04:45 +00:00
|
|
|
CTID_FROM_PATH=$(pct mount $CTID_FROM | sed -n "s/.*'\(.*\)'/\1/p") ||
|
2022-10-02 15:02:23 +00:00
|
|
|
die "There was a problem mounting the root disk of LXC '${CTID_FROM}'."
|
2022-10-30 01:04:45 +00:00
|
|
|
[ -d "${CTID_FROM_PATH}${CORE_PATH}" ] ||
|
2022-10-02 15:02:23 +00:00
|
|
|
die "Home Assistant directories in '$CTID_FROM' not found."
|
2022-10-30 01:04:45 +00:00
|
|
|
CTID_TO_PATH=$(pct mount $CTID_TO | sed -n "s/.*'\(.*\)'/\1/p") ||
|
2022-10-02 15:02:23 +00:00
|
|
|
die "There was a problem mounting the root disk of LXC '${CTID_TO}'."
|
2022-10-30 01:04:45 +00:00
|
|
|
[ -d "${CTID_TO_PATH}${DOCKER_PATH}" ] ||
|
2022-10-02 15:02:23 +00:00
|
|
|
die "Home Assistant directories in '$CTID_TO' not found."
|
|
|
|
|
|
|
|
msg "Copying Data..."
|
|
|
|
RSYNC_OPTIONS=(
|
|
|
|
--archive
|
|
|
|
--hard-links
|
|
|
|
--sparse
|
|
|
|
--xattrs
|
|
|
|
--no-inc-recursive
|
|
|
|
--info=progress2
|
|
|
|
)
|
|
|
|
msg "<======== Docker Data ========>"
|
2022-10-02 15:32:20 +00:00
|
|
|
rsync ${RSYNC_OPTIONS[*]} ${CTID_FROM_PATH}${CORE_PATH} ${CTID_TO_PATH}${DOCKER_PATH}
|
2022-10-02 15:02:23 +00:00
|
|
|
echo -en "\e[1A\e[0K\e[1A\e[0K"
|
|
|
|
|
|
|
|
info "Successfully Transferred Data."
|
|
|
|
|
|
|
|
# Use to copy all data from a Home Assistant Core LXC to a Home Assistant Container LXC
|
|
|
|
# run from the Proxmox Shell
|
2023-09-05 15:39:29 +00:00
|
|
|
# bash -c "$(wget -qLO - https://raw.githubusercontent.com/tteck/Proxmox/main/misc/copy-data/home-assistant-core-copy-data-home-assistant-container.sh)"
|