[[fester112:additional_configbackup]]

Backing Up The FreeNAS Configuration File Nightly Using A Cron Job

This is entirely optional.

It is recommended that you have email notifications setup on the FreeNAS server before embarking on this subsection.

This is a guide for creating a Cron job to back-up the FreeNAS configuration file each night.

This file can be used for recovery purposes should your FreeNAS server encounter a problem of some sort (bloody ferrets!), so this is well worth doing.

The first thing to do is to create a dataset within which we can store the nightly backup of the FreeNAS configuration file. This will keep things neat and tidy.

Go to the “Storage” page.

Select “Tank1” or whatever you called the volume (1) by clicking on it (it should turn blue when selected).

A series of buttons should appear on the bottom of the screen.

From these buttons click on one that creates a dataset (2).

A new smaller window will pop up for creating the dataset.

  • In the “Dataset Name:” text box (1) give the share a name (because this is a backup dataset, Fester used NightlyBackup).
  • Leave the “Compression level:” drop down selection box (2) set to lz4.
  • Set the “Share type:” to whatever suits the type of clients on your network (Fester has mainly Windows machines so I set this to Windows).
  • Leave the “Case Sensitivity:” drop down selection box and “Enable atime:” at their default settings as shown (4).
  • “ZFS Deduplication:” should be set to off in the drop down selection box (5) unless you understand this and you have plenty of memory.
  • Now click the “Add Dataset” button (6).

The dataset will now be created and you should see something like this.

Remain on this screen and select the newly created dataset (1) if it is not selected already (in Fester’s case this was NightlyBackup).

Now click on the change permissions button (2).

A new window will pop up for changing the permissions of the new dataset.

I did not need to change any of the settings from their default value (1).

Now click the “Change” button (2).

Do not set the user and group to any of those you use for shares. This would be unwise. Only the root user and wheel group should be allowed to access this particular share.

We now need to create a file in the volume directory (in Fester’s case this is Tank1).

Open up an SSH session in PuTTY and log in as the root user. You should see a screen something like this.

We now need to navigate to the volume directory by typing in the following command into the command prompt. Don’t forget to hit the “Return/Enter” key to execute the command.

cd /

You should now see a screen something like this.

Now type into the command prompt the following command.

cd mnt

You should see a screen something like this.

Now type in the following command at the command prompt to see your volume’s name.

ls

You should see a screen that looks something like this.

The name of the volume will be revealed at this point (in Fester’s case it is the blue text “Tank1”).

Now type into the command prompt the following command with your volume name. The volume name is case sensitive so make sure you observe this when typing in the command.

cd YourVolumeNameHere

In Fester’s case the command would look like this:

cd Tank1

You should see a screen like this.

We now need create an empty file in this directory. You can call this file anything you like but remember its name as you will need it later.

At the command prompt type the following command (1).

touch YourFileNameHere.sh

In Fester’s case the command looked like this.

touch bkpconfig.sh

Now type in the following command to confirm the file was created (2).

ls

If all has gone well you should see the file listed in the SSH window (3).

We now need to edit the file. At the command prompt type in the following command.

edit YourFileNameHere.sh

In Fester’s case this command would look like this.

edit bkpconfig.sh

If all goes well you should see a screen like this.

We now need to put in the text line that will run each evening when the Cron Job is activated.

Type into the edit window the following line of text (this is all one line).

cp /data/freenas-v1.db /mnt/YourVolumeNameHere/YourDatasetNameHere/`date +%Y%m%d`.db

So in Fester’s case this command would look like this.

cp /data/freenas-v1.db /mnt/Tank1/NightlyBackup/`date +%Y%m%d`.db

If you want the FreeNAS version tagged on to the backup file names then use this command instead. This is all one one line; do not press the Enter key to insert a line break:

cp /data/freenas-v1.db /mnt/YourVolumeNameHere/YourDatasetNameHere /.scripts/ConfigBackups/`date %Y%m%d`_`cat /etc/version | cut -d'-' -f2`_`cat /etc/version | cut -d'-' -f4`.db

(Please note the “`” character is not an apostrophe. This character on my keyboard is found at the top left hand side under the “Esc” key. Your keyboard may be different.)

When you are done the edit screen should look something like this.

Now hit the “Esc” key.

You should be presented with a series of options at this point.

Press the “c” key or navigate to the c option using the “↑↓” keys and press the “Return/Enter” key.

Now press the “c” key again or navigate to the c option using the “↑↓” keys and press the “Return/Enter” key.

The text line in the editor will now be saved to the file.

Press the “Esc” key again.

Now press the “a” key or navigate to the a option and press the “Return/Enter” key.

This will take you out of the editor and return the command prompt.

You should see a screen something like this.

Now type the following command to leave the SSH console.

exit

Now go to the FreeNAS GUI and log in if needed.

Go to the “Tasks” page.

Click on the “Cron Jobs” button (1) if it is not selected already.

Now click on the “Add Cron Job” button (2).

A new window will pop up that should allow you to configure the Cron job.

In the “User:” drop down selection box (1) chose root as the user.

In the “Command:” text box (2) type in the following command.

sh /mnt/YourVolumeNameHere/bkpconfig.sh

So in Fester’s case this would look like this.

sh /mnt/Tank1/bkpconfig.sh

In the “Short description:” text box (3) give the Cron job a meaningful name.

Fester wants this Cron job to run every day, of every month at midnight (if you run this Cron job at midnight while repeating a special incantation that only certain SysAdmin’s know it will give your FreeNAS system the ability emulate a Sinclair ZX Spectrum when there is a full moon!).

To run the Cron job every day at midnight set the “Each selected minute” setting of the “Minute:” section to 00 (4).

Set the “Each selected hour” of the “Hour:” section to 00 (5).

Now scroll down.

In the “Every N day of month” setting of the “Day of month:” section set this to 1 (6).

Put a tick next to every month in the “Month:” section (7).

Now scroll down.

Put a tick next to every day in the “Day of week:” section (8).

Fester leaves the “Redirect Stdout:” and “Redirect Stderr:” at their default values as I don’t know what they do. The “Enabled:” tick box needs to be ticked (9).

Now click the “OK” button (10).

If all goes well you should see an entry for the newly created Cron job. It should look something like this.

We now need to test that the Cron job actually works.

Select the newly created Cron job by clicking on it (it will turn blue when selected) (1).

Now click the “Run Now” button (2).

If this worked then a file should have been created in the dataset you made for this (in Fester’s case this was the “NightlyBackup” data set). We now need to go and check the file was created.

Open up an SSH session in PuTTY and log in as the root user. You should see a screen something like this.

We now need to navigate to the dataset you created to hold the nightly backups by typing in the following command into the command prompt. Don’t forget to hit the “Return/Enter” key to execute the command.

cd /

You should now see a screen something like this.

Now type into the command prompt the following command.

cd mnt

You should see a screen something like this.

Now type in the following command at the command prompt to see your volume’s name.

ls

You should see a screen that looks something like this.

The name of the volume will be revealed at this point (in Fester’s case it is the blue text “Tank1”).

Now type into the command prompt the following command with your volume name. The volume name is case sensitive so make sure you observe this when typing in the command.

cd YourVolumeNameHere

In Fester’s case the command would look like this.

cd Tank1

You should see a screen like this.

Now type in the following command at the command prompt to see your dataset’s name.

ls

You should see a screen that looks something like this.

The name of the dataset will be revealed at this point (in Fester’s case it is the blue text “NightlyBackup”).

Now type into the command prompt the following command with your dataset name. The dataset name is case sensitive so make sure you observe this when typing in the command.

cd YourDatasetNameHere

In Fester’s case the command would look like this.

cd NightlyBackup

You should see a screen like this.

Now type in the following command at the command prompt.

ls

You should see a screen showing a file with the date for its name starting with the year, then the month and then the day. If you get something that resembles the following then it has worked.

So the “20160517.db” file in the screen shot was created on the 17/05/2016.

That’s the nightly backup of the FreeNAS configuration file done.

  • fester112/additional_configbackup.txt
  • Last modified: 2017/06/24 16:24
  • (external edit)