Raspberry Pi : make an auto backup of the SD card

After the crash of my SD card There is little (see here), I told myself that it was time to get looking for a way to make a backup of the SD card automatically.

Raspberry Pi Home Server : faire un backup automatique de la carte SD

I browsed the forums and I came across a series of pretty interesting post (here and There among others) and I wanted to combine a little it is information and use them to ensure minimal if my system crashes, I do not find me to reinstall everything.

So I went on the principle that make a backup of the SD card while the system is running, It is not a good idea. The ideal is that another OS that works. As it is possible to boot a distro on a USB stick, I had my backup system :

  1. Since my main system, I restart the Raspberry Pi and boot on a USB key that contains a minimalist distribution
  2. Once in this system, I run my backups
  3. At the end of backups, I reboot to my main system

I chose to leave the USB that my home will be more easily accessible than the hard disk-based backups. Mac OS I installed Fuse that allows to read ext4 partitions so I can easily retrieve archives if needed.

Just to take action !

  1. Make a backup of your SD card before any thing (Yes Yes manually, you never know)
    DD - bs = 1 m if = / dev /.<xxx> of =<ImageName>.IMG
  2. Install Moebius on a USB key by following the instructions here
    The partition to boot on the USB stick will be sdX2 where X will be a or b
    My home is/dev/sda2 is that we will use later
  3. On the partition /boot the SD card, Save the file /boot/cmdline.txt en /boot/Cmdline.txt.origin and /boot/Cmdline.txt.usbkey
  4. Edit /boot/Cmdline.txt.usbkey (still from the SD card) and replace
    root = / dev/mmcblk0p2


    root = / dev/sda2
  5. A from now using Cmdline.txt.origin, on boot on the SD card, using Cmdline.txt.usbkey, on boot on the USB stick. Copy /boot/Cmdline.txt.usbkey in place of /boot/cmdline.txt, connect the USB and restart Raspberry Pi
  6. To connect using SSH. Please note the password is again raspi Since the distribution is any fresh
  7. The system will launch the Setup menu. Leave it without anything to do.Enlarge the main partition will not work because it is based by default on the SD card and the device.
  8. Reconnect
  9. Update the system
    apt-get update
  10. Install partclone which will allow a partition backup
    apt - get install partclone
  11. My choice was to create a partition that will use the rest of the key to store the backups from my SD card. So I used fdisk to add a primary with partition fdisk /dev/sda
  12. Edit the file /etc/fstab to automatically mount the partition at startup. Use the UUID to ensure the correct partition mounting (see here for mounting with UUID)
  13. Use the command dmesg to identify the map, generally mmcblk0backup-1
  14. The goal now is to create a script that after restarting will perform the following actions :
    1. Set various parameters
      Backup_path = / backup BACKUP_TYPE = full or BACKUP_TYPE = partition
    2. Be sure to find the map and that the backup partition is installed
      Mount | grep $BACKUP_PATH
    3. Choice :
      • Save all of the map (BACKUP_TYPE = full)
      • Save the MBR, the boot partition and the system partition (BACKUP_TYPE = partition)
    4. On the partition/boot from the SD card to the Cmdline.txt.origin in place of the Cmdline.txt
    5. Restart (the script is available here backup_sdcard.sh)
  15. Add the script in /etc/init.d so it will be launched at system startup
    update - rc.d defaults backup_sdcard.sh
  16. If you manually run the script, CA should create you a backup of the primary system in the backup directory
  17. Copy /boot/Cmdline.txt.origin in place of /boot/cmdline.txt, revive Raspberry Pi
  18. Create a script backup_sdcard.sh on the original system
    #! /bin/sh
    # Copy cmdline.txt.usbkey on cmdline.txt cp /boot/cmdline.txt.usbkey /boot/cmdline.txt
    # Reboot reboot
  19. You can decide if you start it manually or by crontab regularly

Voilà ! If you have concerns or comments, the comments are open !

Good holidays to all

You may also like...