Backup config file every night automatically!

rplucker

Dabbler
Joined
Jul 10, 2016
Messages
30
Question:

Why would you make a cron job to run a script file instead of just making the cron job run the command itself?

Ex: Paste (in my case)

Code:
 cp /data/freenas-v1.db /mnt/Volume2/db-backups/`date +%Y%m%d%H%M`.db 


Under "command" in the cron job config. It seems to do the same thing for me
 

Spearfoot

He of the long foot
Moderator
Joined
May 13, 2015
Messages
2,478
Question:

Why would you make a cron job to run a script file instead of just making the cron job run the command itself?

Ex: Paste (in my case)

Code:
 cp /data/freenas-v1.db /mnt/Volume2/db-backups/`date +%Y%m%d%H%M`.db 


Under "command" in the cron job config. It seems to do the same thing for me
Not sure who you're asking... but I'll take a stab at answering.

Cron can be used to execute any command: programs, user scripts, etc. So if you can do what you want to do with a simple 'cp' command or any other single-line command, that works fine. But if you need to execute more than a single line of code, it's much easier to write a shell script. And it's easier to debug, too. :)
 
Last edited by a moderator:

Rudi Pittman

Contributor
Joined
Dec 22, 2015
Messages
161
Anyone know how to fix this error which started tonight after installing the latest updates:

cp: (d989edd)_edd).db is not a directory


Current line:
cp /data/freenas-v1.db /mnt/volume1/.ConfigBackups/`date \+%Y%m%d`_`cat /etc/version | cut -d'-' -f2`_`cat /etc/version | cut -c21-32`.db
 
Last edited:

Glorious1

Guru
Joined
Nov 23, 2014
Messages
1,211
I got a similar error sent by email:
Code:
cp: (d989edd)_.db is not a directory

I'm sure it's not a coincidence that the new version is build FreeNAS-9.10.1 (d989edd), but this command and all the quoting and piping and sed is beyond me. Cron command:
Code:
cp /data/freenas-v1.db /mnt/Ark/Jim/configbak/`date +%Y%m%d`_`cat /etc/version | cut -d'-' -f2`_`cat /etc/version | cut -d'-' -f4 | sed -e 's/ /_/'`.db
 
Last edited:

Rudi Pittman

Contributor
Joined
Dec 22, 2015
Messages
161
A friend looked at this for me and the following line:

Code:
cp /data/freenas-v1.db /mnt/volume1/.ConfigBackups/`date +%Y%m%d`_`cut -d' ' -f1 /etc/version|cut -d'-' -f2`.db


will give you a filename like:

Code:
20160807_9.10.1.db


It should also NOT break every time the /etc/version changes.
 

Glorious1

Guru
Joined
Nov 23, 2014
Messages
1,211

Kam

Dabbler
Joined
Mar 28, 2016
Messages
39
Where is difference between such own backups and following freenas feature?

Feature #4821 Automatic configuration backup

It seems that freenas automatically makes backup of config db to .system dataset, mounted under /var/db/system/configs-xxxx directory.
 

Spearfoot

He of the long foot
Moderator
Joined
May 13, 2015
Messages
2,478
Where is difference between such own backups and following freenas feature?

Feature #4821 Automatic configuration backup

It seems that freenas automatically makes backup of config db to .system dataset, mounted under /var/db/system/configs-xxxx directory.
@Kam, the issue you cited has to do with backing up the configuration database locally, whenever changes are made to the system.

What we're doing here is copying the FreeNAS configuration file off-machine for safekeeping. This allows for completely restoring an existing FreeNAS server if it should fail in any way, or burn in a house fire, or what have you.
 

Kam

Dabbler
Joined
Mar 28, 2016
Messages
39
@Kam, the issue you cited has to do with backing up the configuration database locally, whenever changes are made to the system.

What we're doing here is copying the FreeNAS configuration file off-machine for safekeeping. This allows for completely restoring an existing FreeNAS server if it should fail in any way, or burn in a house fire, or what have you.

Most of scripts posted here makes backup to local zfs pool.
But of course remote offsite backup makes sense. Unfortunately with this new feature it makes backup to .system dataset, which seems is not possible to replicate or rsync to second location using gui.
 
Last edited by a moderator:

Spearfoot

He of the long foot
Moderator
Joined
May 13, 2015
Messages
2,478
Most of scripts posted here makes backup to local zfs pool.
But of course remote offsite backup makes sense. Unfortunatelly with this new Feature it makes backup to .system dataset, which seems is not possible to replicate or rsync to second location using gui.
Doooh! You're right, these scripts also just copy the configuration file locally. I've configured my system to rsync mine off-machine shortly after this script runs. I got the two separate processes muddled.
 

Glorious1

Guru
Joined
Nov 23, 2014
Messages
1,211
Most of scripts posted here makes backup to local zfs pool.
But of course remote offsite backup makes sense. Unfortunately with this new feature it makes backup to .system dataset, which seems is not possible to replicate or rsync to second location using gui.
A. I have no idea how to get anything out of the system dataset. That's a black box to me. I know how to access my saved config files.
B. Assuming you back up your data, you are also backing up your locally saved config files. I don't expect the system dataset gets backed up when you back up your data datasets.
C. When I think of it, I click the System > General > Save Config button to save the config on my laptop. But yeah, it would be nice automate that. Or even better, save it to a cloud location.
 

CrossEye

Cadet
Joined
Oct 23, 2013
Messages
5
Wanted to share my code with everyone, it's a revised version of the original script running as a Cron job, except I removed cut for awk and removed all of the escapes and added a 90 day retention.

Code:
cp /data/freenas-v1.db /mnt/volume/backups/config-`hostname`-`cat /etc/version | awk -F'[- ]' '{print $2 "_" $3}'`-`date "+%Y%m%d%H%M"`.db && find /mnt/volume/backups/* -mtime +90 -type f -delete


Outputting: config-hostname.internal.domain.com-9.10.1_(d989edd)-201608250252.db

I wrote a post about it in further detail but the gist is the code above,

https://secure.crosseye.org/blog/freenas-automatic-config-backups/

Hope this helps and take care all!
 

CrossEye

Cadet
Joined
Oct 23, 2013
Messages
5
I was looking through some of the comments and noticed a concern for having the configs in a local volume. So with that in mind, you can SCP the configs to another server if you want with the following script.

Code:
scp /data/freenas-v1.db username@backup_server_fqdn:/mnt/volume/backups/config-`hostname`-`cat /etc/version | awk -F'[- ()]' '{print $2 "_" $4}'`-`date "+%Y%m%d%H%M"`.db


Outputting on remote server: config-hostname.internal.domain.com-9.10.1_d989edd-201608250252.db

Only thing is there will be no retention option using SCP vs a local copy but at least you have options now.
 

CrossEye

Cadet
Joined
Oct 23, 2013
Messages
5
I don't know what I was thinking but this will not work because you cannot send a password in this script and I do not believe FreeNAS allows us to use SSH with RSA keys.
 

Spearfoot

He of the long foot
Moderator
Joined
May 13, 2015
Messages
2,478
I don't know what I was thinking but this will not work because you cannot send a password in this script and I do not believe FreeNAS allows us to use SSH with RSA keys.
Huh?
 

CrossEye

Cadet
Joined
Oct 23, 2013
Messages
5

I should have worded that better, using SCP in a script will not work because you cannot pass the password to the command. So the script for using SCP will not work BUT the first script will for making a backup to a local volume.

Sorry for the confusion!
 

Spearfoot

He of the long foot
Moderator
Joined
May 13, 2015
Messages
2,478
I should have worded that better, using SCP in a script will not work because you cannot pass the password to the command. So the script for using SCP will not work BUT the first script will for making a backup to a local volume.

Sorry for the confusion!
Ah, now I understand! FreeNAS is based on FreeBSD, and you can set it up w/ RSA keys for password-less access via SSH. That ought to let you use SCP. Or if not, you could definitely use rsync; I do all the time.
 

Jacopx

Patron
Joined
Feb 19, 2016
Messages
367
Good morning, after the last update: (FreeNAS-9.10.1 (d989edd)) i have occurred in a problem with the automatic backup of my configuration... This is my script:
Code:
cp /data/freenas-v1.db /mnt/WDVolume_A/Data/Config_Backups/backups/`date \+%Y%m%d`_`cat /etc/version | cut -d'-' -f2`_`cat /etc/version | cut -c21-32`.db

And this is the error:
Code:
cp: (d989edd)_edd).db is not a directory

I sure that is something related to the name of the configbackup name but i can't solve it... Someone can help?
 
Last edited:

Spearfoot

He of the long foot
Moderator
Joined
May 13, 2015
Messages
2,478
Good morning, after the last update: (FreeNAS-9.10.1 (d989edd)) i have occurred in a problem with the automatic backup of my configuration... This is my script:
Code:
cp /data/freenas-v1.db /mnt/WDVolume_A/Data/Config_Backups/backups/`date \+%Y%m%d`_`cat /etc/version | cut -d'-' -f2`_`cat /etc/version | cut -c21-32`.db

And this is the error:
Code:
cp: (d989edd)_edd).db is not a directory

I sure that is something related to the name of the configbackup name but i can't solve it... Someone can help?
Yeah, I think the parentheses and/or spaces are causing problems. Try my script located earlier in this thread, it strips parentheses and replaces the spaces with a '-'.

EDIT: I've posted the script here on pastebin.
 
Last edited:
Top