Saturday, December 7, 2013

HOW TO PERFORM BACKUPS WITH RSYNC?

Luna llena desde el parque (Full moon from the park)
I learned that courage was not the absence of fear, but the triumph over it. The brave man is not he who does not feel afraid, but he who conquers that fear



Nelson Mandela

1. WHAT IS RSYNC?

RSYNC a tool to copy data locally or through the network (from/to another host). It's widely used because of the delta-transfer algorithm, which reduces the amount of data sent over the network by sending only the differences between the source files and the existing files in the destination.
Rsync is widely used for doing backups and as mirroring tool.

2. SYNTAX

Structure of the command:
$ rsync [options] source destiny

Most usefull parameters:
-v
--verbose
Increase verbosity.
-e
--rsh=command
Specify the remote shell to use.
-a
--archive
Archive mode: recursive, copy links, preserves permissions modification times, groups, preserve and device files.
-r
--recursive
Recurse into direcDelete extraneous files from destination directory.tories.

--delete

-z
--compress
Compress data files during the transfer.

--progress
Displays the progress of the copy.

Setting limits:
--exclude=pattern
Excludes all files or folders that follow the pattern .
--include=pattern
Include all files or folders that follow the pattern.
--max-size=size
Exclude all files that are bigger that the limit size.
--min-size=size
Exclude all files that are smaller that the limit size.
--bwlimit=kpbs
Limit the I/O bandwidth in kilo bytes per second.

3. EXAMPLES

The following examples are based on the following network structure:


  • FROM LOCAL TO LOCAL
Copies or syncronizes the content of the folder Fotos, zipping all data to transfer, preserving information for each copied data but deleting all data in the destiny folder that don't exist in the original folder.
$ rsync -vaz --delete /media/hd1/Fotos /media/hd2/Backup_Fotos

Copies a file that is not compressed and preserves all its information.
$rsync -va /media/hd1/file /media/hd2/

  • FROM LOCAL TO REMOTE SERVER
Copies folders and displays the progress for each transferred data.
$ rsync -vaz -- progress /tmp/ user@192.168.1.160:/srv/Departments

Copies all the content that is less than or equals to 10 Mb and greater than or equals to 1MB
$ rsync -vaz --max-size='10240k' --min-size='1024k' ~/Documents user@192.168.1.160:/srv/ProjectX

  • FROM REMOTE SERVER TO LOCAL
Exclude all files and directories except the compressed zip files.
$ rsync -vaz --exclude='*' --include='*.zip' user@192.168.1.160:~user/Documents /media/hd2/mybackup

Data are copied and transferred encrypted over ssh
$ rsync -vaze ssh user@192.168.1.160:~user/Documents /media/hd2/mybackup

  • FROM REMOTE SERVER TO REMOTE SERVER
Syncronizing folders controlling that the band width is not bigger than 1Mb over ssh
$ rsync –bwlimit=1024 -vaze ssh user@192.168.1.150:/srv user@192.168.1.160:/srv

4. HOW TO SCHEDULE COPIES WITH RSYNC?

If you plan to make backups regularly, you can use cron to schedule a rsync job. To do it, follow this steps:
1. Edit cron using the command: crontab -e
2. Add a new line on the bottom of the file according to the following syntax:
Minute Hour Day_of_month Month Day_of_Week User Command
For each missing column, you have to introduce the * character. It match for each possible value. For instant, * in the “Hour” and “Day of the Week” column means for each hour and for each day of the week.

For instance:
2.1 Job that runs from Monday to Friday at midnight:
00 00 * * 1-5 rsync -vaz --delete /media/hd1/Fotos /media/hd2/Backup_Fotos

2.2 Job that runs every saturday at 22:00:
00 22 * * sat rsync –bwlimit=1024 -vaze ssh user@192.168.1.150:/srv user@192.168.1.160:/srv

No comments:

Post a Comment