Backup config file every night automatically!

cyberjock

Inactive Account
Joined
Mar 25, 2012
Messages
19,526
Got it figured out.. Added to the first post, and below:



If you want to do these commands as tasks that run from cron directly, you'll want to use the following commands, correcting for your locations as appropriate:

cp /data/freenas-v1.db "/mnt/tank/`date \+%Y\%m\%d`.db"

- or -

cp /data/freenas-v1.db /mnt/tank/`date \+%Y\%m\%d`_`cat /etc/version | cut -d'-' -f2`_`cat /etc/version | cut -d'-' -f4`.db

Seems weird, but the code seems to escape the first % symbol, but not subsequent % symbols. Not sure if that's a bug or not. Asked a dev if this is expected or not. But the two above examples worked perfectly for me.
 

Glorious1

Guru
Joined
Nov 23, 2014
Messages
1,211
That works for me running as an automatic direct cron command. That's some pretty deep Unix stuff. Thanks cyberjock!
 

2fi5ban

Cadet
Joined
Feb 2, 2015
Messages
3
I also would surely win the "biggest d*ck of the first quarter".. lol.

But thanks for the kind words. ;)
cyberjock now you are just bragging :p I do agree as you make for some very enjoyable and extremely informative reading. I am only through page 2 of your post (I think my wife is getting jealous :))
and thank you for this post it gave what I need to finish my configuration plan!!
 
Joined
Jan 21, 2015
Messages
18
Works like a charm. Thanks a lot!
 

Wolfeman0101

Patron
Joined
Jun 14, 2012
Messages
428
Got it figured out.. Added to the first post, and below:



If you want to do these commands as tasks that run from cron directly, you'll want to use the following commands, correcting for your locations as appropriate:

cp /data/freenas-v1.db "/mnt/tank/`date \+%Y\%m\%d`.db"

- or -

cp /data/freenas-v1.db /mnt/tank/`date \+%Y\%m\%d`_`cat /etc/version | cut -d'-' -f2`_`cat /etc/version | cut -d'-' -f4`.db

Seems weird, but the code seems to escape the first % symbol, but not subsequent % symbols. Not sure if that's a bug or not. Asked a dev if this is expected or not. But the two above examples worked perfectly for me.
Does this just keep the last version of the db? Is there a way to keep the last 5 let's say.
 

j_r0dd

Contributor
Joined
Jan 26, 2015
Messages
134
Does this just keep the last version of the db? Is there a way to keep the last 5 let's say.

You can always add another cron job to periodically clean up your backup directory for files older than 5 days.

Code:
 find /path/to/your/backups/ -mtime +5 -exec rm {} \; 
 
Last edited:

adrianwi

Guru
Joined
Oct 15, 2013
Messages
1,231
First off, thanks for the guide grumpy :D

I've got this running and for the last 4 nights (@ 23:59, just before CJ turns into a pumpkin) the config file is copied into a config folder.

And every morning I get an e-mail telling me

Code:
/mnt/APEpool1/temp/config/bkpconfig.sh: /etc/version: Permission denied


Pretty sure it's working - the files are 303kb which the same as a file taken from the GUI - just not sure why I'm getting the e-mail.

Any ideas?
 

joeschmuck

Old Man
Moderator
Joined
May 28, 2011
Messages
10,994
So if you wanted to copy your configuration and then delete all but the most recent 5 days (120 hours to be exact) all on the same CRON job:

Code:
cp /data/freenas-v1.db "/mnt/tank/`date \+%c`.db" && find /mnt/tank/ -mtime +5 -exec rm {} \;

Yes, I didn't do anything special, just joined the two commands where the first one must finish OK before the second one runs. I could have used a semicolon vice the double ampersands but then if the copy fails, you could run into a situation where you have some crap partial files and you are deleting the older good ones. It's personal preference. Also I didn't care for just a simple date code so I used a different format for the file name. Instead of "20150527.db" my file name is "Wed May 27 14:11:26.db". Of course you can customize your own name as well, it's easy enough. Also if lets say you ran this CRON more than once a day (lord knows why someone would) but it will not overwrite the same name file.

If you wanted to change this to a different value for the days being retained, just change the "+5" value to the number of 24 hour periods you would like. You can also specify it by number of weeks, this would give you 1 week (168 hours from the time you run the command)
Code:
cp /data/freenas-v1.db "/mnt/tank/`date \+%c`.db" && find /mnt/tank/ -mtime +1w -exec rm {} \;


I wanted to give you a true 5 backups and not 120 hours but my daughter is bugging me to make dinner so I have to stop now to ensure she doesn't starve.

EDIT: After thinking about this over night I thought to myself, why 5 days or 1 week, why not 30 days. Lets face it, by the time you realize something is wrong, it could be a long time. I'd change the deletion to allow you to maintain as many copies as you can handle. I'm going with 30 days for my system. Someone else might want to elect for 60 or 90 days. The files are not that large to store.
 
Last edited:

Nick McCloud

Cadet
Joined
Apr 4, 2015
Messages
3
Just a minor but critical typo in the 'cron with version' command string - missing ":

cp /data/freenas-v1.db "/mnt/tank/`date \+%Y\%m\%d`_`cat /etc/version | cut -d'-' -f2`_`cat /etc/version | cut -d'-' -f4`.db"

For some reason the Run Now button doesn't appear to run anything - I'll debug it further after lunch.
 

Spearfoot

He of the long foot
Moderator
Joined
May 13, 2015
Messages
2,478
Here's my script, which mimics the FreeNAS naming convention for the backup file (hostname+version+timestamp), and saves the file to a pool named 'tank':

Code:
#!/bin/bash

SRC_FILE=/data/freenas-v1.db

DEST_DIR=/mnt/tank
DEST_HOST=$(hostname -s)
DEST_VERSION=$(cat /etc/version)
DEST_DATE=$(date +%Y%m%d%H%M%S)

DEST_FILE=${DEST_HOST}-${DEST_VERSION}-${DEST_DATE}.db

cp ${SRC_FILE} ${DEST_DIR}/${DEST_FILE}


I configured it as a Cron job owned by root with this command line:
Code:
/bin/bash /mnt/tank/save-config.sh


It runs from the CLI and at the scheduled time in Cron -- but not when I use the Cron 'Run Now' button, as pointed out by Nick. Odd...
 

diedrichg

Wizard
Joined
Dec 4, 2012
Messages
1,319
What about this? Since I haven't the slightest clue as how to write it - What about creating folders for the Year and Month to keep things organized and OCD rather than your database folder holding hundreds of small .db files, you can at least break it down into neat folders for year and month. e.g.

/mnt/tank/.scripts/ConfigBackups/2015/06

/mnt/tank/.scripts/ConfigBackups/2015/07

/mnt/tank/.scripts/ConfigBackups/2015/08

and then all .db files from the 08 month would be stored in that one location.
 
Last edited:

wisebear

Cadet
Joined
Jun 16, 2015
Messages
8
I tried to use the command below but it is not working. Cron job creates .db file with a strange name (for example T3FABE~N.DB) and the file is not usable. Joeschmuck, do you have an idea what's wrong?
Code:
cp /data/freenas-v1.db "/mnt/ZFS/Backup/FreeNAS_configuration/`date \+%c`.db" && find /mnt/ZFS/Backup/FreeNAS_configuration -mtime +30 -exec rm {} \;
 

Tom_

Explorer
Joined
Jan 4, 2015
Messages
62
It runs from the CLI and at the scheduled time in Cron -- but not when I use the Cron 'Run Now' button, as pointed out by Nick. Odd...
Shrug, bash. I took the liberty of adding more quotes to your script, for me this runs at cron time and with "run now" button.
Code:
#!/bin/bash

set -eu

SRC_FILE="/data/freenas-v1.db"

# name the backup file
DEST_DIR="/mnt/volume1/Backups"
DEST_HOST=$(hostname -s)
DEST_VERSION=$(cat /etc/version)
DEST_DATE=$(date +%F)
DEST_FILE="$DEST_DATE"-"$DEST_HOST"-"$DEST_VERSION"-"$RANDOM".db

# make a copy
cp "$SRC_FILE" "$DEST_DIR"/"$DEST_FILE"

# remove files older than 90 days ending in ".db"
$(find "$DEST_DIR" -mtime +90 -name "*.db" -exec bash -c 'rm {}' \; )

Oh and since we're using bash I stuck a random number on the end in case it runs twice on the same day... Gives you a filename like:
Code:
2015-08-16-xxxxxxxx-FreeNAS-9.3-STABLE-201506292332-7584.db
 

cyberjock

Inactive Account
Joined
Mar 25, 2012
Messages
19,526
The bug with the characters not escaping properly is fixed in the nightlies.. so there's a chance that your script will break on the next update.
 

Tom_

Explorer
Joined
Jan 4, 2015
Messages
62
Thanks for letting us know, I'll check it when the update is released.
 

Glorious1

Guru
Joined
Nov 23, 2014
Messages
1,211
The bug with the characters not escaping properly is fixed in the nightlies.. so there's a chance that your script will break on the next update.
Wow, @cyberjock you are prescient. 9.3.1 broke the cron command I've been using from your post above.
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`.db
Now it sends an email saying:
Syntax error: EOF in backquote substitution
Syntax error: Error in command substitution
Not sure if I now need to remove the \ before % or add one to the first % or what?
 
Last edited:

cyberjock

Inactive Account
Joined
Mar 25, 2012
Messages
19,526
I'm not prescient. I just get info from the dev when they fix stuff.

Read this bug ticket: https://bugs.freenas.org/issues/9091

That explains what has changed. ;)

If you read the very first post I revised it recently to show what you should do for 9.3.1. Basically you should do in 9.3.1 exactly what you had to do looong ago before 9.3.0 came out. :P
 

Glorious1

Guru
Joined
Nov 23, 2014
Messages
1,211
Thank you, it works again :D
 

hervon

Patron
Joined
Apr 23, 2012
Messages
353
Yes. Same here. Thanks cyberjock.
 

Glorious1

Guru
Joined
Nov 23, 2014
Messages
1,211
I've been using cyberjocks configuration backup trick. I recently ran across this in the user guide:
Note that while FreeNAS® automatically backs up the configuration database to the system dataset every morning at 3:45, this backup will not occur if the system is shutdown at that time and the backup will not be available if the system dataset is stored on the boot pool and the boot pool becomes unavailable.
How would one recover that automatically backed up configuration? The system database has always been a mystery to me. I go to the volume where it's supposed to be and can't find much of anything. There is a .freenas directory, but not much in it that I can find, and nothing with 'config' in it.
 
Top