Introduction
This Blog records the configuration of a NAS server using Ubuntu 16 Server. This server only has a few functions:
- Ensuring a clean shutdown on loss of power, with a UPS monitor
- Providing a File system share / server, using samba
- Providing a UPNP Media Server, using mediatomb
- Providing centralised backups, using backuppc
- Sharing selected folders with Google File Share
- Other miscellaneous tools and scripts
Configuration
Disk Configuration
Once booted, the disks can be configured. I map two drives: one for the data/media server, and one for backups. I prepare the /data directory to receive these disks (for me, they are external USB disks).
My internal disk is a solid state device, and as such, I use the ext2 filesystem. Filesystems such as ext4 are journaling and have an increased amount of writes.
Directory Location Type Size Description / SSD ext2 40G Root Partition /tmp SSD ext2 10G Temp Partition /opt SSD ext2 60G Local Applications Partition /home SSD ext2 20G User Home Directories /data/backup USB ext4 1T Backup Files /data/data USB ext4 1T Server FilesNote: USB Disks (backup, data and media) are mounted with the noauto option.
The SSD disks are configured during the install.
The USB disks are configured afterwards.
The USB disks are configured using 'cfdisk', and then formatted using mkfs.
The disks are identified usint their UUID, which can be found with the blkid command (run as root).
This information is used to populate the /etc/fstab file. fstab The /etc/fstab file contains the disk identifiers and their targetted mount points.
The disks are referred to by UUID, rather than /dev device name which enables them to be plugged in in any order into any USB port. The noauto option on the USB disks prevents them from holding up the boot process.
The disks are later mounted using the rc.local script.
# mkdir /data/backup ; touch /data/backup/DISK-NOT-MOUNTED ; chmod a-w /data/backupSee the Initialisation script section below for disk mounting (and why the dummy files have been created)
# mkdir /data/data ; touch /data/data/DISK-NOT-MOUNTED ; chmod a-w /data/dataInitialisation Script
The /etc/rc.local initialisation script is responsible for mounting the USB disks and then launching the appropriate servers. The mount points contain a single file (DRIVE-NOT-MOUNTED), which are hidden if the mount is successfully achieved.
mount /data/backup || true mount /data/data || true if [ -f /data/backup/DRIVE-NOT-MOUNTED ]; then #Error echo "Error mounting backup" true || echo "/etc/rc.local, /data/backup mount problem" | mail -s "NAS Server Problem" rootelse # Launch Backup /etc/init.d/backuppc start fiif [ -f /data/data/DRIVE-NOT-MOUNTED ]; then #Error echo "Error mounting data" true || echo "/etc/rc.local, /data/data mount problem" | mail -s "NAS Server Problem" rootelse # Launch SAMBA /etc/init.d/smbd start # Launch Mediatomb /etc/init.d/mediatomb start fiexit 0This script launches the media and file system servers which use the USB drives (provided they mounted correctly). In order to stop the system launching the applications before the drives are mounted, the appropriate /etc/init.d scripts are edited to remove the start runlevels, and the actual start scripts themselves are removed from the filesystem.
# vi /etc/init.d/mediatomb # vi /etc/init.d/samba # vi /etc/init.d/backuppc # rm /etc/rc?.d/S??mediatomb # rm /etc/rc?.d/S??samba # rm /etc/rc?.d/S??backuppcApplication Installation
UPS Monitor
This program monitors a UPS connected via a USB interface, and forces a controlled shutdown on loss of power.
In my case, the UPS is produced by UPC, and the configuration is as simple as installing the monitor application, and plugging in the USB cable.
# apt-get install apcupsd
# vi /etc/apcupsd/apcupsd.conf
...You can monitor the health of the UPS with the apcaccess command, or by viewing the /var/log/apcupsd.events file.
UPSCABLE usb
UPSTYPE usb
BEEPSTATE N
...
Samba Server
Install Samba using apt-get, and configure by editing smb.conf. The configuration should add the data disk share.
Do the last stage for each remote linux machine (windows machines will use samba).
On the Main Configuration Editor, Select Hosts, and add all of the hosts you wish to backup. Make sure you use the same names you put in the /etc/hosts file
Select the hosts from the drop-down menu on the screen, and select Edit Config / Xfer.
For Linux Machines, select the transfer method as rsync, and then simply add the paths you wish to backup to the RsyncShareName, e.g. /home or /etc. Don't forget to Save.
For Windows Machines, share the folders you wish to backup (you can share read-only if you wish).
In backuppc, you will need to select smb as the method, and include the names of the shares you wish to backup. Don't forget to add the username and password for the share.
Depending on the share, you may get errors when backing up, because files are locked. I exclude the ntuser.* files, and the AppData directory with the Exclude feature:
# apt-get install samba
# vi /etc/samba/smb.conf
[data]Create users:
comment = Data
path = /data/data
browseable = yes
read only = no
guest ok = no
create mask = 0660
force create mask = 0660
directory mask = 02770
force directory mask = 02770
# useradd -u 1000 -U -m -s /bin/bash -c "User Name" username
# passwd username
# echo "username = username" >> /etc/samba/smbusers
# smbpasswd -a username
BackupPC Server
Backuppc is an application which pulls backups, rather than clients pushing them. It is configured through a web interface, through which backups and restores are managed. There is a command line application which can be used to uncompress files - a soft-link to /usr/bin/bpczcat will make it easier to access this program if needed.
Installing
Install, move the backup data disk to the new drive, create a link to the usefule zcat program, used to uncompress individual files, and set the backuppc user admin password for the web interface.# apt-get install backuppc
# mv /var/lib/backuppc /data/backup
# ln -s /data/backup/backuppc /var/lib/backuppc
# ln -s /usr/share/backuppc/bin/BackupPC_zcat /usr/bin/bpczcat
# htpasswd /etc/backuppc/htpasswd backuppc
Static Hosts
For the Linux machines, and preferably all, it is desirable that they are configured with static IP addresses. The best way to centrally do this is to configure your modem to fix the addresses when allocated (DHCP) - look for a bind IP address to MAC feature.
If you have a local Domain Name Server configuration feature, you can also allocate the names of the machines.
If you don't have a local DNS, you will need to add each of the backup client's details to the /etc/hosts file on the backup server, so that they can be referred to by name in the backuppc configuration.
# vi /etc/hosts
Enabling SSH on Remote Machines
On remote Linux machines, you will need to enable ssh so backups can take place.
If you are planning to back up system files or files from multiple users at the same time, you will need to enable it for root by enabling sshd_config.
remote# apt-get install ssh
remote# vi /etc/ssh/sshd_config
PermitRootLogin yes
Configuring SSH for Remote Backups
In order to log into the remote machines, you need to configure the backuppc user such that he can remote-login as root to machines, without needing a password. This is achieved with ssh public/private keys.Do the last stage for each remote linux machine (windows machines will use samba).
# su - backuppc
$ ssh-keygen
Generating public/private rsa key pair.
Enter file in which to save the key (/home/backuppc/.ssh/id_rsa):
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
$ cat ~/.ssh/id_rsa.pub | ssh root@localhost 'cat >> .ssh/authorized_keys'
$ cat ~/.ssh/id_rsa.pub | ssh root@remotehost 'cat >> .ssh/authorized_keys'Note that if you are intending to backup files for a specific user, you need to use that username instead of root in the ssh command, and use that same username instead of root in the backuppc configuration.
Configure BackupPC
In a web browser, connect to http://nasserver/backuppcOn the Main Configuration Editor, Select Hosts, and add all of the hosts you wish to backup. Make sure you use the same names you put in the /etc/hosts file
Select the hosts from the drop-down menu on the screen, and select Edit Config / Xfer.
For Linux Machines, select the transfer method as rsync, and then simply add the paths you wish to backup to the RsyncShareName, e.g. /home or /etc. Don't forget to Save.
For Windows Machines, share the folders you wish to backup (you can share read-only if you wish).
In backuppc, you will need to select smb as the method, and include the names of the shares you wish to backup. Don't forget to add the username and password for the share.
Depending on the share, you may get errors when backing up, because files are locked. I exclude the ntuser.* files, and the AppData directory with the Exclude feature:
Mediatomb UPNP Media Server
Email Forwarding
Edit /etc/postfix/main.cf, and make sure they your domain and hostname are not included in the mydestinations string.
Add a generic map, which translates local addresses to internet-recognised ones (this will prevent a mail relay from rejecting a message because it's domain is not recognised).
Set the relayhost to that of your ISP.
You can automate the backups by adding a cron job to re-synchronise regularly:
Add a generic map, which translates local addresses to internet-recognised ones (this will prevent a mail relay from rejecting a message because it's domain is not recognised).
Set the relayhost to that of your ISP.
# vi /etc/postfix/main.cf
smtp_generic_maps = regexp:/etc/postfix/sender.mapAdd the mapping that translates the from address from xxx@nas to xxx.nas@mydomain.com
myhostname = nas
mydestination = localhost.localdomain, localhost
relayhost = [mailhost.zen.co.uk]:25
# vi /etc/postfix/sender.map
/^(.*)@(.*)$/ ${1}.${2}@mydomain.com
# postmap /etc/postfix/generic
Restart Postfix
# /etc/init.d/postfix restartThese changes cover the translation of the 'To' and the 'From' headers in an email message, and all mail is passed via the relayhost.
Google File Share
In order to share a folder with Google Drive, you need to download and configure the grive application.
# apt-add-repository ppa:nilarimogard/webupd8Log into Google through a web browser (this need not be on the server), then, on the server:
# apt-get update
# apt-get install grive
$ mkdir /data/data/DriveFollow the on-screen instructions to grant permissions to the share, and perform the initial synchronisation.
$ cd /data/data/Drive
$ grive -a
You can automate the backups by adding a cron job to re-synchronise regularly:
$ mkdir /data/data/Drive /data/data/Drive/bin
$ echo "cd /data/data/Drive ; grive" > /data/data/Drive/bin/drivesync
$ chmod u+x /data/data/Drive/bin/drivesync
$ crontab -e
5,25,45 * * * * /data/data/Drive/bin/drivesync
Or you can get a bit more adventurous by using inotify to monitor file activity, and only triggering an update if something changes.


