logotype

How to create a NAS with Ubuntu and USB External HDDs

 

I have been playing around with a few ideas on how to best create a low powered NAS. Initially I was planning to go with a Raspberry Pi but conceded that I already have a small Ubuntu server running anyway, so I should use the available resources. Most of the following guide is 100% transferable to a RPi so this document will be useful in future for me.

The Hardware:

  1. Lower Powered Asus Pizza Box
  2. USB Powered USB Hub (if your using a RPi then I suggest a mains Powered USB Hub)
  3. An old 160GB WD external HDD (which will be added to in future)

 

40,000ft Overview:

  1. Make a folder
  2. Mount the HDD to that folder
  3. Share the folder out using Samba

 

The Details:

First thing to do is mount plug it all in switch on the HDD and then mount it.

Create a folder which will represent the mounted hard drive:

sudo mkdir /media/wd1

Now find out the name of the HDD:

sudo fdisk -l

Then try mounting the HDD to the folder we created with:

sudo mount -t ntfs /dev/sdb1 /media/wd1

 

Now that the HDD is mounted we want to share it with our network, to do this we will use Samba. The following commands must be done as root:

sudo su

Install Samba:

apt-get update
apt-get install samba

We now have to set a password for our user in Samba as it uses a separate password than the system account. Note there must be a system user in existence already for the following to work.

smbpasswd -a <username>

Make a copy of the samba config to your home folder in case of a mistake:

sudo cp /etc/samba/smb.conf ~

Edit the samba config

pico /etc/samba/smb.conf

Add this to the end of the file:

[wd1]
path = /media/wd1
available = yes
valid users = <username>
read only = no
browsable = yes
public = yes
writable = yes

Now to restart the Samba server:

restart smbd

Check the config for any mistakes:

testparm

Now to access the HDD from the network on a windows machine use the following:

\\Host_IP_Or_Name\wd1

You will be prompted for your username and password (as defined by Samba)

 

 

Backup Apache2 Access Logs in Ubuntu

This is a followup post to my CRON backup post. To backup the access (and error) logs of apache2 there are a couple of extra steps needed. Primarily moving the log files before zipping and gracefully restarting Apache to release the read lock on the current log files.

 

To backup the apache access logs on Ubuntu you can use the following script:

 

#!/bin/sh
####################################
#
# Backup to NFS mount script.
#
####################################

# What to backup.
backup_files="/var/log/apache2/oreillyit.com-access_log"

# Where to backup to.
dest="/var/www/vhosts/oreillyit.com/logbackups"

# Create archive filename.
thedate=$(date +%Y-%M-%d)
hostname=$(hostname -s)
archive_file="$hostname-$thedate.tgz"

# Print start status message.
echo "Backing up $backup_files to $dest/$archive_file"
date
echo

mv $backup_files $backup_files$thedate
#Gracefully restart apache apachectl graceful
sleep 600


# Backup the files using tar.
tar czf $dest/$archive_file $backup_files$thedate

# Print end status message.
echo
echo "Backup finished"
date

# Long listing of files in $dest to check file sizes.
ls -lh $dest

 

If this was of interest, then so might my article on using cron to run the backup script automatically

Re-mapping the keyboard on a Raspberry Pi

 

When I first started programming with my Raspberry Pi I found the keyboard layout was set to UK. This meant my # key (Shift+3) was a set to the pound symbol £.

To fix simply run the following from the command line:

sudo dpkg-reconfigure keyboard-configuration

Follow the prompts. Then restart your Raspberry Pi.

The changes wont take effect until after you relog into the Pi

Using CRON to backup files in Ubuntu

 

With all the the fun and games I have been having playing with Ubuntu servers recently to host and manage websites and mail, it's probably time that I organized some proper form of backup. For the different servers I did things a little differently, one uses rsync to send the backups to a different machine, one has a USB drive mounted to store the backups. However for this guide I am simply going to explain how to write a shell script and get it running once a week with cron.

 

Step 1: Write the script to backup

First we need to create the .sh script, in general it can be stored in /usr/local/bin/ and I have called this one backup.sh. So open your favorite text editor and get to it!

sudo pico /usr/local/bin/backup.sh

The following script backups up the home directiory, mail, etc, root, boot, opt and www - so reasonably complete. Feel free to paste and change the code from my file:

#!/bin/sh
####################################
#
# Backup to NFS mount script.
#
####################################

# What to backup.
backup_files="/home /var/spool/mail /etc /root /boot /opt /var/www"

# Where to backup to.
dest="/bkup"

# Create archive filename.
thedate=$(date +%Y-%M-%d)
hostname=$(hostname -s)
archive_file="$hostname-$thedate.tgz"

# Print start status message.
echo "Backing up $backup_files to $dest/$archive_file"
date
echo

# Backup the files using tar.
tar czf $dest/$archive_file $backup_files

# Print end status message.
echo
echo "Backup finished"
date

# Long listing of files in $dest to check file sizes.
ls -lh $dest


We have placed in a few feedback calls so we can see it operate when we call it manually. To do so type the following:

 bash backup.sh

As you can see we have one file in there that was run by the current script (and four that were run by earlier scripts)

 

Step2: Run the script manually from CRON

So that is all well and good but we don't want to have to remember to run the script ourselves every week/day/hour etc. For that we will use CRON

To get it running its as simple as opening up the cron tab

sudo crontab -e

and adding the following line:

0 0 * * 1 bash /usr/local/bin/backup.sh

Explanation:

m          h      dom               mon    dow             command
Minute, Hour, Day of Month, Month, Day of week, the command to be run

So for ours we are running the command on minute 0, hour 0  for any day of the month, for any month on the first day of each week :-)

We now have a automatically running weekly backup of our site!

How to install the Edimax EW-7811UAn Wifi Adapter on the Raspberry Pi

 

Recently I got myself my first Raspberry Pi Model B and they are Amazing little devices for around AUD$38.

To setup an Edimax EW-7711UAn Wifi adapter simply:

sudo pico /etc/network/interfaces

This is just a copy if my interfaces file:

auto lo

iface lo inet loopback
iface eth0 inet dhcp

allow-hotplug wlan0
#iface wlan0 inet manual
#wpa-roam /etc/wpa_supplicant/wpa_supplicant.conf
#iface default inet dhcp

iface wlan0 inet dhcp
wpa-ssid <Wifi SSID>
wpa-psk <The wifi password>


Save the file:

Ctrl + O

Exit

Ctrl + X


Reboot the Pi:

sudo shutdown -r now
Copyright 2019 OReillyIT. All rights reserved.