Husar
Posts: 67
Joined: Tue Nov 12, 2013 12:43 am
Location: Midwest, USA
Contact: Website

Backing Up SD Card?

Mon Nov 18, 2013 6:35 pm

I am looking for a way to automate a nightly backup of an DS card. I don't want to remove it and archive the img each time I want a backup. I would be great if this was done via a cron or other method and I wouldn't have to worry about it. Maybe backing up to a mounted HD or a USB thumb drive?

Thanks.
Ed

gmc
Posts: 123
Joined: Fri Mar 09, 2012 11:31 am
Location: Cheshire, UK
Contact: Website

Re: Backing Up SD Card?

Mon Nov 18, 2013 8:48 pm

I run rsync and sync my card to a linux server once a week.
Last edited by gmc on Tue Nov 19, 2013 7:19 am, edited 1 time in total.

User avatar
DougieLawson
Posts: 39120
Joined: Sun Jun 16, 2013 11:19 pm
Location: A small cave in deepest darkest Basingstoke, UK
Contact: Website Twitter

Re: Backing Up SD Card?

Mon Nov 18, 2013 11:01 pm

Do you want a logical copy of it?
Or a bootable physical copy?

You can use dd if=/dev/mmcblk0 of=/somewhere/backup.img on a running system. You just have to remember to run a fsck when you restore that *.img file.

rsync is a logical copy.
Note: Any requirement to use a crystal ball or mind reading will result in me ignoring your question.

Criticising any questions is banned on this forum.

Any DMs sent on Twitter will be answered next month.
All non-medical doctors are on my foes list.

gmc
Posts: 123
Joined: Fri Mar 09, 2012 11:31 am
Location: Cheshire, UK
Contact: Website

Re: Backing Up SD Card?

Tue Nov 19, 2013 7:24 am

What he said.

I also run a full backup (create a img file) with the below script which I found on the forums here:

I think I might have added a mod or two. It creates a img file and then transfers it to my linux server via a nfs mount. I've uncommented the tar lines as the compression doesnt safe much and takes too long.

Code: Select all


#!/bin/bash

# Setting up directories
SUBDIR=raspberrypi_backups
DIR=/mnt/disk2/$SUBDIR

echo "Starting RaspberryPI backup process!"

# First check if pv package is installed, if not, install it first
PACKAGESTATUS=`dpkg -s pv | grep Status`;

if [[ $PACKAGESTATUS == S* ]]
   then
      echo "Package 'pv' is installed."
   else
      echo "Package 'pv' is NOT installed."
      echo "Installing package 'pv'. Please wait..."
      sudo apt-get -y install pv
fi

# Check if backup directory exists
if [ ! -d "$DIR" ];
   then
      echo "Backup directory $DIR doesn't exist, creating it now!"
      mkdir $DIR
fi

# Create a filename with datestamp for our current backup (without .img suffix)
OFILE="$DIR/backup_$(date +%Y%m%d_%H%M%S)"

# Create final filename, with suffix
OFILEFINAL=$OFILE.img

# First sync disks
sync; sync

# Shut down some services before starting backup process
echo "Stopping some services before backup."
#service apache2 stop
#service mysql stop
#service cron stop

# Begin the backup process, should take about 1 hour from 8Gb SD card to HDD
echo "Backing up SD card to USB HDD."
echo "This will take some time depending on your SD card size and read performan                ce. Please wait..."
SDSIZE=`sudo blockdev --getsize64 /dev/mmcblk0`;
sudo pv -tpreb /dev/mmcblk0 -s $SDSIZE | dd of=$OFILE bs=1M conv=sync,noerror if                lag=fullblock

# Wait for DD to finish and catch result
RESULT=$?

# Start services again that where shutdown before backup process
echo "Start the stopped services again."
#service apache2 start
#service mysql start
#service cron start

# If command has completed successfully, delete previous backups and exit
if [ $RESULT = 0 ];
   then
      echo "Successful backup, previous backup files will be deleted."
      rm -f $DIR/backup_*.tar.gz
      mv $OFILE $OFILEFINAL
      #echo "Backup is being tarred. Please wait..."
      #tar zcf $OFILEFINAL.tar.gz $OFILEFINAL
      #rm -rf $OFILEFINAL
      #echo "RaspberryPI backup process completed! FILE: $OFILEFINAL.tar.gz"
      exit 0
# Else remove attempted backup file
   else
      echo "Backup failed! Previous backup files untouched."
      echo "Please check there is sufficient space on the HDD."
      rm -f $OFILE
      echo "RaspberryPI backup process failed!"
      exit 1
fi



Return to “Beginners”