Hello,
What I achieve: securely backuping my NAS to an encrypted drive pluged into HassOS on Raspberry in another location.
As stated in Rsync install it is not possible to install software on the Hass OS host. And the host does not have rsync, nor cryptsetup/Luks.
But using Docker it is possible to do it.
How to do it:
- Activate low level SSH following instruction from https://blog.leandrotoledo.org/how-to-enable-ssh-on-home-assistant-os/ or with addon “SSH & Web Terminal” but not “Terminal & SSH” !
- Help on encrypt a drive : https://www.cyberciti.biz/security/howto-linux-hard-disk-encryption-with-luks-cryptsetup-command/
Command used on Hass OS, only one time:
- Create a new container
hdd
from Ubuntudocker run -it -p 22:1979 -v /mnt/data/backup:/mnt/backup --name hdd --privileged --cap-add=ALL ubuntu:20.04
- Update packages list:
apt update
- Install tools:
apt install -y cryptsetup rsync openssh-server
- Copy/paste your ssh public key in /root/.ssh/authorized_keys
- Set PermitRootLogin to yes in /etc/ssh/sshd_config
- Exit the container
exit
- Save your docker container to a docker image:
docker commit hdd hdd
Command to run before a backup:
- Run your new image
docker run -td -v /mnt/data/backup:/mnt/backup --name hdd --privileged --cap-add=ALL hdd
- Decrypt drive
docker exec -it hdd cryptsetup luksOpen /dev/sda1 hdd
- Mount drive
docker exec hdd mount -t ext4 /dev/mapper/hdd /mnt/backup
- Find the container IP:
docker inspect hdd | grep 'IPAddress'
To do the backup copy:
rsync -azv -e 'ssh -A -J your_user@ip_of_hass_os' /path_to_local_files_to_backup/ root@ip_of_docker_container:/mnt/backup
When your copy is done, close the drive to keep it secure:
docker exec hdd umount /mnt/backup
docker exec hdd cryptsetup luksClose hdd
It works but it certainly could be improve. I think we could rsync directly into the docker container with exposing the ssh port of the container. So no need for IP of container and .
Let me know if you have ideas or questions.
Notes on security :
- The communication is encrypted through SSH and the partition is encrypted by cryptsetup. So if someone stole the drive, he can’t read the data. BUT when you do the copy, someone with access to HassOS could access your data unencrypted ! So do this only on a HassOS installation you trust.
- I could put an image on Docker Hub to simplify this, but I discourage you to use such a thing to manipulate your data has you don’t know what the image will do.