Here’s a little trick I use to back up my Raspberry Pi without having to shutdown my Home Assistant Raspberry Pi, ejecting drives, or otherwise messing around with physical drives. Once you do the initial preparation, the backups are painless and are essentially done by one command. You can even use a chron job to schedule them. All while your Pi sits in your closet. As a bonus, you will end up with a virtual hard drive (VHD) that you can use to easily image your SD card and/or hard drive again and even attach to your Windows machine to browse if you have the proper drives.
In short the process is as follows: On my Windows machine, in Virtualbox, I create a machine with Debian Linux installed . Then, using Windows’ Disk Manager, I create a dynamically sized Virtual Hard Drive (VHD) of the size I need to backup my Pi’s hard drive, attach that to the Virtualbox machine, and rsync the Pi to the VHD over SSH.
Below are detailed instructions – the first few steps are in my prior post “Backing Up an SD Card to VHD” but I will repeat them here for simplicity.
This step is somewhat lengthy but will only need to be done once. Here we are creating the Virtual Hard Drive which will be the backup drive, attaching it to the Virtualbox machine, and formatting it:
Click “Ok” to create the drive.
Disk Manager will then create and attach the VHD to the list of drives shown. For instance in my case, the VHD attaches as Disk 9. The virtual drive will show as as uninitialized with a red X since we have not formatted it.
sudo fdisk -l
You should see something like this:
Note the device number of the disk of the VHD you have created. Mine is the 149 GB drive and is shown in the listing above as /dev/sdb.
sudo mkfs -t ext4 /dev/sdb1 [<--- Make sure this is correct as all data will be wiped!!!] sudo mkdir /mnt/backup-pi sudo mount /dev/sb1 /mnt/backup-pi
Confirm the mount:
mount | grep backup-pi
If it mounted correctly, output should look something like this:
/dev/sdb1 on /mnt/backup-pi type ext4 (rw,relatime
That's it for the preparation of the backup drive. You won't have to do the above steps again.
Enter the following to drop to root:
sudo -i [to drop to root]
rsync -avz --delete --exclude '/cache/' --exclude '/dev/' --exclude "**/tmp/" --exclude "/proc/*" --exclude '/swapfile' --exclude '/etc/udev/rules.d/' --exclude '/lost+found/' --exclude '/sys/' --exclude '/mnt' --exclude '/media/' --exclude '/var/cache' root@[Ip to Raspberry Pi]:/ /mnt/backup-pi/
This will do a one way sync from your Pi duplicating the drive more or less exactly onto your VHD, pulling the files to your backup drive on your virtual machine. Note that anything on your backup drive which does not existing on your Pi will be wiped out (i.e., this is a one way mirror sync). This command also excludes various directories that are not needed in a backup and will slow the backup down.
Note that for a full backup, your Pi will need to permit SSH root login. If you only wish to backup files of a particular user, then you can adjust the path and modify the rsync command to login in as a non-root user.
Avoiding Browser Plugins with KeePassXC KeePassXC is a popular free open source password manager. As…
If you've had a Gmail account for years, like I have, at some point you…
I try to keep my email inbox fairly clean but I do subscribe to some…
I was recently faced with the dreaded prospect of re-installing Windows 10. While the standard…
Pi-Hole and Upstream DNS Providers Pi-hole is an excellent whole-home, self-hosted ad blocker and DNS…
I just released a new open source project on github called Uproot. What is Uproot…