Personal File Server on Raspberry Pi
Jan 20, 19Synopsis
I recently completed setup of my personal file server running on raspberry pi. For beginners, NextCloud is a personal, free alternative to services such as icloud or dropbox. I will walk you through the steps involved in this setup and my overall experience with this tool.
Introduction
Hardware Requirement
- Raspberry PI 3 - 1
- Micro SD Card - 1
- External hard drive - 2
- USB Micro power supply - 1
Software Requirement
- Raspberry Setup - Follow these instructions to setup Raspberry Pi
Setup
We need to ensure that the external harddrive is always mounted to the same folder location under /mnt Firstly, we need to gather the UUID of the external hard drive. Run following command to output this information
lsblk --fs
Output
sda
+-sda1 ntfs easystore E81EB2EA1EB2B0C4 /mnt/hd_homesink
sdb
+-sdb1 ntfs easystore DA88A08288A05F2F /mnt/hd_nextcloud
Make a note of the UUID and make an entry into the /etc/fstab as follows. This will ensure that everytime the raspi is booted it will mount in the same location
File: Make you following entry in /etc/fstab
UUID=E81EB2EA1EB2B0C4 /mnt/hd_homesink ntfs async,big_writes,noatime,nodiratime,nofail,uid=1000,gid=1000,umask=007 0 0
UUID=DA88A08288A05F2F /mnt/hd_nextcloud ntfs async,big_writes,noatime,nodiratime,nofail,uid=1000,gid=1000,umask=007 0 0
Create this shell script to backup the content from one external drive to another File: nextcloud_backup.sh
rsync -aPv /mnt/hd_nextcloud/data /mnt/hd_homesink/nextcloud_backup/
File: system_backup.sh
#!/bin/bash
#
# Automate Raspberry Pi Backups
#
# Kristofer Källsbo 2017 www.hackviking.com
#
# Usage: system_backup.sh {path} {days of retention}
#
# Below you can set the default values if no command line args are sent.
# The script will name the backup files {$HOSTNAME}.{YYYYmmdd}.img
# When the script deletes backups older then the specified retention
# it will only delete files with it's own $HOSTNAME.
#
# Declare vars and set standard values
backup_path=/mnt/hd_homesink/pi1
retention_days=1
# Check that we are root!
if [[ ! $(whoami) =~ "root" ]]; then
echo ""
echo "**********************************"
echo "*** This needs to run as root! ***"
echo "**********************************"
echo ""
exit
fi
# Check to see if we got command line args
if [ ! -z $1 ]; then
backup_path=$1
fi
if [ ! -z $2 ]; then
retention_days=$2
fi
# Create trigger to force file system consistency check if image is restored
touch /boot/forcefsck
# Perform backup
dd if=/dev/mmcblk0 of=$backup_path/$HOSTNAME.$(date +%Y%m%d).img bs=1M
# Remove fsck trigger
rm /boot/forcefsck
# Delete old backups
find $backup_path/$HOSTNAME.*.img -mtime +$retention_days -type f -delete