Backup your datas automatically on a NAS (synology)

Here I explain how I have configured my workstation to backup my data automatically each night.

The principle of the automatic backup

Use rsync through a bash script and a crontab to make a backup (mirror) of specific directories on a Network Attached Storage (NAS).

Requirement for the backup

  • Debian / Linux on your workstation
  • rsync
  • A NAS (I have a Synology Ds411 slim)
  • A network configuration that works (not detailed here)!

Preparation

Configuration of the NAS

First, you must allow SSH connection in the setup of your NAS as described here.
Create a user on the NAS (Config Panel > User > Create) and give him write access to his directory.
Tips: since DSM 4.1, check that the user have write access to ‘homes’ directory or you will have a read-only error.

Create the script

Connected as root, create a directory backup/ in the root directory and create an empty bash script file.

# cd /root
# mkdir backup/
# touch snapshotHome.sh

Copy and adapt the script snapshotHome.sh (see bellow).

Exclude some subdirectories from backup

create a file excludeHome.lst in /root/backup/ and fill it with the directories you DO NOT want to be backuped.

lost+found
down
tmp
.cache
.thumbnails
...

Automatize the backup

To automatize your backup each night, just use a crontab task on your workstation (under root)

# crontab -e
Ex to execute the backup each night at 3:35 am, use this crontab
# 35 3 * * * /root/backup/snapshotHome.sh

Check logfile

Logs are written in /root/backup/snapshot-date-of-backup.log

Backup Script (snapshotHome.sh)

#!/bin/bash
# Backup the LOCALDIR
# on a distant server through SSH
# Execute the script as Root
#
# change with your local dir
LOCALDIR=/home/YOUR-LOCAL-DIR/
# change with your distant dir
SERVERDIR=/volume1/homes/YOUR-DISTANT-DIR
SERVERPATH=$(hostname)_snapshot
# change with your user ssh
SERVERUSER=YOUR-USER-SSH
# change with your server nameserver
SERVER=diskstation.xrousse.lan
EXCLUDE='/root/backup/excludeHome.lst'

NOW="$(date +"%Y-%m-%d")"
LOGFILE="/root/backup/snapshotHome-$NOW.log"

rsync -av \
--stats \
--del \
--backup \
--backup-dir=$SERVERDIR/$SERVERPATH/deleted/ \
--progress \
--log-file=$LOGFILE \
--exclude-from $EXCLUDE \
$LOCALDIR \
-e ssh $SERVERUSER@$SERVER:$SERVERDIR/$SERVERPATH/homedir
GD Star Rating
loading...
Backup your datas automatically on a NAS (synology), 5.0 out of 5 based on 6 ratings