Raspberry Pi – File storage backup

Introduction

This article describes briefly my setup of an automatic backup procedure for my file storage I have connected to my Rasperry Pi 3 Model B. The file storage as well as the backup storage are external hard drives. Both are connected to the Pi over a  USB port I bought on Amazon to properly supply them with current.

Hard drive configuration

Both hard drives are configured in /etc/fstab:

UUID=7db942b8-19a0-4118-9aef-c217cbe7fc8a /media/dlna_drive ext4 defaults,nofail,x-systemd.automount,x-systemd.device-timeout=5 0 0
UUID=e87b9ca7-d6d9-4a3d-913f-cf7710309e4f /media/backup_drive ext4 defaults,nofail,x-systemd.automount,x-systemd.device-timeout=5 0 0

In addition both hard drives are managed with hd-idle. The /etc/default/hd-idle config file is configured like this:

START_HD_IDLE=true
HD_IDLE_OPTS="-i 0 -a /dev/disk/by-uuid/7db942b8-19a0-4118-9aef-c217cbe7fc8a -a /dev/disk/by-uuid/e87b9ca7-d6d9-4a3d-913f-cf7710309e4f -i 900 -l /var/log/hd-idle.log"

Hd-idle and how to mount a hard drive are already covered by my Minidlna article.

Backup configuration

Create a file /usr/local/sbin/backup_dlna_drive.sh:

#!/bin/bash
SOURCE_DIR=/media/dlna_drive/
DESTINATION=/media/backup_drive/

echo "Start backup: $(date)"

rm -rf "$DESTINATION/backup.dlna_drive.3"
mv "$DESTINATION/backup.dlna_drive.2" "$DESTINATION/backup.dlna_drive.3"
mv "$DESTINATION/backup.dlna_drive.1" "$DESTINATION/backup.dlna_drive.2"
cp -al "$DESTINATION/backup.dlna_drive.0" "$DESTINATION/backup.dlna_drive.1"
rsync -a --delete $SOURCE_DIR "$DESTINATION/backup.dlna_drive.0/"

echo "Backup finished: $(date)"

and make it executable:

root@raspberrypi:/usr/local/sbin# chmod 755 backup_dlna_drive.sh

Finally add an entry into crontab of root user:

0 2 1 * * /usr/local/sbin/backup_dlna_drive.sh >> /var/log/backup.dlna_drive.log 2>&1

Conclusion

This article described briefly my automatic backup procedure in place on my Pi. The backup is started by crontab at the beginning of every month and it keeps up to 3 month of snapshots. By having hd-idle in place the hard disks are only spinning in case they are needed. This is good for both hard disks connected as it will increase their lifetime dramatically.