How do I backup my Raspberry Pi?

How do I backup my Raspberry Pi?

I have been writing programs for my Raspberry Pi (running Raspbian) for a few weeks now and would like to make sure I protect the work I have done. 
How can I backup the files I have created? 
Can I simply plug the SD card into my Windows (XP or 7) PC and copy it either to the hard drive or another SD card?

Solutions/Answers:

Answer 1:

If you want to preserve all of the data, you will probably have to create a disk image. Furthermore, Windows cannot recognize typical Linux filesystems, so you probably won’t even be able to see your files, when you plug in your SD card.

Creating a disk image will preserve not only files but also the filesystem structure and when you decide to flash your new SD card, you will be able to just plug it in and it will work.

Linux

On Linux, you can use the standard dd tool:

dd if=/dev/sdx of=/path/to/image bs=1M

Where /dev/sdx is your SD card.

Mac

On Mac, you can also use the standard dd tool with a slightly different syntax:

dd if=/dev/rdiskx of=/path/to/image bs=1m

Where /dev/rdiskx is your SD card.

(using rdisk is preferable as its the raw device – quicker)

To find out which disk your device is type diskutil list at a command prompt – also, you may need to be root; to do this type sudo -s and enter your password when prompted.

Windows

Option 1

On Windows, you can use the reverse process that you used when flashing the SD card.

You can use Win32 Disk Imager, which is the preferred tool for flashing a SD card of the Foundation. Just enter the filename (the location and name of the backup image file to be saved), select the device (the SD card) and press read:

Win32 Disk Imager

Of course, you can also use RawWrite, dd for Windows or similar tools, the process is quite similar.

Option 2

If you don’t want to back up your entire system, but only specific files, I suggest you connect to your Raspberry Pi via SFTP and copy the files to your local computer (You can use the WinScp client). If you have SSH enabled, SFTP usually requires no special configuration on the Raspberry Pi side.

Another option is to copy the files to a remote system using rsync.

You can also install special drivers so your Windows can read ext filesystems (and will thus be able to read the whole SD card), such as ext2fsd but it is probably not worth the effort.


Since the image will be of the same size as your SD card, you may want to compress it. This can be achieved simply by using your favorite compression tool, such as gzip, 7zip, WinZip, WinRar …

Answer 2:

If you are running Linux then you can use the dd command to make a full backup of the image:

dd if=/dev/sdx of=/path/to/image

or for compression:

dd if=/dev/sdx | gzip > /path/to/image.gz

Where sdx is your SD card.

To restore the backup, you reverse the commands:

dd if=/path/to/image of=/dev/sdx

or when compressed:

gzip -dc /path/to/image.gz | dd of=/dev/sdx

Answer 3:

Besides those block-level backups, there are two common approaches to deal with the sources: to archive it continuously (1), or to use the revision control system (2).

We are going to use the command-line (any local terminal or SSH connection to a Raspberry Pi machine), right?

1. Archive

cd ~/projects
tar czvf your-raspberry-project-top-level-dir-v1.0.tgz \
         ./your-raspberry-project-top-level-dir 
scp your-raspberry-project-top-level-dir-v1.0.tgz \
         user@backup-host:/home/user/backups/

2. RCS (Git for instance)

cd ~/projects/your-raspberry-project-top-level-dir
make clean                          # Or clean it manually using rm (rm ./*.o ./*.pyc)
git init                            # Create new repo here
git add .                           # Add source files to the staging index
git status                          # Verify if it's OK
git commit -a -m "Initial import"   # Fix application's source changes
git add remote https://github.com/user/your-raspberry-project.git
git push -u origin master           # Sends the sources to your github repo
git pull && git push && git status  # Now origin/master is your tracking branch

Answer 4:

On the Mac you don’t want to be using /dev/diskn. You should use /dev/rdiskn instead, where n is the number the OS uses to identify your SD card. This decreases the time required to copy by a huge amount.

So for the optimal backup process on a Mac, I would recommend doing the following:

Run diskutil list, and find the disk corresponding to your Raspberry Pi’s SD card:

$ diskutil list
/dev/disk0
   #:                       TYPE NAME                    SIZE       IDENTIFIER
   0:      GUID_partition_scheme                        *500.1 GB   disk0
   1:                        EFI                         209.7 MB   disk0s1
   2:                  Apple_HFS Macintosh HD            499.2 GB   disk0s2
   3:                 Apple_Boot Recovery HD             650.0 MB   disk0s3
/dev/disk1
   #:                       TYPE NAME                    SIZE       IDENTIFIER
   0:     FDisk_partition_scheme                        *7.9 GB     disk1
   1:             Windows_FAT_32                         58.7 MB    disk1s1
   2:                      Linux                         7.9 GB     disk1s2

Clearly /dev/disk1 is my 8GB SD card, the Linux partition name is also a bit of a clue.

However, instead of using /dev/disk1 with dd, you should use /dev/rdisk1, like so:

sudo dd if=/dev/rdisk1 of=/path/to/backup.img bs=1m

And to restore it, just swap the if (input file), and of (output file) parameters:

sudo dd if=/path/to/backup.img of=/dev/rdisk1 bs=1m

Or, with gzip, to save a substantial amount of space:

sudo dd if=/dev/rdisk1 bs=1m | gzip > /path/to/backup.gz

And, to copy the image back onto the SD:

gzip -dc /path/to/backup.gz | sudo dd of=/dev/rdisk1 bs=1m

For more information, see this wiki page.

Answer 5:

You can run this command from your Linux PC with lots of space:

ssh root@raspberrypi gzip -c /dev/mmcblk0 > img.gz

As a prerequisite you’ll need to have generated .ssh keys and copied the id_XXX.pub over to /root/.ssh/authorized_keys.

It’s possible to have issues with the file system since it’s a live backup, but if your Raspberry Pi isn’t real busy it will usually be OK.

It’s probably faster to do the compression on the PC like this:

ssh root@raspberrypi dd if=/dev/mmcblk0 | gzip -c > img.gz

Answer 6:

If you are using Windows:

  1. Download Win32 Disk Imager.

  2. Create the file path you wish to use to save your image. (I use C:\RasPi_Bkp)

  3. Run Win32 Disk Imager

  4. Browse to your backup file path spot, and type a file name.

  5. Click save

  6. Make sure the device shown in the drop down to the right of your file path is the one you want to back up.

Win32 Disk imager in Action

  1. Click “Read”

  2. Wait.

Answer 7:

If your programs are all in the pi userid, there’s really no reason to back up the entire filesystem, as is being suggested. what I do is to just back up the single id. I run the command:

tar -czf pi.tgz *

from pi’s home directory, which creates the file pi.tgz, containing all the files and directories contained there (excluding hidden files). I then scp this file to another linux computer, but you could ftp it or e-mail it somewhere for safe-keeping… anything that got it onto another computer.

This creates a much smaller file for your back-up.

Concerned about all the additional packages you’ve installed along the way? Create a script that will perform those installs for you again should you need to recreate your SD card, and keep it in pi’s home directory somewhere. (I have a ~/bin directory for such things). Have all the commands you need to do the installs to bring you back to the position you want to be in. It documents your changes, and allows you to quickly build up a new SD card. It would contain commands in the form of:

apt-get --assume-yes install apache2 mysqld mysql php5 php-pear

Start it out with

apt-get update
apt-get --assume-yes upgrade

so that your system also is brought up to the current level before you start adding your packages.

Since this will be in your pi.tgz file, you’ll have it when you need it.

Answer 8:

I run Raspbian and use dd and cron to do automated backups of my SD card to my external USB drive.

It is a really simple solution. It runs once a week at 2 am on a Monday morning and makes an image of the SD card.

I wrote a script which shuts down services such as lighttpd and cron, to minimise the risk of the SD card being written to in the middle of the backup, it then calls dd, before starting to the services again when the backup has finished.

I’ve put the script and some instructions on my blog, http://www.stuffaboutcode.com/2012/08/raspberry-pi-auto-backups.html

Answer 9:

For Debian you could use dd and tar. This would make a complete copy of your SD card and would require external (to SD card) storage, probably mounted USB or network drive.

dd if=/dev/sdd of=yourbackupfilenamehere.dd
tar zcvf yourbackupfilenamehere.dd.tar.gz

With /dev/sdd being the location of your SD card, and of being the output file name.

Answer 10:

Fortunately the Raspberry Pi can create a backup of itself which is independent of the primary OS used to access the Raspberry (Windows, Linux, Mac).
dd, tar and rsync are different tools to create backups on Linux. All these types of backups can be created by the running Pi provided it’s stopping all busy tasks as mysql, smb, dlna, owncloud, seafile, apache et al before creating the backup. There is a different question where the approach is explained in more detail and a possible solution is explained here

Answer 11:

If your Raspberry Pi is connected to a network and you want automatic on-the-fly backups, probably the easiest way is Bittorent Sync – very easy install and running smoothly and stable on the Pi and many other platforms.

Answer 12:

No more scary dd, sync, tar, …, beginner friendly SD Card Copier

The new version of Raspbian called stretch has a very beginner friendly app called SD Card Copier. You don’t need to use the Linux tools such as dd etc. It can even copy big cards to small cards.

How to do it

You start with the GUI RPi app menu icon at the very top left corner, go to Accessories, then click SD Card Copier.

All you need to do is a couple of clicks inside the GUI!

After you make the backup SD card, you can use Windows Disk Manager to read the card’s image to a Windows file and store it in any Windows directory. This way you need only a physical SD card and store many versions of images in the Windows PC.

Of course you can just use the backup card as the working copy, to replace your original card, which now becomes your master card, and lock it in a safe place.

Answer 13:

While searching to create an image of my customized SD card I found this post. The best answer here discusses imaging the SD card, but what I was looking for required shrinking down the filesystem to make the image as small as possible. I ended up creating my own tool to do this and outline the process at: https://raspberrypi.stackexchange.com/a/37899/32585

Answer 14:

Here are 3 options:

  1. In the latest version of Raspbian, there is a tool that converts your OS and files into a .img file that you can then place onto a USB, or your PC.

  2. Plug your SD card into a Windows PC, and start up Win32 Disk Imager (install here if you haven’t already.)

    In (1) type a filename & location for athe image file. In (2), select the drive letter of your SD card. Then press 3. This does the same as option 1.

  3. Plug in a USB stick to your Pi, and copy any important files across.

Our Awesome Tools

References

Loading...