Automatic backup to a Windows share

July 08, 2011 • Administrator       
Synopsis

This article explains how to set up CensorNet Professional to automatically back-up to a networked Windows share

Article

NOTE: For this wiki, I'm using a machine called "adserver" which has a drive shared as "Public" and a windows user called "joe" with a password of "password123". You need to substitute this with your own details.

First of all, we need to install a couple of packages to help facilitate this process. To do this, we need log into the censornet command line as "root" and do:

apt-get update
apt-get install smbfs smbclient

Once this is complete, you then need to test that the censornet server can see the share. To do this, we need to do:

smbclient -L adserver -U joe

(Remember to substitute your own details). This should produce an output similar to:

Sharename       Type      Comment
---------       ----      -------
C$              Disk      Default share
IPC$            IPC       Remote IPC
ADMIN$          Disk      Remote Admin
Public          Disk
SYSVOL          Disk      Logon server share
NETLOGON        Disk      Logon server share

With a number of other items tagged on. The share we want in this case is one set up on the fileserver as "Public". As you can see, the censornet machine can see this share.

We need to set this share up so that it mounts on boot. In order to do this, we create a mount point, then edit the file that manages the filesystems on boot. Follow these instructions:

mkdir /mnt/win
nano /etc/fstab

Create a new line at the bottom of this file and add the following (remembering to substitute your own details):

//adserver/Public /mnt/win smbfs defaults,user,auto,username=joe,password=password123 0 0

Save this by doing Ctrl-O and exit by doing Ctrl-X. Now reboot the machine using the reboot command.

Once the machine is back up, you need to test the share is mounted successfully. To do this, run:

touch /mnt/win/test

Check on the fileserver (not the censornet box) that an empty file has been created. You can delete this file once you've ascertained that its there.

If this fails and the file does not exist, try this alternative configuration in the /etc/fstab file:

//adserver/Public /mnt/win cifs defaults,user,auto,username=DOMAIN/username%password 0 0

Test again with the touch command.

Now that we know the mount point is live, we need to create the script to do the backup. This is an adaptation of the default script, so it will produce identical output. Do:

cd /usr/local/sbin
nano auto_backup.sh

This will open a blank file. Add the following to the file:

#!/bin/bash
DATE=`date +%Y-%m-%d`
FILENAME="censornet_db_backup_${DATE}.pgdump";
/bin/su -c "/usr/bin/pg_dump -F c censornet -f /tmp/${FILENAME}" postgres
/bin/cp /tmp/${FILENAME} '/mnt/win/'

Make sure you get this exactly as it is above or it simply won't work! Note the " ` " is normally on the key next to the number 1 and isn't a " ' " symbol. (You may want to copy and paste. You can do this from a putty window by right-clicking after selecting the text and copying).

Now we need to set this script to be executable:

chmod 700 /usr/local/sbin/auto_backup.sh

Next, test the script works. Run:

/usr/local/sbin/auto_backup.sh

And wait for it to complete. Now move over to the fileserver, and make sure that the file is there and contains binary data.

Once you've checked this is complete, we need to set this up to execute every night. Please make sure this doesn't coincide with the time you've selected for the automatic URL database updates.

In order to do this, we need to edit the crontab. Type:

nano /etc/crontab

Add the following line:

30 3    * * *     root    /usr/local/sbin/auto_backup.sh

This will set it to run at 3.30am. You can change this to suit your own requirements (see below). Save the file (Ctrl-O) and exit (Ctrl-X). This should now be set up to automatically backup the database to the fileserver share you selected every day.

To explain how cron processes actually work, please read this: http://en.wikipedia.org/wiki/Cron, however as an example adapting the above line, you could use:

Weekly on Sunday (at midnight):

0 0    * * 0     root    /usr/local/sbin/auto_backup.sh

Monthly on first day of the month (at midnight):

0 0    1 * *     root    /usr/local/sbin/auto_backup.sh

Related articles


Last modified on Thu, January 19, 2012 « Back