Tuesday 29 January 2019

Simple backup of a Raspberry Pi


Back up SD card data to USB drive

The Raspberry Pi generally uses an SD (or microSD) card as its main storage. This is a cheap and generally reliable storage medium, but it is vulnerable to problems if the power is interrupted while data is being written.

If the power is interrupted (by the cat pulling the power lead out or you thinking it is the mobile 'phone's charger etc.) there is always the possibility that the SD card may become corrupted and all the data is lost.

Of course, you should be running a backup process on any system.

Back up home directory


To back up your home directory to a mounted USB drive, use the following command lines. You will need to know the name of the USB drive (have a look in /media.pi for the USB drive), replace the "name_of_usbdrive" with the actual drive name. You might want to replace "mypi" with the network name of the machine if you have a number of different systems.

First change directory to the /home directory (off of root).

cd /home/
Note space between change directory and directory path.

sudo tar czf /media/pi/name_of_usbdrive/backups/mypi_$(date +%Y%m%dT%H%M)home.tar.gz pi

Once that is running, go off and make a beverage of choice, it will take a while, and using the machine will upset the backup (as any use may cause a file change).

Note

One thing to be aware of, this will back up the contents of the trash bin. 
This might cause issues as the zip process falls over at about 4GB, and deleting files just puts them in the bin.

It might be worth emptying the bin occasionally.

You might also not want some deleted items to be backed up ^___^.