The simplest backup of FreeBSD configs with sending archive to mail

  • Tutorial
For a small local area network, NAS was installed under FreeBSD and, of course, in the end the question arose of backup of its configuration in case of a system crash. I did not want to screw anything cumbersome, especially since the recovery speed, in which case, is not yet critical. Therefore, it was decided to write the simplest script for myself, adding the necessary files to the archive every night. And as an external storage, a mail server was selected. I want to share this script with you.

I will make a reservation right away. To whom this method will not work:
  • avid paranoid
  • Those who wish to restore the server’s performance in half an hour on day X
  • whose archive size will exceed 25 MB. (in the case of gmail )
The post is aimed at the same newcomers as I do. He described everything as detailed as possible.

So, we have Network Access Server on FreeBSD, which also acts as a Web server for a couple of sites and one forum.
In my case, the MySQL database is backed up, the entire contents of the / etc, / usr / local / etc folders (so as not to rewrite the path to each config separately), the kernel configuration, cron, and the directory with sites.

Let's start with the most difficult. We dump the MySQL database using the standard mysqldump utility.. Especially for her, we will create a new sql-user “backup” with a minimum of privileges sufficient for our idea. I set the following: SELECT, FILE, SHOW DATABASES, LOCK TABLES, SHOW VIEW. I will not describe the process of creating a user due to the variety of options, but if I try to answer all questions in the comments.

The database is dumped into the /var/tmp/all.sql file using the command:

/usr/local/bin/mysqldump --opt -Aau backup -pПАРОЛЬ_ПОЛЬЗОВАТЕЛЯ_BACKUP > /var/tmp/all.sql

In principle, everything should work out the first time. Further it is easier.
Yes, inveterate Linux users will not bother me, but I chose RAR as the archiver, because I wanted to password protect the resulting archive for better reliability, but tar cannot do this right away.
RAR setup is trivial:

cd /usr/ports/archivers/rar
make install clean

After a successful installation, we read the manual, select the necessary keys, specify the path to the files and folders through a space, and check the operability.
In my case, the team

/usr/local/bin/rar a -ow -inul -pПАРОЛЬ_НА_АРХИВ /var/tmp/server_backup.rar /var/tmp/all.sql /usr/src/sys/i386/conf/kernel /var/cron/tabs /etc /usr/local/etc /usr/local/www/data

created the archive /var/tmp/server_backup.rar containing all the listed files and directories. Please note that if you specify paths to directories with a slash at the end, then the subdirectories will not be archived, but only the files from the root of the specified folder!

Next, we encode the resulting archive into a format that is understandable to mailers and send it to the mail with the subject “server backup”

/usr/bin/uuencode '/var/tmp/server_backup.rar' server_backup.rar | mail -s 'server backup' 'ПОЧТА'

Naturally, sending to e-mail can be replaced by merging a backup to FTP or ... anywhere - depending on what is available :)
After successful sending, we will delete our archives

rm /var/tmp/server_backup.rar
rm /var/tmp/all.sql

And finally, the whole script assembly looks like this:

/usr/local/bin/mysqldump --opt -Aau backup -pПАРОЛЬ_ПОЛЬЗОВАТЕЛЯ_BACKUP > /var/tmp/all.sql
/usr/local/bin/rar a -ow -inul -pПАРОЛЬ_НА_АРХИВ /var/tmp/server_backup.rar /var/tmp/all.sql /usr/src/sys/i386/conf/kernel /var/cron/tabs /etc /usr/local/etc /usr/local/www/data
/usr/bin/uuencode '/var/tmp/server_backup.rar' server_backup.rar | mail -s 'server backup' 'ПОЧТА'
rm /var/tmp/server_backup.rar
rm /var/tmp/all.sql

We save it in any way we know, such as /home/%username%/ (% username% is your name in the system) and give the necessary rights

chmod 750 /home/%username%/

After that, the script can be run and verify that it is operational. If something goes wrong, the entries in / val / log / messages and / var / log / maillog

are suitable for debugging. It remains to add the script execution task to cron . We start crontab -eand, using vi , enter the line

1 4 * * * /home/%username%/

don't forget to press Enter at the end (there should be an empty line at the very end of the file).
To enter editing mode in Vi, press i. To exit with saving the file Escand :wq
With such a recording, the script will be executed every night at 4 hours 1 minute. View all crontab entries

crontab -l

I also want to pay attention to the fact that the names of files and directories in the archive are not encrypted! By opening the archive, for example, in WinRAR you can view all folders and their contents (the directory structure is saved), but you cannot open files without entering a password.

Actually, that's all. There is no limit to perfection, so with pleasure I will listen to all offers, wishes and especially criticism.
Good luck and let the resulting archive not come in handy to you;)

Also popular now: