Remote Backup your Raspberry Pi with SSH via Terminal
Backing up the SD card of the Raspberry Pi is really time consuming. Switch off the Raspberry, remove the card, insert the card into the computer, create an image of the SD card, switch the card back into the Raspberry, switch it on again. Lots of manual work and downtime for the duration of the backup. Reason enough to do it all remotely via terminal session. These are the instructions for creating and restoring the backup. Works for backups of SD cards on any Linux system.
Create Backup
Open the terminal session and fire the following line, adjusting the red parts (IP address of the raspberry and file name of the backup):
ssh pi@192.168.10.11 “sudo dd if=/dev/mmcblk0 bs=1M | gzip -” | dd of=~/Desktop/backup_2017-11-14.gz
The system then asks for the given password for the user “pi”. Enter the password and then WAIT FOR A LONG TIME, even if the system does not display any updates during the process.
Fun Fact: If you are on the Mac, you can check the current status of your backup with “CTRL”+”T”. You can press the combination infinitely often during the transfer to display the transferred bytes.
When the backup is complete, your input line will reappear in the terminal.
For best performance I recommend this SD-card: http://amzn.to/2BDuGyX (Affiliate-Link)
Further on I recommend this Raspberry Pi Starter Set: http://amzn.to/2BERjDw (Affiliate-Link)
Restore Backup
Recovery does not work remotely. After all, the system would be flattened at runtime. It would be exactly like cutting the famous branch you are sitting on. If that was possible, it would be an security risk. Here you really have to grab the card and create the SD card from the backup on an external system.
Open the terminal session and fire the following line, adjusting the red parts (target disk {SD card} and file name of the backup):
diskutil unmountDisk /dev/disk1
gzip -dc ~/Desktop/backup_2017-11-14.gz | sudo dd of=/dev/rdisk1 bs=1m conv=noerror,sync
All the links in this text are Amazon Affiliate Links, and by clicking on an affiliate link I will earn a percentage of your subsequent Amazon purchases. However, this circumstance does not influence the background of my product recommendation. This product recommendation is independent, honest and sincere.
Bei allen genannten Links in der Videobeschreibung handelt es sich um Amazon-Affiliate-Links.Durch einen Klick auf einen Affiliate-Link werde ich prozentual an Euren darauf folgenden Amazon-Einkäufen beteiligt. Dieser Umstand beeinflusst aber nicht die Hintergründe meiner Produktempfehlung. Diese Produktempfehlung erfolgt unabhängig, ehrlich und aufrichtig.
David Gallagher
Hi Daniel – I followed these instructions, and was able to make a backup copy. However, it saved on the desktop of the Raspberry Pi, not on my PC backup. Do I need to take an extra step, such as transferring the file with WinSCP? Thanks!
Wolfgang
Hi Daniel, that is exactly what I was looking for. However, I get the message “bash: “sudo: command not found” after entering the password. Anything I could check? Thanks!
Wolfgang
Problem solved. I had to connect to the remote server first and then do “sudo dd if=/dev/mmcblk0 bs=1M | gzip – | dd of=~/Desktop/backup_2017-11-14.gz”.
Is there a way to store the backup directly on the requesting computer?
Max
Hey Daniel,
was running into trouble to execute on macOS 10.13 & 10.15. If anyone else runs in the same: removing the ‘ ” ‘ did the job.
ssh pi@192.168.10.11 sudo dd if=/dev/mmcblk0 bs=1M | gzip – | dd of=~/Desktop/backup_2017-11-14.gz
Best Max
Simon
Hi
I’m not sure if I’m missing something here. I am SSH into a remote Pi via putty. I am logged in and have gone to elevated rights via sudo su however when I try to run the commands I get:
gzip: -: No such file or directory.
I removed the ~ in the hope that it was that giving me issues and I have tried creating an empty file on the desktop with the correct name but still I get the error.
I’m not really a linux user so maybe I misunderstand something.
Thanks in advance for any sugggestions
Peter
Thanks Daniel. Here is a modified version I used to backup my pi to a linux desktop PC…
ssh pi@192.168.10.11 “sudo dd status=progress if=/dev/mmcblk0 bs=1M | gzip -f” >pibackup.img.gz
You need the double quotes to make the dd and gzip commands run remotely on the pi (if you cut/paste from a web page make sure the quotes are the correct chars). The “-f” option for gzip forces output to a terminal. “status=progress” outputs the dd progress to stderr, so you can see progress. The stdout output of ssh (remote dd & gzip commands) is redirected to the pibackup.img.gz file on the linux desktop where the ssh command is run.
Julio
Peter, thanks for the revised code. I was able to run it on MacOS 11 with success. For those that are copying the snippet, before hit enter at the terminal, change de double quotes typing it again.
Joe
On MacOS 11.2.3 (Big Sur) I verified you must remove the quotes. This works:
ssh pi@192.168.1.23 sudo dd status=progress if=/dev/mmcblk0 bs=1M | gzip -f > ~/Desktop/pibackup.gz
Brian
I found this to be the easiest way to follow this tutorial… Log into your pi via ssh the way you normally would. Then copy and paste the following (READ PAST THE COMMAND BEFORE EXECUTING)…
sudo dd status=progress if=/dev/mmcblk0 bs=1M | gzip -f > ~/Desktop/pibackup.gz
The above command makes some assumptions, so it may need to be modified. First off, pibackup.gz is the name of the backup file, so name it whatever you want but make sure .gz is the file extension. Secondly, it assumes there is a Desktop directory. I am using a Lite OS with no GUI so I replaced
~/Desktop/pibackup.gz
with
~/pibackup.gz
I am not sure if it will create a Desktop directory if one doesn’t exist, so I just played it safe and left it out. For those who are not familiar with Linux “~/” is a shortcut for the home directory of the user you are logged in as. It is the same as typing “/home/username/”. Just to clarify, “~/Desktop/piback.gz” is the same as “/home/pi/Desktop/pibackup.gz” assuming the user you are logged in as is the default “pi” user.
To copy the file from your pi to your local machine use the following command. For path to your backup file go to the directory containing you backup and type pwd (which stands for print working directory). You can copy and paste that path into the command. Path to local file is where you want the file to be saved on the computer you are actually using to ssh into the pi. To be clear, you type the following command from your computers command line, NOT from the pi terminal command line. So log out of your pi ssh session. I am not sure how to copy from the pi to your local machine from the pi terminal command line, but I’m sure there is a way if you want to research that yourself.
scp username@hostname:/path/to/backup/file /path/to/local/file
I just restored a backup from my backup file by using Raspberry Pi Imager on my Mac (free software download) and navigating to the .gz file. Worked great.
SHSH
Hi there,
i managed to create the backup as supposed –> Directly stored to my remote PC and not on the RPi
I have a general question:
Due to the fact, that the RPi is running all the time i assume that files/contents are changing while the very slow image-creation is running
Is there a risk, that the resulting .img-file is not working after re-flashing it to the SSD because some critical system-files have changed during the very time-intense backup and the result is a .img-file which has “file-combinantions” leading to a “not-running” system?
In the version where i shudown the RPi, unplug the SD card and create a img-file from this “not-running” system, there is no risk because no file should change during backup
P.L
There is no good way to make sure there is no writes during the hot backup is happening, therefore there are always chances you get a corrupted image. If your sever is not very busy, it might be fine, say, you backup daily, keep 10 versions, there is good chance you can find at least one working. If you have a very busy server that has writes(to the disk that you want to backup) all the time, such hot backup won’t work.
You can try to reduce the disk I/O by disabling swap and mounting filesystems to tmpfs, e.g. here is an article mentioning some useful tips, https://domoticproject.com/extending-life-raspberry-pi-sd-card/