I have PC's running both Ubuntu and Windows (XP & 7), but I would expect most people to be interested in the Windows answer. I will edit the question to make that clear.
–
Steve Robillard♦Jun 14 '12 at 18:38

For code, you may also want to consider using distributed version control software like git or hg, that way you not only have copies of your code in multiple places, but also have the complete history of how that code has changed.
–
Mark BoothJun 15 '12 at 11:01

1

@MarkBooth I don't see why a DVCS is needed over a VCS if we are just considering being able to retrieve code later. Heck, it's even easier since you use revision numbers not commit hashes to identify a particular state. Learning a CVCS like Subversion isn't much of a time investment (as I recall... though it was a very, very long time ago). Yes, switching to Git was a bit unnerving at the time, but I have never ever looked back.
–
Steven LuApr 13 '14 at 21:17

12 Answers
12

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, select the device and press read:

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.

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 s probably not orth 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 favourite compression tool, such as gzip, 7zip, WinZip, WinRar ...

@Yamikuronue Yes. The page in the answer has a link to the sourceforge page you specified.
–
Cristi DiaconescuMay 6 '13 at 16:34

1

When I use WinDiskImager, I get an image that is the size of the storage partition - the boot partition appears to be missing. Have you tried writing the image onto a new SD card? I don't have a second one with which to test at the moment.
–
me--Jan 3 '14 at 3:46

bzip2 should compress even better, and is available on all Linux systems. On newer Linux systems xz should compress even better.The decompressors for these are bunzip2 and unxz respectively.
–
ArneMay 29 '13 at 12:10

1

@Arne I tried using bzip2 now, and maybe it would be appropriate if I was backing up from my Class 4 SD card, but on this Class 10 UHS 1 SanDisk 8GB card (which was $12 last week) it is maxing out an Ivy Bridge CPU thread and limiting read speed from the card to 6MB/s (dd with Ctrl+T reports 4.2MB/s). This is not ideal because I can use gzip with this card and read off of it at a much quicker pace (dd reporting 18MB/s). Granted, if most of the disk is free space, then reading during those empty stretches will speed up. But I'll have to see what the difference is in the resulting file size.
–
Steven LuApr 20 '14 at 3:40

Besides that 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 command-line (any local terminal or ssh connection to a raspberry machine), right?

It might also be worth noting that people can set up bare repos on other machines on their network and push from their Raspberry Pi to there, rather than having to set up a github account, ssh keys etc and push to there.
–
Mark BoothApr 13 '14 at 12:06

That's a good start for the real answer. Code needs to be managed under version control if one wants to be able to maintain it. Version control allows to track changes, understand history of changes, manage different branches (e.g. stable vs dev) and merge between them. But it is not backup per se. However, one simply need to backup the repository (e.g. the .git folder for Git). Check Linux/Unix forums, wikis, stackexchanges, etc. for ways to backup folders under Linux. Note: I don't consider github as backup. And you don't want to publish everything to github!
–
Huygens2 days ago

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 Pi's SD card:

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.

I run raspbian and use dd and cron to do automatted backups of my SD to my external usb drive.

Really simple solution, it runs once a week at 2am 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.

This has the limitation of needing somewhere to write the image to though (that is also large enough to take the image(s). Presumably it also means a large data io through the usb hub - would take a while?
–
Jon EgertonJul 5 '12 at 10:38

True, but it's kind of required in the question: "I want a full image of the SD card."
–
Forkrul AssailJul 5 '12 at 10:39

1

Why do you make a .tar.gz file of the dd-file? Wouldn't it be better to just run gzip or bzip2 on the dd-file directly?
–
AndersJul 25 '12 at 21:11

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 ways to create the backups. All these types of backups can be initiated and created by the running Pi provided that it's stopping all busy tasks as mysql, smb, dlna, owncloud, seafile, apache et al before creating the backup. There is an answer which explains this approach in more detail.

If your RasPi 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.

I've been using usbit for Windows. It's the only tool I can find that will allow you to swap from a larger SD card to a smaller one. All I did was tick the following on the options page; Ignore size checks & Truncate oversize images.