[How to] Backup your SDCard over SMB / Network for Raspberry Pi users.

  • Hi All,


    I have written a small script, adapted from my Raspbian Pi Hole linux for LibreElec which will backup the ENTIRE SDCard to a share on the network (desktop PC, NAS, file server) once per week.

    This means if your MicroSD storage card dies, or heck if you lose your Pi somehow, you can simply "re-burn" an SDCard using an imaging program, like Win32DiskImager.
    (Please note I'm a mid-skill user, others may suggest cooler things which may be superior.)


    You'll need to enable SSH on your LibreElec install, it's in the menu somewhere (sorry, google it)

    SSH Terminal in to the Pi system with a tool like putty, mobaXterm, etc

    I decided to store my backup script file in the /storage/ folder. (NOTE: The SMB password is in plain text, inside a file, depending on your network setup, this may not be a big deal, some may find this scary, you can always make a new share, with a new username / password like 'backup/backup on your server with limited access)

    cd /storage
    nano backup.sh

    Paste in the below information into the nano text editor, be sure to change what's relevant to you!
    (I use shift insert to paste)

    mkdir /tmp/BackupDestination
    mount -t cifs -o username=USERNAME,password=PASSWORD //IPADDRESSOFSMBSHARE/SHARENAME/FOLDERS/ /tmp/BackupDestination
    dd bs=4M if=/dev/mmcblk0 | gzip -c >/tmp/BackupDestination/LibreElec-Pi-133.img.gz
    umount /tmp/LibreElecPi

    Press CTRL-O to 'write out' (save) the file and CTRL-X to exit Nano.

    You may need to put in a username and password, relevant to your share, you then need the IP of the server (your desktop PC?) and a shared Windows SMB folder off it

    Here's mine for example:

    Code
    mount -t cifs -o username=root,password=PASS //192.168.0.9/data/backup/LibreElecPi/  /tmp/BackupDestination

    This will map the local folder called tmp/BackupDestination to (a mount point) on the destination SMB (CIFS) share remotely, this case it's my NAS Server, share name data and a folder underneath called /backup/LibreElecPi/

    NOTE:

    It would be smart and time saving to test this mount line, by pasting it on its own into the console to see if it correctly maps your destination SMB share!

    You can test by going to

    cd /tmp/BackupDestination

    Make a folder in here, then check on your server to see if it appears.

    mkdir test

    I initially had lots of dififculty figuring out how to map SMB with linux, so test it before finishing your script.


    The next line in the script is the actual backup, this will take at least 30 minutes to run and the Pi will be basically unusable, it's best run at midnight for example or 3am etc. (more on schedule shortly)

    Finally, we unmount the share.

    Now to make the backup actually "runable" simply type "chmod +x backup.sh"

    To test your backup works simply type:

    ./backup.sh

    This should take a long time, when it's done, you'll have a file on your server, in my case 1.5GBytes, about an hour

    Scheduling:

    Finally, we schedule the backup using the linux schedule tool cron / crontab

    You'll want to generate an exact line for your schedule.

    Go to Crontab Generator - Generate crontab syntax and define a schedule by clicking the desired entrypoints.

    You will also need to paste in the actual command to run, which is simply

    /storage/backup.sh

    Then click generate, you'll end up with a weird looking line.

    Here's mine

    * 1 * * 6 /storage/backup.sh >/dev/null 2>&1

    That's gonna backup at 1am on the 6'th day of the week (Saturdays)


    Here's one which will backup at 6:45am, Monday to Friday

    45 6 * * 1-5 /storage/backup.sh >/dev/null 2>&1

    Cron is pretty weird / complicated, you don't need to learn the little numbers, just use the generator to make your desired schedule.

    Once you have your desired cron entry, type the following to edit the cron schedule

    crontab -e

    Simply paste in the line from the generator and hit CTRL-O to write out (save) your file.

    You're done.

    Whenever your schedule is configured, your Pi will backup the entire SDCard, across the network, to a share.

    If the SDCard wears out / corrupts or you just screw the settings up really badly, you can just install Win32DiskImager, open the gzip file and write a new SDCard. Put it in the Pi and it'll be like nothing happened.

    I hope this helps users with the simpler, 'fire and forget' systems on SDCard

  • Before dd stop kodi at least.

    Code
    systemctl stop kodi
    dd ......

    Also you could make one temp file and fill it with zeros. After whole space is used remove it and run dd. Then final size of the backup should be smaller.

  • Before dd stop kodi at least.

    Code
    systemctl stop kodi
    dd ......

    Also you could make one temp file and fill it with zeros. After whole space is used remove it and run dd. Then final size of the backup should be smaller.


    What's your suggestion on how to do that? I'm happy to impliment!