Use rsync to back up images from linux to linux or to windows

Source: Internet
Author: User
Use rsync to back up images for websites from linux to linux or to windows-Linux Enterprise applications-Linux server applications. For more information, see the following. Rsync is a data image backup tool in Linux. It can be seen from the software name-remote sync. Rsync supports most Unix-like systems, including Linux, Solaris, and BSD. The latest version of rsync can be found at Its features are as follows:
1. images can be used to save the entire directory tree and file system.
2. It is easy to maintain the permissions, time, and soft links of the original file.
3. installation without special permissions.
4. optimized process, high file transmission efficiency.
5. You can use rcp, ssh, and other methods to transmit files. Of course, you can also use a direct socket connection.
This article describes how to use the rsync service for remote backup from Linux to Windows and linux.

1. Configure the server

Software Download
The home address of rysnc is you can download the rysninstallation software from here.

Compile and install
Rsync compilation and installation are very simple. You only need to perform the following steps (install both servers ):

$ Tar xvf rsync-2.6.5.tgz
$ Rsync-2.6.5 cd
$ Make
$ Make install

However, you must install rsync on both the master server and synchronization server. The master server runs rsync in server mode, while the synchronization server runs rsync in client mode. In this way, the rsyn daemon is run on the master server, and crond is used to regularly run the client program to synchronize the content to be synchronized on the master server.

Configure the rsync server

The most important and complex rsync server is its configuration. The configuration file of the rsync server is/etc/rsyncd. conf, which controls authentication, access, and logging. This file is composed of one or more modules. A module definition starts with the module name in square brackets until the definition of the next module starts or the file ends. The module contains the Parameter definition in the format of name = value. Each module corresponds to a directory tree to be backed up. For example, in our instance environment, there are three directory trees to be backed up: the/www/AND/mirror/file0/AND/mirror/file1/directories must be defined in the configuration file. The configuration file is the unit of action, that is, each new line represents a new comment, module definition, or parameter value assignment.

For example, create the rsyncd configuration file/etc/rsyncd. conf on 168. The content is as follows:

Uid = nobody # ID of the backup, user ID
Gid = nobody # ID of the backup, group ID
# Note that this user ID and group ID can be set to root for convenience, so rsync is almost ready.
# To read any files and directories, but it also brings security risks. We recommend that you set it to read data only.
# Copies of directories and files.

# Use chroot = no
Max connections = 0 # no limit on the maximum number of connections
Pid file =/var/log/rsync/rsyncd. pid
Lock file =/var/log/rsync. lock
Log file =/var/log/rsync/rsyncd. log

[Attachment] # specify the name of the authenticated backup module
Path =/www/htdocs/pub/attachment/# directory to be backed up
Comment = BACKUP attachment # comment
Ignore errors # ignore unrelated IO errors
Read only = false # Set to read-only
List = false # column Files Not Allowed
# Hosts allow = # IP address of the host that allows connection to the server
# Hosts deny = # disallow access to the server's Host IP Address
Auth users = msyn # The authenticated user name. If this row is not used, it indicates that it is anonymous.
Secrets file =/etc/rsyncd. scrt # authentication file name, used to store the password

[98 htdocs]
Uid = nobody
Gid = nobody

Path =/www/htdocs/
# Ignore errors
Read only = false
List = false
# Hosts allow =
# Hosts deny =
# Hosts deny =
Auth users = msyn
Secrets file =/etc/rsyncd. scrt

[98 html]
Uid = ejbftp
Gid = nobody
Path =/www/htdocs/pub/html/
# Ignore errors
Read only = false
List = false
# Hosts allow =
# Hosts deny =
Auth users = 98syn
Secrets file =/etc/rsync98.scrt

The three modules [attachment], [98 htdocs], and [98 html] are defined here, which correspond to the three target trees to be backed up. The backup users authorized by the three modules are msyn, msyn, and 98syn, respectively. The user information is stored in the file/etc/rsyncd. scrt and/etc/rsync98.scrt. The content is as follows:

[Root @ www/etc] # cat/etc/rsyncd. scrt
Msyn: xxxxxxxxx

In addition, this file can only be read and written by the root user. Note that for security purposes, the attribute of this file must only be readable by the owner; otherwise, rsync will refuse to run. We can set its property to 600:

[Root @ www/etc] # chmod 600/etc/rsyncd. scrt

[Root @ www/etc] # mkdir/var/log/rsync

After these files are configured, you need to start the rsyncd service on the master server:

[Root @ www/etc] #/usr/local/bin/rsync -- daemon

After the preceding command is executed, rsync can be started. The default service port of rsync is 873. The server receives anonymous or authenticated backup requests from the customer on this port.

Ii. Configure the client

1. Run the rsync Client Command in linux

The next step is to run the rsync client startup command:

[Backup @ backup/]/usr/bin/rsync-vlzrtogp -- progress -- delete 98syn@x.x.x.168 :: 98 html/usr/local/apache/htdocs/pub/html/-- password-file =/etc/rsync98.scrt

In the preceding command line-In vzrtopg, v represents verbose (details), z represents zip (compression), and r represents recursive (recursion ), topg is a parameter that maintains the original attributes of a file, such as the owner and time. -- Progress indicates that the detailed progress is displayed. -- delete indicates that if the server deletes the file, the client also deletes the file to ensure true consistency.
98syn@x.x.x.168: 98 html indicates that the command is to back up the 98html module in server x. x. x.168, where 98syn indicates that 98syn is used to back up the module. -- Password-file =/etc/rsync98.scrt to specify the password file, which can be used in the script without the need to enter the verification password interactively, note that the permission attribute of the password file must be set to only the current user who executes the command. In this example, it is a 98syn user. The backup content is stored in the/usr/local/apache/htdocs/pub/html/directory of the backup machine.

In this way, the rsync synchronization service has been set up. Finally, we can use the crontab command executed on the client to implement automatic backup or write some scripts, in this way, the rsync synchronization service is more perfect.

2. Configure the Windows Client

To use rsync in Windows, we need to download the cwRsync tool, which is an rsync for windows version.

Now we can run rsync in Windows. For example, use the following command to connect to the server and start backing up directories and files:
Rsync-vzrtopg -- progress -- delete 98syn@xx.xx.xx.xx: 98 html. \ bak \
We can see that:

A prompt asking you to enter the password should be displayed after you enter the password correctly.

There are several possibilities for this error. One is that you did not enter the correct user name or password, and the other is that the file storing the password on your server does not have the correct permission, that is, your password file is not a permission similar to this:
-Rw ------- 1 root

After the backup is complete

The password can be read from a file:
Rsync-vzrtopg -- progress -- delete 98syn@xx.xx.xx.xx: 98 html. \ bak \ -- password-file =. \ rsync98.scrt
You can write batch processing to implement scheduled tasks in windows and perform scheduled tasks to implement automatic backup.

You can also download the cwRsync-1.2.8 version here: cwRsync_1.2.8_Installer.exe

Note: The reference address is Respect originality, and indicate the source if any reposted information is provided.


Q: How can I perform rsync through ssh without entering a password?
A: follow these steps:

1. Use ssh-keygen to create an ssh keys on server a. do not specify a password ~ /. Ssh: the identity and identity. pub files are displayed.
2. Create a subdirectory. ssh in the home directory on server B.
3. Copy identity. pub OF a to server B.
4. Add identity. pub ~ [User B]/. ssh/authorized_keys
5. So user a on server a can use the following command to ssh user B to server B.
E.g. ssh-l userb serverb
In this way, user a on server a can log on to server B as user B without a password.

Q: How can I use rsync through the firewall without compromising security?
A: The answer is as follows:

There are two common cases: one is that the server is inside the firewall and the other is outside the firewall. In either case, ssh is usually used. In this case, it is best to create a backup user and configure sshd to only allow this user to access through rsa Authentication.
If the server is in the firewall, it is best to limit the IP address of the client and reject all other connections. If the client is in the firewall, you can simply allow the firewall to open the ssh outbound connection on tcp port 22.

Q: Can I back up the changed or deleted files?
A: Of course:

You can use commands such as rsync-other-options-backupdir =./backup-2000-2-13.
In this case, if the source file is/path/to/some/file. c changed, so the old file will be moved. /backup-2000-2-13/path/to/some/file. c,
Here, this directory requires you

Manually created

Q: What ports do I need to open on the firewall to adapt to rsync?
A: depends on the situation.

Rsync can directly transfer files through tcp connection on port 873, or through ssh on port 22, but you can also change the port through the following command:

Rsync -- port 8730 otherhost ::
Rsync-e 'ssh-p 2002 'otherhost:

Q: How can I copy only the directory structure through rsync and ignore the file?
A: rsync-av -- include '*/' -- exclude '* 'source-dir dest-dir

Q: Why do I always see the "read-only file system" error?
A: Check if you forget to set "read only = no ".

Q: Why do I encounter the '@ error: invalid gid' error?
A: During rsync, uid = nobody is used by default; gid = nobody is used for running. If your system does not have a nobody group, this error will occur, you can try gid = nogroup or another

Q: Why does port 873 fail to be bound?
A: If you do not run the daemon with the root permission, this error will occur because the ports below port 1024 are privileged ports. You can use the -- port parameter to change the value.

Q: Why does my authentication fail?
A: From the Perspective of your command line:

You are using:
> Bash $ rsync-a test
> Password:
> @ Error: auth failed on module test
> I dont understand this. can somebody explain as to how to acomplish this.
> All suggestions are welcome.

There should be no issues caused by login with your username, try rsync-a max@ test

Ii. Some useful scripts

These scripts are examples on the rsync Website:

1. Incremental backup of data to the central server every seven days

#! /Bin/sh

# This script does personal backups to a rsync backup server. you will end up
# With a 7-day rotating incremental backup. the incrementals will go
# Into subdirectories named after the day of the week, and the current
# Full backup goes into a directory called "current"

# Directory to backup
Bdir =/home/$ user

# Excludes file-this contains a wildcard pattern per line of files to exclude
Excludes = $ home/cron/excludes

# The name of the backup machine
Bserver = owl

# Your password on the backup server
Export rsync_password = xxxxxx

######################################## ################################

Backupdir = 'date + %'
Opts = "-- force -- ignore-errors -- delete-excluded -- exclude-from = $ excludes
-- Delete -- backup-dir =/$ backupdir-"

Export path = $ path:/bin:/usr/local/bin

# The following line clears the last weeks incremental directory
[-D $ home/emptydir] | mkdir $ home/emptydir
Rsync -- delete-a $ home/emptydir/$ bserver: $ user/$ backupdir/
Rmdir $ home/emptydir

# Now the actual transfer
Rsync $ opts $ bdir $ bserver: $ user/current

2. Back up data to an idle Hard Disk

#! /Bin/sh

Export path =/usr/local/bin:/usr/bin:/bin

List = "rootfs usr data data2"

For d in $ list; do
Mount/backup/$ d
Rsync-ax -- exclude fstab -- delete/$ d // backup/$ d/
Umount/backup/$ d

Day = 'date "+ % "'

Rsync-a -- delete/usr/local/apache/data2/backups/$ day
Rsync-a -- delete/data/solid/data2/backups/$ day

3. image the cvs tree of

#! /Bin/bash

Path =/usr/local/bin:/usr/freeware/bin:/usr/bin:/bin

Run = 'lps x | grep rsync | grep-v grep | wc-l'
If ["$ run"-gt 0]; then
Echo already running
Exit 1

Rsync-az cvs/cvsroot/changelog $ home/changelog

Sum1 = 'sum $ home/changelog'
Sum2 = 'sum/var/www/cvs/vger/cvsroot/changelog'

If ["$ sum1" = "$ sum2"]; then
Echo nothing to do
Exit 0

Rsync-az -- delete -- force cvs // var/www/cvs/vger/
Exit 0

4. A clever way to use find

Rsync-avr remote: ''find/home-name "*. [ch]" ''/tmp/

You can use this method to list the files to be backed up. This method seems to be rarely used.
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.