Backing up / Imaging PiAware SD card remotely?

Hi guys,

With all the tweaks, updates and options we have with PiAware (mutability, FR24 feeders etc), I’m sure a lot of people will want to try these out, but dont want to risk “hosing” their current PiAware setup.
Being a PC-man, my knowledge of command-line Linux is limited.

So, if I wanted to make a complete image of my current PiAware SD card - that could just be rewritten to the SD if I trash the install - BUT do this over my (wired) network - Pi’s in the attic - what’s the best way of doing this?

THX

Check out http://www.aboutdebian.com/tar-backup.htm for making TAR (compressed) backups of your Pi.

Each Pi runs the following (cron) script twice a week to make a backup and then copy the backup to the server in the closet (I do a local backup then copy to the server so that I get a complete backup even if the server is unavailable). I also run it before I’m about to try something exciting that I may want to back out of… The backup file has the Pi host name and a date stamp. You do give all your Pis different host names, don’t you?

The script assumes you have a (root level) directory /backups – this one also skips mounted servers /sheep and /pigs

(mea culpa, mea culpa, mea maxima culpa – if you have suggestions on how to make this better, I’m open to them!)
(warning: this doesn’t work with “noobs” installs, as they do funky paritioning)


#!/bin/bash
# K6RTM 2014/07/17 back up raspberry pi and copy the backup to server volume /sheep/rpibackups
# using the $HOSTNAME for the backup file
# insure we have a server connection, it's in fstab
sudo mount -a
# put us in the backups directory
cd /backups
# remove old backups (dangerous, I know)
echo "Snuff old backups..."
sudo rm *.gz
# capture our startup time
T="$(date +%s)"
# capture date for date-stamping the backup file
_now=$(date +"%Y-%m-%d")
_bfile="/backups/"$HOSTNAME"backup-$_now.tar.gz"
_sfile="/sheep/rpibackups/"$HOSTNAME"backup-$_now.tar.gz"
# start doing the backup
echo "Starting backup to $_bfile..."
tar -zcpf "$_bfile" --directory=/ --exclude=proc --exclude=sys --exclude=dev/pts --exclude=backups --exclude=var/swap --exclude=sheep --exclude=pigs .
# done, let 'em know
T="$(($(date +%s)-T))"
printf "Backed up in %3d minutes.
" "$((T/60))"
# capture time again for copying to server
T="$(date +%s)"
# copy the backup file to the server
echo "Copying $_bfile to $_sfile"
sudo cp "$_bfile" "$_sfile"
T="$(($(date +%s)-T))"
printf "Copied to server in %3d minutes.
" "$((T/60))"


Save this as something like backups.sh in the /backups directory. make sure it’s executable. It excludes a number of system things, as well as the backups directory and specific mounted volumes – otherwise it will try and back up anything and everything you have mounted. This usually takes a long time and fails when the SD card in your Pi fills up…

If you have 5 gigs used on your SD card, this can take up to an hour – be patient. The Pi 2 is faster!

restore is easy–

cd /
sudo tar -zxvpf /backups/

and reboot when it completes.

bob k6rtm

Thanks Bob- looks good!

I only have the Rpi (everything else in te house is PC based, or Android).

Found this, which uses DropBox to copy everything to - which seems ideal (as my RPi already has it’s own wired-network connection.

alexbelezjaks.com/automatic-back … -tutorial/

However, I was originally looking for a way to remotely image the RPi SD card - so I could completely “rebuild it” by a simple re-install of the image if needed.

Looking at the above, I’ll compare this with yours to work out what folders etc to copy.

Will study over the weekend, posted for others’ interest…

Just found this, which looks to create an image over the network (started locally from the Pi, pushed to a network location).

Am looking at pushing this image from my Pi to my PC in the first instance.

cagewebdev.com/index.php/raspber … s-running/

Will have a look at this myself over the weekend.

(Of course, if one of you linux guru’s on here could build that into a cronjob, that would be perfect!).

If you want to do a sd card to sd card image, look for rpi-clone, which does just that – copies your SD card to an SD card attached to a USB port.

https://github.com/billw2/rpi-clone

When I do this, I try and make the system as quiescent as possible, stopping other processes and services.

–bob

Bob:

Sorry to pop up an old thread, but your script looks like just what I need to copy my Pi installs. Problem is, it doesn’t run for me. Can you tell if you made subsequent revisions to the code posted here? I get tar errors indicating the local backup file was not found [even though I was in the process of creating the file…]. Thanks in advance.

hey there–

I haven’t really changed the script since I wrote it. I’ve got it running on a bunch of RPi systems running on my network.

The first time you run it, there aren’t any archives in the directory, so the delete command looking to snuff out old backups will fail – is that what you’re seeing? That delete (sudo rm *.gz) will throw an error message, but the tar backup runs and creates one.

Looking at the script, I made one change – I put a 30 second delay in between the tar step and copying the archive to the server to let things cool off a bit.

Here’s the script I’m running on my systems now:

$ cat /backups/backup.sh
#!/bin/bash

K6RTM 2014/07/17 back up raspberry pi and copy the backup to server volume /sheep/rpibackups

using the $HOSTNAME for the backup file

changed 2018/03/23 to add 30 second cooldown afterTAR

insure we have a server connection, it’s in fstab

sudo mount -a

put us in the backups directory

cd /backups

remove old backups (dangerous, I know)

echo “Snuff old backups…”
sudo rm *.gz

capture our startup time

T=“$(date +%s)”

capture date for date-stamping the backup file

_now=$(date +“%Y-%m-%d”)
_bfile=“/backups/”$HOSTNAME"backup-$_now.tar.gz"
_sfile=“/sheep/rpibackups/”$HOSTNAME"backup-$_now.tar.gz"

start doing the backup

echo “Starting backup to $_bfile…”
tar -zcpf “$_bfile” --directory=/ --exclude=proc --exclude=run --exclude=tmp --exclude=sys --exclude=dev/pts --exclude=backups --exclude=var/swap --exclude=sheep --exclude=pigs .

done, let 'em know

T=“$(($(date +%s)-T))”
printf “Backed up in %3d minutes.\n” “$((T/60))”
echo “30 second cool down…”
sleep 30

capture time again for copying to server

T=“$(date +%s)”

copy the backup file to the server

echo “Copying $_bfile to $_sfile”
sudo cp “$_bfile” “$_sfile”
T=“$(($(date +%s)-T))”
printf “Copied to server in %3d minutes.\n” “$((T/60))”

One thing to watch for – there are kinds of file system damage that fsck won’t detect/fix, but will cause the tar backup to fail.

Let me know what you’re seeing in more detail, I’ll help if I can, but for me it works a treat. Since I have a bunch of systems running this script, I have staggered their start times so they all aren’t trying to send things to the server at the same time.

cheers–

bob

1 Like