Backup config file every night automatically!

cyberjock

Inactive Account
Joined
Mar 25, 2012
Messages
19,526
Cyberjock, I'm sorry, I had a hard time trying to read your post. Can you put that in paragraphs so it's a little easier to read? Please?! Thanks, -matt
Tell your forum admins to fix their crap! LOL
 

KevinM

Contributor
Joined
Apr 23, 2013
Messages
106
You should NOT be emailing it. Your passwords are stored, unencrypted, in that config file. Big security risk doing email, which is why this thread doesn't show how to do emails.

I just did a strings on the db file. Then I changed the password on my gmail account and put a gpg wrapper around the attachment. My config should be nicely flagged for the NSA now. Thanks.
 

sgregory

Contributor
Joined
Apr 3, 2014
Messages
105
Is there any reason the this won't work on 9.2.1.5?
I am getting a strange email from the command.
cp /data/freenas-v1.db /mnt/Media0ne/cfgbkp/configbkps/`date+%Y%m%d`.db

My email notifying a cron failure states:
/mnt/Media0ne/cfgbkp/bkconfig.sh: date+%Y%m%d: not found




The script file is located in /mnt/Media0ne/cfgbkp and is named bkconfig.sh . I want to place the copied config files in /mnt/Media0ne/cfgbkp/configbkps


SOLVED. Looked up date command on freeBSD and found I was missing a space after date command
 

D G

Explorer
Joined
May 16, 2014
Messages
60
I've created a cron job that will automatically backup your config every night(or whenever you want).

Here's how I did it:...


cyberjock,

I just wanted to thank you for posting this handly little guide. Yes, many of us could figure it out on our own and get all the settings right, but it is certainly nice to have a pro like you spell it out for us.

Thanks again.
 

diedrichg

Wizard
Joined
Dec 4, 2012
Messages
1,319
Awesome. (and double-awesome paired with the database check with backup/no backup) - subscribed
 

Robert Smith

Patron
Joined
May 4, 2014
Messages
270
I decided to improve a little bit on the already excellent solutions everybody else offered.

This is my first ever bash script, if you find something wrong with it, please let me know.
Code:
#!/bin/bash
SRCEFILE=/data/freenas-v1.db
DESTDIR=/mnt/myvolume/mydataset/configbackups
DESTARCHIVEFILE=${DESTDIR}/freenas-v1_$(date +%Y%m%d%H%M%S).db
DESTLASTFILE=${DESTDIR}/freenas-v1_last.db

#if the destination file does not exist or md5 does not match
if [ ! -f ${DESTLASTFILE} ] || [ $(/sbin/md5 -q ${SRCEFILE}) != $(/sbin/md5 -q ${DESTLASTFILE}) ]; then
   /bin/cp ${SRCEFILE} ${DESTARCHIVEFILE};
   /bin/cp -f ${SRCEFILE} ${DESTLASTFILE};
fi


Here is how it works. The latest version of the configuration is saved into freenas-v1_last.db file. The history of old versions is saved in files with corresponding date and time appended.

Before copying, the script compares md5 hash of the current configuration file with the last saved; and only makes copies if the hash (and, by extension, the configuration) differs.

Modify the DESTDIR, in the script, to match where you want to store configuration backups. You can save the script into a file on one of your datasets somewhere (make sure the file has execute permission), and call it from a chron, as explained in the first post of this thread, only instead of “sh” put “bash”. I.e. the command becomes:

bash /mnt/myvolume/mydataset/scripts/cfgbackup.sh
 

cyberjock

Inactive Account
Joined
Mar 25, 2012
Messages
19,526
Someone else has a script that basically does this. Not to belittle your efforts as I'm sure you learned a lot and that will definitely help you in the future, but what was the point? You're trying to save how much space again? Oh.. 300kb per file. You could have 200 years worth of daily files and it will take up 20GB of space. That's a rounding error when you are talking about pools measured in TBs. And let me tell you, when I tried to compress my backup directory a few weeks ago it had something like 550 images and compressed the 128MB of files to.... less than 100KB. Now, since lz4 is the standard and enabled by default on pool creation, why add unnecessary complexity?

When it comes to dealing with doing backups and such, you want the simplest possible method to accomplish the goal so as to minimize the possibility that something might go wrong and you might suddenly not be getting backups anymore. For example, what if md5 is moved from /sbin to /usr. Your code suddenly won't work. What if the /bin/cp moves? Weird things like that *may* move in FreeBSD 10 (and therefore FreeNAS 10) because of the

In my opinion, KISS (Keep it simple stupid) when it comes to backups. Too many people won't look at their backups until they need them. And that's when they'll find out the horrible truth... it hadn't worked in months.

Just had someone a few weeks ago that had replication broken for 10 months and he never noticed because he wrote a very elaborate script that worked extremely well, but when he upgraded for 8.3.1 to 9.1.0 one of the parameters was removed from a command he used and it caused his script to work for the snapshots that had been created on 8.3.1 but snapshots created on 9.1.0+ wouldn't work.

Game over because he was also noticing this at the same time that he needed the backups. Whoops!

Please don't take this as me bashing you. I'm just explaining why I went with such a simple setup and why I also chose to post this online for the world. I've done plenty of things that later in hindsight didn't work out "as well" as I had hoped. But the education I got doing that stuff that either was useless or being superceded by something that is much better was never pointless. In fact, if you look at my recycle bin auto-emptier script I'll be providing some massive improvements to it very soon. It has some nasty and rather lame limitations that I've managed to work out.
 

hungarianhc

Patron
Joined
Mar 11, 2014
Messages
234
Question on taking nightly config backups. Is it necessary? I backup my config file before / after I do an upgrade or after I make some type of configuration change, but assuming that my NAS is mostly in a "set it and forget it" mode (obviously lots going on w/ plex, crashplan, time machine, etc), is there a benefit for me in having nightly config backups? Thanks!
 

cyberjock

Inactive Account
Joined
Mar 25, 2012
Messages
19,526

cyberjock

Inactive Account
Joined
Mar 25, 2012
Messages
19,526
You did. Some people would rather have automated backups because you won't forget where you put those or forget to make one. ;)
 

FNSeeker

Dabbler
Joined
Jan 3, 2014
Messages
45
In searching for tips on remote backup using rsync, I found an this backup tip, written up by Cyberjock, and was able to get my FreeNAS cfg backed up automatically nicely. Many thanks Cyberjock.

From a newbie. But I've got to start somewhere. Thanks gain.
 

travanx

Explorer
Joined
Jul 1, 2014
Messages
62
Is there an easy way to automatically transfer the backup file to a Windows PC nightly? Or is this good enough to just leave on the zpool?
 

anodos

Sambassador
iXsystems
Joined
Mar 6, 2014
Messages
9,554
Is there an easy way to automatically transfer the backup file to a Windows PC nightly? Or is this good enough to just leave on the zpool?
You should be fine leaving it on your pool. It's just a matter of auto-importing your pool, enabling SSH, downloading the config through SFTP, and then applying it. If your pool is toast... well... you don't need your config anyway.
 

fracai

Guru
Joined
Aug 22, 2012
Messages
1,212
I'd say you want a copy that's not on your pool. While what anodos posted will work, you can skip the steps of configuring SFTP or other methods to enable sharing if you have the config in another location. There are a number of ways to automate getting it off the pool (Crashplan, BT Sync, rsync, cronjob and FTP).

It'd be easier if the configuration GUI included a way to browse local pools.
 

anodos

Sambassador
iXsystems
Joined
Mar 6, 2014
Messages
9,554
I'd say you want a copy that's not on your pool. While what anodos posted will work, you can skip the steps of configuring SFTP or other methods to enable sharing if you have the config in another location. There are a number of ways to automate getting it off the pool (Crashplan, BT Sync, rsync, cronjob and FTP).

It'd be easier if the configuration GUI included a way to browse local pools.
I'm personally leery of having my config file sitting in random places because it contains plain-text passwords. :)
 

fracai

Guru
Joined
Aug 22, 2012
Messages
1,212
Crashplan allows encrypting files with your own key. BT Sync only goes to the machines that you configure (in my case the data would never leave my LAN). Rsync is the same. Nothing random about any of that.
 

FNSeeker

Dabbler
Joined
Jan 3, 2014
Messages
45
Is there an easy way to automatically transfer the backup file to a Windows PC nightly? Or is this good enough to just leave on the zpool?

Not sure if you had settled on one. But I suggest Free File Sync. I use it to backup data on all PCs to NAS on local network. It's a really good app and free as well. It also works on Macs.

(For remote backup to a second NAS (@ my friend's place), I use Rsync to minimize the amount of data transfer, via a VPN connection for security of transmission).

I personally think auto backup is good for recovery of last resort. Normal good practice is to run a config backup before any update to FreeNAS is done. It takes only a minute or so but it forces you to think of any relevant issue and there is no oops...

Cheers.
 

travanx

Explorer
Joined
Jul 1, 2014
Messages
62
I'd say you want a copy that's not on your pool. While what anodos posted will work, you can skip the steps of configuring SFTP or other methods to enable sharing if you have the config in another location. There are a number of ways to automate getting it off the pool (Crashplan, BT Sync, rsync, cronjob and FTP).

It'd be easier if the configuration GUI included a way to browse local pools.

Thanks. Time to play around with those options. I only want this 1 file on my Windows PC just in case. But if it all goes down and I can recover the file off the zpool then it doesn't matter. I back up what I do care about in various other ways before they go to Freenas.
 

Saz

Cadet
Joined
Feb 15, 2015
Messages
5
Thanks cyberjock! I have been reading much of your stuff including your guide. I have my usb drives mirrored but I am doing your script as well. You can never have too much protection! Speaking of that, I was thinking of building a 2nd system with FreeNAS as a backup to my main FreeNAS system. This would not do anything but hold backups of the main system. What do you recommend as the most reliable solution to replicating files between the two? SCP? Replicating snapshots? Or something else?

Thanks for all you do!

Saz
 
Top