SpiffberryPi Backup | Ultimate Breakdown – How-To *

MAC BACKUP

Backup Raspberry Pi SD Card on macOS — the 2019 Simple Way to Clone (if your SD card is small enough)

 

You want to backup an SD card you are using to boot up a Raspberry Pi. The best way is to clone it. You can do it in an easy and fast way with Disk Utility, an app shipped with macOS.

One

Insert the SD card in you Mac’s slot. Then open the app Disk Utility.

 

img

Two

Select View > Show All Devices.

 

img

Three

Select the SD card from in the list of volumes on the left. The icon and the information will help to understand if you’ve selected the right one.

img

It’s important that you select the parent device, not the partition inside. Like in the example.

Then right-click on it and select Image from “NAME”.

Four

Select the DVD/CD master as Format. Click Save and you’re done. It will create an image file using with cdr extension.

img

Restore a backup

First rename the extension of the image file previously created, from .cdr to .iso. Then download and open Etcher to write the image onto an SD card.

 

Mac backup – Version 2

https://pimylifeup.com/backup-raspberry-pi/

In this guide, we will be showing you various ways on how to backup your Raspberry Pi and also how to restore it.

Backup your Raspberry Pi SD Card

Backing up is a crucial task that you should often be doing, especially if you make a lot of changes or are storing data on it.

One thing to note is that some backup methods are going to be way more thorough the others, for instance backing up your Raspberry Pi SD Card image is going to be more reliable than just backing up all the files to a USB device since the image is a replication of all partitions on the SD Card.

We will be exploring two different methods of backing up your Raspberry Pi in this guide, the two different methods that we will be showing you how to do is the following:

  • Backing up your Raspberry Pi to a computer using an SD Card Reader
  • Backup the Raspberry Pi to a USB device

Personally, we recommend backup to a computer as it is the most robust method. Mainly since the Raspberry Pi doesn’t have the best USB speeds or network speeds, so creating backups can take considerable time and put a fair bit of stress on the Raspberry Pi’s limited resources.

As a bonus, the backup we make using a tool like win32diskimager is a 1 to 1 copy of the SD Card meaning every byte should be the same.

Backing up the Raspberry Pi is an important step in making sure you don’t lose any data when you go to make big changes to your system. This backup process is important if you have stuff that you would prefer not to lose if you’re looking at doing a complicated Pi project, upgrade or something else to the operating system.

Backing up your Raspberry Pi SD Card

To start off, we are first going to show you how to backup your Raspberry Pi SD Card as an image. To do this tutorial, you will need to have an SD Card reader handy.

To begin this tutorial, please first turn off your Raspberry Pi by running the following command on your Pi’s terminal:

sudo shutdown now

Once it has shut down, disconnect the power and remove your SD Card. Place the SD Card into your SD Card reader and proceed onwards with this section of the guide.

This section of the guide will be split into three parts, one for Windows, one for Mac OS X and one for Linux based systems.

1. With your SD Card inserted into a card reader on your Mac, we can begin the process of making a full image backup of your Raspberry Pi.

Unlike Windows, we can easily use the terminal to do this.

To proceed with this tutorial, start off by opening the Terminal application.

2. With the Terminal application now open on your Mac device, we need to utilize the following command. This command will display all available disks on your device.

diskutil list

Within this list, look for your SD Card by looking for a disk that is about the size of your SD Card.

For instance, with a 16gb SD Card, you should be looking at a “_partition_scheme” of about 16gb. You will also notice that there is likely a partition called boot.

Once you have found your SD Card in this list, take notice of the mount location. For instance, my own SD Card was under “/dev/disk1“.

3. Now still within the terminal on your Mac, we need to utilize the following command.

This command will basically create a copy of your SD Cards image, and save it to your home directory as “PiSDBackup.dmg” (The file format being a disc image)

Make sure you swap out “/dev/disk1” with whatever you found when using the “diskutil list” command.

sudo dd if=/dev/disk1 of=~/PiSDBackup.dmg

This command can take some time to complete as it requires reading the entire SD Card to the disk.

The command also provides no feedback on how far along it is, so please be patient and wait for the prompt to enter another command to reappear before removing your SD Card from your Mac.

Restoring your Raspberry Pi Backup on OS X

1. Now that you need to restore your Raspberry Pi to its backup we need to use the “diskutil list” command to find our SD Card. Remember as before, take note of partition sizes to find your SD Card.

diskutil list

2. Now before we can write to the SD Card, we will need to unmount it. The reason for this is that OSX will attempt to write to it at the same time, unmounting the SD Card prevents this from happening.

Run the following command on your Mac device to unmount the card, again making sure that you replace “/dev/disk1” with the one you found using the “diskutil list” command.

diskutil unmountDisk /dev/disk1

3. Finally, we can now write the image back to the SD Card, and please be prepared for this to take some time as it involves rewriting the entire SD Card.

Remember to change out “/dev/disk1” with the mount location you grabbed using the diskutil list command.

sudo dd if=~/PiSDBackup.dmg of=/dev/disk1

Like reading the SD Card to a disk image file, the process of writing the image also takes a long time.

As an added note, the dd tool doesn’t show any writing progress so please be patient and wait till the enter command prompt reappears.

4. Once the writing process has completed we can now eject the SD Card from the Mac so we can continue using our Raspberry Pi Backup.

To eject the SD Card, we will need to utilize the command below.

sudo diskutil eject /dev/rdisk3

Your Raspberry Pi’s SD Card should now be in the same state as when you made the original backup.

 


https://raspberrytips.com/backup-raspberry-pi/

The Ultimate Guide to Backup and Restore your Raspberry Pi

Written by Patrick Fromagetin How-To Tutorials

 

If like me, you make many changes on your Raspberry Pi, it might be a good idea to create backups. And what’s even better is to make sure backups are recoverable 🙂 Yes, but then how to backup a Raspberry Pi and recover it? That what we’ll see in this complete guide.

The best way to create a backup of a Raspberry Pi is to use a tool like rsync, and copy the important files to another location on the network. It’s also possible to create entire backups of the SD card, to make sure everything is secured.

As often, there is not only one way to do this. It highly depends on how you use your Raspberry Pi, and what is available on your network to store the backups. It this tutorial, I will guide you through several solutions:

  • Back up only important files (configuration, documents, MySQL databases)
  • Make a complete image of the SD card on another computer
  • Or do a full backup of a Raspberry Pi while running

We will now see in detail how to do each one.

By the way, if you don’t have a budget to afford a course for now (if you just got your Raspberry Pi stuff, I completely understand), you can join Skillshare for a free 14-day trial. It includes courses from the best teachers in many categories, including IT (Linux, programming, …) and Raspberry Pi. I highly recommend getting this offer now so that you can already learn a bunch of things in the next fortnight. I’m a fan of their services personally 🙂

Backup only the important files

The first method you can use if you have a simple installation is to backup only the needed files.

For example, if you use your Raspberry Pi for a security camera, once you back up the configuration file, it’s ok, you don’t need more. So we’ll see step by step :

  • How to create a script to do that
  • How to schedule it if the files are modified
  • How to send the file on another computer (in the script or manually)
  • And finally, how to restore the files

Script

Before starting to create the script, you have to identify the files you need to save. In my script I’ll use a file and a folder that you can adapt to your needs:

  • /etc/app1/file1.conf
  • /etc/app2

Then we need to create a backup folder to store the files: mkdir /home/pi/backups

Use nano to create the script: nano /usr/local/bin/backup.sh

And finally, the first version of a simple script could look like this:

#!/bin/bash
/bin/cp /etc/app1/file1.conf /home/pi/backups
/bin/cp /etc/app2 /home/pi/backups/ -r

As you can see it’s a basic script, that will overwrite the old backup each time. So now we’ll improve this script on several points.

Use variables

The first good practice we’ll use to improve the scripts is to add variables.

For example, you have 200 files to backups to /home/pi/backups and tomorrow you want to save them in /media/nas/pi/. You’ll have to edit 200 lines in your script to change the destination folder.

A better way to do this is to create a variable at the beginning with the destination folder path and use it for each line. I’ll also do the same for the “cp” command, so if you want to change it to use rsync or another command, you’ll have only one line to edit.

Here’s a better script:

#!/bin/bash
DEST_FOLDER='/home/pi/backups/'
BACKUP_CMD='/bin/cp'

$BACKUP_CMD /etc/app1/file1.conf $DEST_FOLDER
$BACKUP_CMD /etc/app2 $DEST_FOLDER -r

The result will be the same, but it will be easier to update

Compress files

Most of the time, we use compression for backup or at least archive files. I’ll use tar to archive all files in one and gzip to compress this file.

Here is the new script:

#!/bin/bash

DEST_FOLDER='/home/pi/backups/'
DEST_FILE='backup.tar'
BACKUP_CMD='/bin/tar -rvf'

/bin/rm $DEST_FOLDER/$DEST_FILE.gz
$BACKUP_CMD $DEST_FOLDER/$DEST_FILE /etc/app1/file1.conf 
$BACKUP_CMD $DEST_FOLDER/$DEST_FILE /etc/app2
/bin/gzip $DEST_FOLDER/$DEST_FILE

I add a new variable DEST_FILE to store the file name of the backup. “tar -rvf” allows you to append several files to one tar file. “gzip” allows you to compress the whole tar file.

Stop overwriting files

As you can see in the last script, we delete the previous backup each time. It’s not a good thing to do. If there is an issue with the backup, you’ll not be able to get an older version.

The good practice is to name the backup file with the current date time, like this:

#!/bin/bash

DEST_FOLDER='/home/pi/backups/'
DEST_FILE="backup-$(date +'%F_%R').tar"
BACKUP_CMD='/bin/tar -rvf'
$BACKUP_CMD $DEST_FOLDER/$DEST_FILE /etc/app1/file1.conf 
$BACKUP_CMD $DEST_FOLDER/$DEST_FILE /etc/app2
/bin/gzip $DEST_FOLDER/$DEST_FILE

Nothing changed except we add the date in the DEST_FILE variable, so that we stop deleting the previous backup.

Each time the script run, it will now create a new file and keep all the previous versions.

Schedule backups

Once we have created our script, following the previous steps, most of the work is done.

We only need to schedule our script so that it starts every day automatically. For that, we will use the crontab:

  • Open the user’s crontab: crontab -e
  • Paste this line at the end of the file: 0 0 * * * /usr/local/bin/backup.sh This cron will run your backup script each day at midnight, but you can change it if you want.
  • Save and quit (CTRL+O, Enter, CTRL+X)

If you back up files with privilege access needed, don’t forget to schedule the script in the root crontab (sudo crontab -e).

If you are not comfortable with this, do not hesitate to read my tutorial on scheduling tasks on Raspberry Pi.

Delete old files

As you can see, each day a new file will be created, and we never delete it. What you can do to free disk space is to delete files older than eight days (or more if needed).

To configure this, you can add this command at the end of the script: /usr/bin/find $DEST_FOLDER -mtime +8 -delete

By adding this, you’ll have at anytime only eight backup files of history in your destination folder. Feel free to change the number of files you want to keep.

It’s always a good idea to keep several days of history. You can’t be sure to see the issue with your files on the first day.

Send backup over the network

We now have a backup with a few days of history on the SD card, but it’s not enough. If tomorrow you SD card doesn’t work anymore, it will not help to have backup files on it. We need to put the files safe on another computer to avoid this kind of problems.

To a Linux computer or a NAS

If you have another computer on Linux (or a NAS), you can transfer backup files on it to keep them safe. This method will work for macOS too.

Personally, I’m using a 4 bay Synology NAS, which is great to save high volumes of critical data. It’s a bit expensive, but you have redundancy and security that is difficult to get on a standard computer (and also many tools included). For less important data, you can also turn another Raspberry Pi into a NAS and sync your files to it (check my tutorial if you want to try).

Manually

The first way to do this is manually. If your files never change or if they are not so critical, you can do this once a week or a month to be safe.

To transfer the files from the Raspberry Pi to the Linux computer, we’ll use rsync. So you need to install it on both machines: sudo apt-get install rsync

Then create a backup folder on your computer: mkdir /home/[USER]/backups_pi

Then you have two ways to transfer files:

  • From your computer: rsync -auzr pi@[RASPBERRY_IP]:/home/pi/backups/* /home/[USER]/backups_pi/
  • From the Raspberry Pi: rsync -auzr /home/pi/backups/* [USER]@[COMPUTER_IP]:/home/[USER]/backups_pi/

Don’t forget to replace the variables with the Raspberry Pi IP address and your username. And feel free to delete old files before or after the transfer on your computer.

Automatically

As when saving on the Raspberry Pi, it’s always a good idea to automate this kind of task. The problem you might have seen is that a password is required when the transfer starts. To avoid this, you must first make an exchange of SSH keys between the two machines.

For this step, I’ll assume it’s your computer that triggers the transfer. If you prefer to transfer in the other direction, you have to do the opposite (generate the keys on the Raspberry Pi and allow them on the PC).

Key exchange

  • Firstly start by generating a key on your computer (if you have already one, it’s ok, go to next step): ssh-keygen -t rsa Press Enter to each question, leave the passphrase empty.
  • Then transfer the public key on the Raspberry Pi: rsync ~/.ssh/id_rsa.pub pi@[RASPBERRY_IP]:/home/pi/ Last time you need to enter your password.
  • On the Raspberry Pi, add the public key to the allowed keys: cat ~/id_rsa.pub >> ~/.ssh/authorized_keys
  • Try to connect in ssh now. It shouldn’t ask you a password: ssh pi@[RASPBERRY_IP]

Script example

So, now you should be able to put the rsync command in a script that will not ask for a password. The script should look like this:

#!/bin/bash
rsync -auzr pi@[RASPBERRY_IP]:/home/pi/backups/* /home/[USER]/backups_pi/
find /home/[USER]/backups_pi -mtime +8 -delete

Then schedule the script by adding it in your crontab as explain previously.

To Windows computer

If your computer is on Windows, you can also send your backup to it, manually or automatically.

Manually

To manually transfer files from the Raspberry Pi to your Windows computer, the easiest way is to install WinSCP on Windows. It’s a free software to transfer files with SSH:

  • Once installed, start WinSCP
  • Add a new site with the Raspberry Pi information winscp settings
  • Click Save, and connect to this new site
  • On the right, you’ll see the Raspberry Pi files, and on the left, your computer files. You can now transfer files from one to the other with a drag & drop. Go to /home/pi/backups on the Raspberry Pi and download one or all files. Then you can remove the older files on your computer to keep only the last eight days for example.
Automatically

There is no way to schedule a transfer with WinSCP. And it’s not easy to install SSH on Windows (to use it in a script at least). So we have to find another method. What I suggest you is to share a folder on Windows and access it from the Raspberry Pi.

Share a folder

Follow these steps to share a folder on Windows:

  • Create a new folder to store your backups
  • Right click on this folder and choose Properties in the context menu
  • Go on the Sharing tab and click on the Share button
  • By default, your user will have access to this share, but you can add other users if you want. You’ll need a user with a password. If your main user doesn’t have any password, you need to set one. If you prefer you can create another account with a password.
  • When it’s ok, click on the Share button
  • It’s OK. You are now sharing the folder on the local network Keep the network path to use it on the next step For me, it was something like this: \[COMPUTER-NAME]\Users[USER]\Desktop\share

Mount the share on the Raspberry Pi

Now we need to connect the Raspberry Pi to the share. To do this permanently, follow these steps:

  • Create a new folder to host the share: sudo mkdir /media/share
  • Edit the /etc/fstab file: sudo nano /etc/fstab This file manages all drives and shares on the Raspberry Pi.
  • Paste this line: //[COMPUTER-NAME]/Users/[USER]/Desktop/share /media/share cifs username=[USER],password=[PASSWORD],iocharset=utf8 0 0 You need to replace all variables with your environment values. You may also need to replace the folder name in the first value. Note that we use / and not \ in the network path.
  • Save and quit (CTRL+O, Enter, CTRL+X)
  • Mount the share once: sudo mount /media/share

Your shared folder is now accessible in /media/share and if you create a file in this folder, the file will be available on your Windows computer: sudo touch /media/share/test

Script example

Now that the share is available, you can create and schedule a script to copy backup files to this folder each day for example:

#!/bin/bash
cp /home/pi/backups/* /media/share/
find /media/share -mtime +8 -delete

Restore

This backup is the easiest one to restore. You only need to extract files from the archive and send them back to the original folder.

From Linux

On Linux, you can use the default archive software from your distribution. For example, on Ubuntu, double-click on the backup file to open it and see files in it. And then click “Extract”.

Once you get the needed files, send them back to the Raspberry Pi with rsync as seen above.

From Windows

On Windows, I’m not certain that the default tool can open a .tar.gz file, but you can try. If not, you have to install a software like 7zip or WinRAR.

Extract the files with this tool and transfer them to the Raspberry Pi with WinSCP as seen above.

Try it

It’s a good idea to follow this restore procedure at least once. So you can be sure that it works and that you know how to do it.

Monitoring

With this method, it’s easy to check that your backup is well done each day as expected.

You could create a script to check that you have recent files in your backups folder. Something like this:

#!/bin/bash

if [[ $(find /home/pi/backups -type f -mtime -1) ]]; then
    echo "Backup OK"
    //DO NOTHING
else
    echo "Backup KO"
    //DO SOMETHING (MAIL ?)
fi

Export MySQL data

If you have MySQL databases on your Raspberry Pi, the procedure is a bit different.

Backup

The best practice for MySQL data backup is to first export and then save this export.

Export database with this command line: mysqldump -uroot -p[PASSWORD] [DATABASE-NAME] > export-[DATABASE-NAME].sql

This command will create a file with all SQL queries needed to recreate the database from scratch.

You can follow the same steps as seen previously to:

  • create a script
  • schedule it
  • transfer files to another computer

If you have more than one database to back up, put as many lines as needed in your script and change the database name and the file name on each line.

Restore

To recover a lost database you have to follow the same steps as for files backup.

Once the .sql file back on the Raspberry Pi, you can use this command to import data to a fresh database: mysql -uroot -p[PASSWORD] [DATABASE-NAME] < [BACKUP-FILE].sql

The database must be empty to start the import. So depending on the situation, you must follow one of these two methods:

  • Import the backup into a database with another name (and then copy only what you are interested in)
  • OR rename the corrupt database, recreate it (empty) and then import the backup file

As with file backup, consider testing this procedure at least once.

Create an image of the SD card

So we saw in the previous paragraphs how to save some files on a Raspberry Pi. Now, how to do if you have a complex installation that you want to fully back up?

Backup

The goal of this backup method is to create an image with all your Raspberry Pi files in it. Then you could flash this image to another SD card to recover the Raspberry Pi in the same state.

Linux

On Linux, you can use the dd command to create an image of a device:

  • Take the SD card out from the Raspberry Pi
  • Insert the SD card on your computer
  • Find the device name: sudo fdisk -l This command will display all the storage devices of your computer. You should see your hard drive, and somewhere at the end of the list your SD card. Note the name of the device (generally /dev/sdX or /dev/mmcblk0).
  • Create the image (replace the device name) : sudo dd bs=4M if=/dev/mmcblk0 of=backup.img

After a few minutes, you’ll get the full backup image from your Raspberry Pi.

This command should also work on macOS, replace 4M by 4m in the last command.

Windows

On Windows, the best software to do this is Win32 Disk Imager:

  • Download Win 32 Disk Imager, install it and launch it
  • You should get a window like this win32 disk image
  • Enter the image destination folder and file name (.img)
  • Select the SD card letter in the device list
  • Click on “Read”
  • This tool will create the image in the selected folder

Restore

The easiest way to restore this backup is to flash the image on another SD card with Etcher:

  • Download Etcher from the official website
  • Install and run it
  • Choose your backup image on the left
  • Choose your SD card
  • Click on the “Flash” button

You’ll get a new SD card with the system in the same state as during the backup.

On Linux / Mac you can also use dd to flash the SD card (reverse if and of): sudo dd bs=4M if=backup.img of=/dev/mmcblk0 If you have an empty SD card, feel free to try this procedure to be sure it’s working in your case.

 

You may also like:

 

Clone a Raspberry Pi in operation (hot backup)

This last way is the same idea as the previous one, but for a critical installation. If you can’t stop your Raspberry Pi to make the full backup into an image, you have to follow this procedure.

Prerequisites

I will introduce you two ways to do a full backup of your Raspberry Pi in operation (from a desktop tool or with command lines). In both cases, you’ll need to plug a second SD card in the Raspberry Pi to copy the running SD card to another.

If you don’t have one already, I recommend buying a cheap USB adapter on Amazon like this one. Prefer an adapter with a short cable (long cable is not needed, and USB key format may obstruct access to other plugs). So, the one I link is perfect for this. If you need more details, I explain how to use a USB SD card reader on your Raspberry Pi in this article.

PiClone (Desktop version)

If your Raspberry Pi is running the Desktop version, there is a tool called PiClone you can use for this. The name in the main menu is “SD Card Copier”. You can find it in the Accessories submenu.

The use of this software is simple: sd card copier raspbian

  • Choose your main SD card in the “Copy From Device” list
  • Choose your backup SD card in the “Copy To Device” list
  • Press Start to start the transfer
  • Wait a few minutes and extract the backup SD card

Feel free to try this backup SD card in another Raspberry Pi to check it’s working properly.

Script (Lite version)

If you are running the Raspberry Pi OS Lite version or if you want to create a script, there is a shell script available to do this.

I will explain the basics to use this script. But you can find all the information on the Github page if you need more.

  • You’ll need to install Git to download the files: sudo apt-get install git
  • Start by downloading and installing the script: cd ~/Downloadsgit clone https://github.com/billw2/rpi-clone.gitcd rpi-clonesudo cp rpi-clone rpi-clone-setup /usr/local/sbin
  • Then use fdisk to get the backup SD card name: sudo fdisk -lYou should see the current SD card: sda or mmcblk0 most of the time.
  • And below the backup SD card: sdb or mmcblk1 generally
  • And finally, use rpi-clone: sudo rpi-clone sdb -v Replace sdb with your backup SD card name.

Support us: Join the community on Patreon to show your support, get behind-the-scenes content and other awesome perks!

Conclusion

That’s it. You know how to make backups of your Raspberry Pi in several ways.

Try to schedule them or if it’s not possible, try to do them regularly. Also, try to check them from time to time.

There is nothing worse than a backup that hasn’t worked or is unusable because you have never tested that it worked as expected.

Scroll to Top