📜 ⬆️ ⬇️

Setting up backup in Ubuntu

Setting up backup in Ubuntu for 20 minutes.


To work on projects using svn, which is located on a remote virtual dedicated host running ubuntu 8.04. Over time, data volumes grew, as did the criticality of these data. The loss of something dreamed in nightmares. From time to time I copied the repositories to the local computer. Recently I got tired of it. And I began to look for opportunities to automate this matter. I will not talk about the search and options, talk about the results.

So, we have a remote host running ubuntu, with some array of rather critical data. It would be quite logical to set up a backup directly on the remote host, using tar for krone, rsyns, etc. But, since space on a virtual dedicated hosting is quite expensive and it is better to use it in the case, it would be ideal if the data were automatically copied to some local machine, where there is more than enough space on it. In my case, this is a file service in the office, running all the same Ubuntu.


Training


The data will be poured using SSH, so let's first configure the public and private keys for local and remote servers. We do this so that the program that will transfer the data can go over SSH without a password.
')
$ ssh-keygen -t dsa

Leave the default folder and make the password blank.
This command should create two files in the ~ / .ssh folder (by default) - private and public key. private is for the local machine, pub is sent to the remote.

Now copy the private key to the /root/.ssh folder so that the root user can use it so

$ cd ~/.ssh
$ sudo mkdir /root/.ssh
$ sudo cp id_dsa /root/.ssh


Now we need to copy the public key to the remote machine from which we want to copy data. Pre-create a backup user on a remote machine (adduser command). Remember to give this user permissions to read the directories you want to copy.

$ cat ~/.ssh/id_dsa.pub | ssh backup@remotehost.ru "cat >> ~/.ssh/authorized_keys2"

Now we can try to log in via ssh to the remote machine:

$ ssh backup@remotehost.ru

In case everything is done correctly, we will be let in without a password.
On the remote machine we set the normal rights to read the public key:

remotehostru$ chmod 700 .ssh
remotehostru$ chmod 400 .ssh/authorized_keys2
remotehostru$ exit


Rsnapshot setup


rsnapshot is a utility for creating copies of the state of rsync-based file systems. It simplifies the creation of periodic copies from local and remote machines via ssh. It uses, whenever possible, rigid communications that allows to reduce essentially the volume of necessary disk space. (quote from here )

Installation


Install rsnapshot:

$ sudo apt-get install rsnapshot

If you are using a non-debian-like distribution, rsnapshot must also be in your distribution's repositories. For CentOS, with RPMForge enabled, this is done, for example, as follows:

# yum install rsnapshot

Now we need to create a directory where we are going to store our “snapshots”:

$ sudo mkdir /var/snapshots

Customization


Now you can proceed to the setting, in fact, rsnapshot:

$ sudo nano /etc/rsnapshot.conf

Instead of nano, you can use any other editor, such as vi, or gedit, if you are working in GNOME.
You need to configure the following parameters:

snapshot_root - , "".

interval xxx yy - - ( hourly, daily), yy - . :
interval hourly 6
interval daily 7


Means that we want to keep 6 hourly copies and 7 monthly copies. If the specified number of copies is already available, rsnapshot will replace the old one with a newer one.

Uncomment cmd_cp. cmd_ssh uncomment and change to

cmd_ssh /usr/bin/ssh

Backup can be set up using the backup command <where> <where>:

# /etc/ localhost/
backup /etc/ local/
# /var/svn remotehost/
backup backup@remotehost.ru:/var/svn/ remotehost/


Remember that spaces are not allowed in the configuration file - use only tabs.

Trial run


Run rsnapshot:
$ rsnapshot hourly

The second parameter indicates the interval that we set in the configuration file.
The command can be performed for a long time. After execution, we look at what she created:
$ ls -l /var/snapshots

So far there should be one directory in the directory: hourly.0. The next time rsnapshot starts, it will create directories hourly.1, hourly.2, etc., until it hits the maximum that we specified in the configuration file.

Cron setup


Ubuntu automatically creates the /etc/cron.d/rsnapshot file with the following contents:
0 */4 * * * root /usr/bin/rsnapshot hourly
30 3 * * * root /usr/bin/rsnapshot daily
0 3 * * 1 root /usr/bin/rsnapshot weekly
30 2 1 * * root /usr/bin/rsnapshot monthly


That's all. Now you should automatically create a snapshot of data from your remote server 6 times a day. The data is safe, and yes even geographically distributed.

By the way, 6 times a day does not mean that the size will be 6 times larger than if you copy only 1 time per day. If there are no changes in the files between copies, the total size of the copies will hardly change.

Additional Information


With the backup_script option, you can also configure the backup of MySQL databases, and indeed everything, anything. I did not describe this process, because I do not use it and I can not say anything concrete.
You can read more in Google. At the request of rsnapshot gets a bunch of relevant links, though in English.

I ask you not to scold too much, I don’t look like an administration guru (and linux), but for a long time I was looking for how easy it was to automate backups - I found a way, I decided to share.
But constructive criticism and suggestions will, of course, be pleased.
UPD: The same article in my blog

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


All Articles