ZuLuuuuuu
Posts: 26
Joined: Thu May 23, 2013 6:30 am

Backup SD card image to USB disk while Raspberry is running

Fri Nov 27, 2015 6:03 pm

Hello, for one of my projects I want to automate SD card backup, so I don't want to remove the SD card from Raspberry, plug it to my PC and back it up. I want it to be backed up automatically to a USB disk while Raspberry is still running.

Is this possible?

sandstein
Posts: 6
Joined: Wed Nov 18, 2015 9:55 pm

Re: Backup SD card image to USB disk while Raspberry is runn

Fri Nov 27, 2015 8:17 pm

Well you can "dd if=/dev/rootdevice of=file bs=4k" or so...
But to make it save you should do a "mount -o remount,ro /" which does a read only mount of the filesystem. You would need another partition to dump your data to or dump over net by piping dd into ssh.

Joe Schmoe
Posts: 4277
Joined: Sun Jan 15, 2012 1:11 pm

Re: Backup SD card image to USB disk while Raspberry is runn

Fri Nov 27, 2015 8:39 pm

The real answer to your question is: Yes, and it will work most of the time. Obviously, systems need to be able to update themselves while running, so the basic idea has to be basically sound. So, yes, I would do it, and it should work.

However, the canonical answer - that you will get from posters on the forum - is that you can't do it and that it will lead to corruption and ingrown toenails.
And some folks need to stop being fanboys and see the forest behind the trees.

(One of the best lines I've seen on this board lately)

User avatar
DougieLawson
Posts: 36578
Joined: Sun Jun 16, 2013 11:19 pm
Location: Basingstoke, UK
Contact: Website Twitter

Re: Backup SD card image to USB disk while Raspberry is runn

Sat Nov 28, 2015 12:20 am

sandstein wrote:Well you can "dd if=/dev/rootdevice of=file bs=4k" or so...
But to make it save you should do a "mount -o remount,ro /" which does a read only mount of the filesystem. You would need another partition to dump your data to or dump over net by piping dd into ssh.
I've never bothered with remounting read-only as the automated fsck will fix problems during boot of a restored backup.

It may be better to use rsync to back up on a file by file basis rather than DD to back up on a partition by partition basis.
Note: Having anything humorous in your signature is completely banned on this forum. Wear a tin-foil hat and you'll get a ban.

Any DMs sent on Twitter will be answered next month.

This is a doctor free zone.

stderr
Posts: 2178
Joined: Sat Dec 01, 2012 11:29 pm

Re: Backup SD card image to USB disk while Raspberry is runn

Sat Nov 28, 2015 1:37 am

ZuLuuuuuu wrote:Hello, for one of my projects I want to automate SD card backup, so I don't want to remove the SD card from Raspberry, plug it to my PC and back it up. I want it to be backed up automatically to a USB disk while Raspberry is still running.
What do you want to back up? Is it your /home directory or do you want to back up a bootable system? I would separate the two activities in any case.

ZuLuuuuuu
Posts: 26
Joined: Thu May 23, 2013 6:30 am

Re: Backup SD card image to USB disk while Raspberry is runn

Sat Nov 28, 2015 5:48 pm

Thank you for your suggestions.
stderr wrote:
ZuLuuuuuu wrote:Hello, for one of my projects I want to automate SD card backup, so I don't want to remove the SD card from Raspberry, plug it to my PC and back it up. I want it to be backed up automatically to a USB disk while Raspberry is still running.
What do you want to back up? Is it your /home directory or do you want to back up a bootable system? I would separate the two activities in any case.
I want to back up a bootable system, in other words an .img file ready to be burned into an SD card.

mrpetrov
Posts: 19
Joined: Sun Mar 08, 2015 3:44 pm

Re: Backup SD card image to USB disk while Raspberry is runn

Fri Dec 04, 2015 9:29 pm

I use BTRFS as root file system on my RPi2, which acts as a home appliance controller, and BTRFS allows snapshotting, backing up the snapshot, etc.

Check how I did it here: viewtopic.php?f=29&t=104108

Not exactly an .img file ready to go to a SD card, though...

User avatar
yy502
Posts: 30
Joined: Sun Aug 03, 2014 2:15 am
Location: Cambridge, UK

Re: Backup SD card image to USB disk while Raspberry is runn

Mon Dec 07, 2015 11:18 pm

Yes, it's possible. Just use tar and exclude certain dirs. I back up my system to a tgz file every 15 days with a cron job.

This is my back up script. 1st argument is target file prefix, or enter it when prompted.

Code: Select all

#!/bin/bash

DES=""

if [ -z "$1" ] ; then
    echo -n "Enter target backup file PREFIX with path (.<DATE>.tgz is auto-appended): "
    read -e DES
else
    DES=$1
fi

EXCLUDE_FILES="
/home/pi/tmp
/home/pi/Downloads
/dev
/media
/mnt
/proc
/run
/sys
/tmp
/lost+found
.gvfs
cache
.cache
.thumbnails
.xsession-errors
Cache
"

EXCLUDE=
for f in ${EXCLUDE_FILES};
do
    EXCLUDE="${EXCLUDE} --exclude=${f}"
done

sudo tar -cvzpf ${DES}.$(date +"%Y%m%d").tgz ${EXCLUDE} /
sudo chown pi:pi ${DES}.*.tgz

To restore:

Code: Select all

#!/bin/bash

USER_DIRS="
home/pi/tmp
home/pi/Downloads
"

ROOT_DIRS="
dev
media
mnt
proc
run
sys
tmp
"

TGZ=""
DES=""

if [ -z "$2" ] ; then
    echo -n "Enter source backup file with path: "
    read -e TGZ
    echo -n "Enter restore destination path with '/' (e.g. /media/usbdisk/): "
    read -e DES
else
    TGZ=$1
    DES=$2
fi

sudo tar xvpfz ${TGZ} -C ${DES}

for d in ${USER_DIRS};
do
    mkdir -p ${DES}${d}
    sudo chown pi:pi ${DES}${d}
done

for d in ${ROOT_DIRS};
do
    mkdir -p ${DES}${d}
    sudo chown root:root ${DES}${d}
done

Note that this script treats /boot as a directory rather than a different partition. After restoring to your new root dir (2nd partition of your new SD card), simply move the contents of /boot to the first partition (FAT) of your SD card.

Return to “Advanced users”