Rsync

Rsync setup backup on CentOS / Debian / Ubuntu

There are many ways to organize backup on CentOS / Debian / Ubuntu servers – free utilities, self-written scripts using tar, bacula backup system, and much more. All this in one way or another I have used or am using in my work.

Today I want to share with you my method of organizing a simple, convenient, and fast way to configure incremental backup using the popular rsync utility on servers running CentOS / Debian / Ubuntu. The method works equally on these systems, minor differences only in the rsync installation itself, which I will mention separately for each system.

Installing rsync on CentOS 6

Install xinetd to automatically run rsync:

# yum install -y xinetd

We install rsync directly:

# yum install -y rsync

Editing the rsync config for xinetd:

# mcedit /etc/xinetd.d/rsync

In the config we find the line  disable = yes  and change it to no :

disable = no

Running xinetd:

# /etc/init.d/xinetd start

And add it to the autorun:

# chkconfig xinetd on

Check if xinetd 873 listens on the rsync port:

# netstat -lnpt | grep 873

tcp 0 0 0.0.0.0:873 0.0.0.0:* LISTEN 1431 / xinetd

All right, you can start configuring rsync.

We put rsync:

# yum install -y rsync

Adding to Auto Backup:

# systemctl enable rsyncd

ln -s '/usr/lib/systemd/system/rsyncd.service' '/etc/systemd/system/multi-user.target.wants/rsyncd.service'

Checking for Auto Backup:

# systemctl list-unit-files --type service | grep rsyncd

rsyncd.service enabled

Run rsync:

# systemctl start rsyncd

We check how it started:

# netstat -tulpn | grep rsync

tcp 0 0 0.0.0.0:873 0.0.0.0:* LISTEN 2782 / rsync

All right, you can start configuring rsync.

Installing rsync on Debian / Ubuntu

Install rsync:

# apt-get install -y rsync

Config Rule:

# mcedit / etc / default / rsync

Find the string RSYNC_ENABLE = false  and change it to true :

RSYNC_ENABLE = true

Run rsync:

# /etc/init.d/rsync start

[ok] Starting rsync daemon: rsync.

Check what works:

# netstat -tulpn | grep rsync

tcp 0 0 0.0.0.0:873 0.0.0.0:* LISTEN 1767 / rsync

All right, you can start configuring rsync.

Configuring rsync

Now proceed to setup. The logic of our backups will be the following. At the first start, we make a full backup of the information we are interested in in the current folder. Then, once a day, we check the existing archive with the source and make it up-to-date again, overwriting all the changed files, but do not delete them, but add them to the increment folder, where every day a folder with a name is created in the form of a date, files for the current day.

Thus, we will always have a full archive, actual at the time of the last synchronization, plus a set of folders for each day with files changed that day. How many days can be stored if necessary?

It turns out we have such a picture:

Rsync

Let’s start to implement. First of all, configure rsync on the servers of information sources, from which we will collect data for backup.

Create the rsync configuration file:

# mcedit /etc/rsyncd.conf
pid file = /var/run/rsyncd.pid
log file = /var/log/rsyncd.log
transfer logging = true
munge symlinks = yes

# folder source for backup
[data]
path = / data
uid = root
read only = yes
list = yes
comment = Data backup Dir
auth users = backup
secrets file = /etc/rsyncd.scrt

Create a file with the credentials for the connection:

# mcedit /etc/rsyncd.scrt
backup: 12345

where the backup is the username, 12345 is the password.
We make read access only to root, otherwise, rsync will not start:

# chmod 0600 /etc/rsyncd.scrt

After the configuration, restart rsync.
On Centos 6:

# /etc/init.d/xinetd restart

On Centos 7:

systemctl restart rsyncd

On Debian / Ubuntu:

# /etc/init.d/rsync restart

Now go to the server receiver, which will store backup copies from the source servers. There we create an incremental backup script using rsync:

# mcedit /root/bin/backup-server1.sh
#! / bin / bash
date
# Folder, where we will put the archives
syst_dir = / backup /
# Name of the server that is archived
srv_name = server1
# The address of the server that is archived
srv_ip = 10.10.1.55
# The rsync user on the server that is archived
srv_user = backup
# Resource on the server for backup
srv_dir = data
echo "Start backup $ {srv_name}"
# Create a folder for incremental backups
mkdir -p $ {syst_dir} $ {srv_name} / increment /
# Run directly backup with parameters
/ usr / bin / rsync -a -delete -password-file = / etc / rsyncd.scrt $ {srv_user} @ $ {srv_ip} :: $ {srv_dir} $ {syst_dir} $ {srv_name} / current / - -backup --backup-dir = $ {syst_dir} $ {srv_name} / increment / `date +% Y-% m-% d` /
# Clean folders with incremental archives older than 30 days
/ usr / bin / find $ {syst_dir} $ {srv_name} / increment / -maxdepth 1 -type d -mtime +30 -exec rm -rf {} \;
date
echo "Finish backup $ {srv_name}"

We make the script executable:

# chmod 0744 /root/bin/backup-server1.sh

Create a file with a password for authorization on the source server:

# mcedit /etc/rsyncd.scrt
12345

We make read access only to root, otherwise, rsync will return an error:
ERROR: password file must not be other-accessible
Correct this:

# chmod 0600 /etc/rsyncd.scrt

That’s it, now you can run the script and wait for it to execute. It remains to add it to the corn:

# mcedit / etc / crontab
30 23 * * * root /root/bin/backup-server1.sh

I usually create several scripts for each server separately. Then I combine their launch in one common script and I add it to the cron. And then, as necessary, I edit it, add or delete the server.

We specified in the settings logging to the file /var/log/rsyncd.log. It is necessary to configure the rotation of this log so that it does not grow to infinity. On large file servers, it will grow very quickly to hundreds of megabytes or more.

To do this, create a file with the rotation configuration in the /etc/logrotate.d folder:

# mcedit /etc/logrotate.d/rsyncd

/var/log/rsyncd.log {
size = 500k
compress
rotate 4
missingok
notifempty
}

With these settings, the rotation will occur every time the log file exceeds the 500 KB size. Will be stored 4 versions of the log file. You can change these settings at your own discretion.

Example of backup server windows using rsync

Another example from my practice. Let’s say we have a windows server with some information, which we also want to back up. No problem, it’s done quite simply.

Create a network ball on the windows server with the information. Create a user and add it to access this folder. This user we will use to connect the Windows to the Linux server.

Mount the ball with the information that we will backup:

# mount -t cifs //192.168.0.16/docs / mnt / docs -o user = backup, password = 12345, iocharset = utf8, codepage = cp866

192.168.0.16 – the address of the
backup and 12345 – the user and password of the machine with access to the docs ball.

All, now the folder/mnt/docs can be used as a receiver in our backup script with rsync. If the folder is mounted directly to the server with backups, you need to configure it on the rsyncd server itself using the example of the source servers, run rsyncd on it, and specify 127.0.0.1 in the script as the IP address of the server.

In such cases I create several scripts: on mounting balls, backup and unmounting, combine them into one, and run them sequentially. As a result, it turns out that we connect the disk, make a backup and turn it off.

Leave a Comment

Comments

No comments yet. Why don’t you start the discussion?

    Leave a Reply

    Your email address will not be published. Required fields are marked *