Wikijunior:Raspberry Pi/Raspberry Pi Updates & Backups Tutorial
by Andrew Oakley - Public Domain Jan 2018
www.cotswoldjam.org
Updating your system
editYou will need a working internet connection. Select the Raspberry menu – Accessories – Terminal. A black box should appear.
sudo apt update
sudo apt upgrade
These commands will get the list of updated software and then upgrade. You may be asked to type Y to confirm. This can take a very long time, especially on a Pi 1 or Zero!
sudo apt autoremove
sudo apt clean
autoremove
will uninstall any software which has been superseded by new software. clean
removes any temporary installation files, to free up space on your SD card.
Restart your system with the Raspberry Pi menu – Shutdown – Reboot.
Installing the zip and unzip tools
editsudo apt update
sudo apt install zip unzip
Connect a USB memory stick and find out what it's called on the system
editInsert the USB memory stick into a USB port. We need to find out what the USB stick is called, and where it is on the system.
The memory stick may open automatically in File Manager. In this case, you can read the name from the address bar. In this example it is BLUE64GB.
Alternatively, you can find the name by clicking the "Eject" symbol in the top right of the desktop.
In this example, the name is BLUE64GB. It also shows the brand name (SanDisk Ultra Fit) but we don't care about that.
Don't actually eject the USB stick yet! We're going to put some backups on it. There's another way to find the name and location of your USB stick; find out about the df
command.
In our instructions, we are using the name USBNAME so you will need to change this to the actual name of your USB stick. Make sure your get capitals and lowercase correct. |
Back up all your files to a USB stick
editThese commands back up all files in the /home/pi
directory. This includes all documents, images, Scratch and Python source code files and Minecraft worlds that you have made, but it doesn't include programs that have been installed on the system.
cd /home
zip -r /media/pi/USBNAME/pibackup-20180127.zip pi/ -x 'pi/.cache/*'
cd
Note how we used cd /home
to move from the /home/pi
directory to the /home
directory.
This allows us to treat the entire pi directory as a subfolder that we can back up.
We also use the -x
option to ignore the .cache
subfolder, as this can contain a lot of large temporary web browser files.
It's a good idea to use the date in the name of your backups, so you can quickly find a backup from a particular day. We used 2018 01 27 for 27th January 2018. Putting the year and month first will make it easier to find in a list that's sorted in file name order.
Once we've finished, we use the cd
command on its own, to return to our home/pi
directory. We could have done cd /home/pi
which would have done the same thing.
Restore all your files from a USB stick
editInsert the USB memory stick into a USB port. Find the .zip file you want to restore using File Manager.
Using the Terminal, restore the files. Note this will overwrite (-o
) existing files with the older versions.
cd /home
unzip -o /media/pi/USBNAME/pibackup-20180127.zip
cd
Back up just one folder
editcd
zip -r /media/pi/USBNAME/Documents-20180127.zip Documents/
Note that folders beginning with a full stop (.
) are sometimes hidden from file listings, but you can still back them up. For example, the Minecraft folder with all your Minecraft worlds:
zip -r /media/pi/USBNAME/minecraft-20180127.zip .minecraft/
Restore just one folder
editcd
unzip /media/pi/USBNAME/Documents-20180127.zip
We didn't use -o
, so it will ask before overwriting any existing files.
List all the files in a large .zip backup archive and pick just one file or folder to restore
editAlthough you can use the unzip
command to extract specific individual files, it's easier to use the Xarchiver program. Use File Manager (Raspberry menu – Accessories – File Manager) to open the USB stick and find your .zip file.
Double-click on your .zip file and the Xarchiver program will start.
You can now double-click to open any folder inside the .zip file. Right-click any file or folder and select Extract to recover it.
Click the folder icon beneath "Extract to" to pick a folder to copy the recovered file to. You probably want /home/pi
which you can either type in, or click the pi
folder from the list of folders.
If you're just recovering one file, untick "Extract files with full path".
Click the Extract button to recover the file.
Use File Manager (Raspberry menu – Accessories – File Manager) to find the files you've extracted.
Safely remove the USB memory stick
editDon't remove the USB stick until you have ejected it from the desktop – your computer might still be halfway through writing a file. |
Click the "Eject" symbol at the top-right of the desktop, then click the smaller eject symbol to the right of your listed USB memory stick.
You should see the message "It is now safe to remove your device". You can remove the USB memory stick from the Raspberry Pi now. You can also eject it from the command line – learn about the umount
command.
Don't keep your backups near your Pi. If your Pi gets lost, stolen, destroyed by fire or eaten by zombies, you don't want your backups to go missing too!
Grandparents, Aunties and Uncles' houses are a really, really good place to keep your backups. Consider having two memory sticks – one to keep at a relative's house, and one to keep in your house. Swap them over every time you visit!
Migrating to a new release of Raspbian
editEvery two or three years, a completely new release of Raspbian happens. For example, in 2017 we moved from Raspbian Jessie to Raspbian Stretch.
Raspbian is the operating system that runs on most Raspberry Pis. It handles the desktop, most of the programs, the command line and much more. Raspbian is based on Debian GNU/Linux, which names releases after characters from the Toy Story films. Debian is short for "Debra and Ian"; Ian Murdoch created it, and Debra was his girlfriend.
Whilst it is technically possible to upgrade an existing SD card to a new release, it's prone to mistakes, so our advice is to:
- Back up all your files to a USB stick, as we showed previously in this tutorial
- Download the new release image, and burn it to a new SD card, as per the instructions here: https://www.raspberrypi.org/documentation/installation/installing-images/README.md
- Restore your files from your USB stick backup, as per this tutorial.
Other top tips
editIf you have both a Pi Zero or Pi 1, and a Pi 2 or a Pi 3, do all your updates on the Pi 2 or Pi 3. It will be much, much faster. You can usually swap the memory cards between them (unless your Pi 1 has a really, really old version of Raspian – in that case, it won't work on the Pi 2/3 but it won't cause any harm, just put it back).
Using an Ethernet network cable directly plugged into your broadband router can often make downloading updates much faster than WiFi. You can buy a simple CAT5e Ethernet cable from Poundland and other bargain shops, just don't pay more than £5.
Files
editThe original PDF for this tutorial is available on Wikicommons: Cjam-updates-backups.pdf