📜 ⬆️ ⬇️

The simplest backup of FreeBSD configs with sending an archive to mail

For a small local network, a NAS was installed under FreeBSD and, of course, in the end, the question arose of backing up its configuration in case of a system crash. I didn’t want to wind up anything cumbersome, especially since the recovery rate in which case is not yet critical. Therefore, it was decided to write a simple scriptbook for themselves, every night adding the necessary files to the archive. And the mail server was chosen as the external storage. I want to share this script with you.

I will make a reservation right away. To whom this method is not suitable:
The post is focused on the same beginners, as well as me. Described everything as possible in as much detail as possible.

So, we have a Network Access Server on FreeBSD, also acting 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 folders / etc, / usr / local / etc (so as not to rewrite the path to each config separately), the kernel configuration, cron and the directory with the sites.

Let's start with the most difficult. MySQL database dump is done using standard mysqldump utility . Especially for it we will create a new sql-user “backup”, having a minimum of privileges sufficient for our idea. I set the following: SELECT, FILE, SHOW DATABASES, LOCK TABLES, SHOW VIEW. The process of creating a user will not be described in view of the variety of options, and if I try to answer all the questions in the comments.
')
Dump the database to the /var/tmp/all.sql file with the command:

/usr/local/bin/mysqldump --opt -Aau backup -p__BACKUP > /var/tmp/all.sql 

In principle, everything should turn out the first time. Further - easier.
Yes, avid Linux users do not want me, but I chose RAR as an archiver, because for the sake of reliability, I also wanted to protect the resulting archive, and tar doesn’t know how to do it on the fly.
Installing RAR is trivial:

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

After a successful installation, we read the manual, select the necessary keys, specify the paths to the files and folders through a space and check the operation.
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 archive /var/tmp/server_backup.rar, containing all the listed files and directories. Note that if you specify paths to directories with a slash at the end, then subdirectories will not be archived, but only files from the root of the specified folder!

Next, re-encode the resulting archive into a clear postal form 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' '@gmail.com' 

Naturally, sending by mail can be replaced by merging backups 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 entire assembly script looks like this:

 #!/bin/sh /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' '@gmail.com' rm /var/tmp/server_backup.rar rm /var/tmp/all.sql 

We save it in any way known to us, such as /home/%username%/backup.sh (% username% is your name in the system) and grant the necessary rights

 chmod 750 /home/%username%/backup.sh 

After that, the script can be run and make sure it works. If something goes wrong, the entries in / val / log / messages and / var / log / maillog

It remains to add the task of running the script in cron . Run crontab -e and, with vi , enter the string

 1 4 * * * /home/%username%/backup.sh 

do not forget to press Enter at the end (there should be an empty line at the very end of the file).
To switch to edit mode in Vi, you must press i . To exit with saving the file Esc and :wq
With this recording, the script will be executed every night at 4 o'clock 1 minute. View all crontab entries

 crontab -l 

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

Actually, everything. There is no limit to perfection, so I’m happy to hear all the suggestions, wishes and especially criticism.
Good luck and let the archive do not come to you;)
via dobryj.ru

Source: https://habr.com/ru/post/105183/


All Articles