زکات علم

زَکاةُ العِلمِ أن تُعَلِّمَهُ عِبادَ اللّه‏ِ امام باقر (ع)
زکات علم

مطالبی در زمینه کامپیوتر و علاقه مندی های شخصی من مطالب این وبلاگ غالبا مطالبی ست که در جای جای اینترنت کتاب یا دانشته های شخصی خودم می باشد که به عنوان مرجعی برای رجوع دوباره در اینجا جمع آوری شده اند .
ehsunitd.ir personal website

پیوندها

۱ مطلب با کلمه‌ی کلیدی «فایل کانفیگ rsync» ثبت شده است

اگر شما هم جزو لینوکس کاران باشید میدانید راهای زیادی برای انتقال اطلاعات بین دو کامپیوتر وجود دارد و راهای بسیار جالبی که در ویندوز از آنها محرومیم خوب یکی از روش های که من به آن علاقه مندم rsync است . این برنامه کوچک لینوکسی قابلیت های زیادی به شما میدهد برای مثال شما میتوانید حجم زیادی از اطلاعات را با سرعت بالا منتقل کنید . پشتیبانی این برنامه از فایل های sparse هم قابل توجه است این فایل ها فایل هایی هستند که اگر شما برای مثال یک ماشین مجازی داشته باشید و در آن ماشین مجازی یک سیستم عامل نصب کرده باشید و حجم دیسک را 100 گیگ انتخاب کرده باشید ولی سیستم عامل شما در حقیقت 3 گیگ جا اشغال کرده باشد در صورت نیاز به انتقال با برنامه های عادی شما نیاز به 100 گیگ فضا داردید اما این برنامه میتواند حجم واقی را از مجازی تشخیص دهد . شما نیاز به 

آموزش کاربردی Rsync
نگارش یافته توسط علی رضا حبیبی   
Rsync  یک برنامه ساده  و کوچک، همانند یک برنامه انتقال فایل می باشد، آخرین نسخه ای که در زمان نوشتن این مقاله وجود دارد نسخه  3.0.4 می باشد که می توانید از سایت رسمی آن با آدرس http://samba.anu.edu.au/rsync   دریافت کنید.
با این برنامه حتی می توان فقط تغییراتی که بر روی یک فایل  انجام شده را انتقال داد ، در نظر بگیرید که شما سروری دارید که مدام مجبورید اطلاعات آن را با سرور دیگر بروز رسانی کنید، برای این کار لازم نیست هر بار اطلاعات مورد نظر را تماما انتقال دهید ، با کمک Rsync  می توان فقط فایل هایی را که در کامپیوتر مبدا و یا مقصد تغییر کرده اند، انتقال داد. این کار فواید بسیاری از جمله ، آپلود سریع تر و کم تر شدن ترافیک شبکه و صرفه جویی در پهنای باند مصرفی  را  بدنبال خواهد داشت.

در واقع  Rsync  یک برنامه چند منظوره است که کارهای Backup  & Mirroring   را برای شما انجام خواهد داد.

بعضی امکانات Rsync   عبارت اند از :
  • پشتیبانی از حفظ اطلاعات فایل منتقل شده ، همانند مالک فایل ، گروه فایل ، مجوز های فایل و ...
  • عدم نیاز به مجوز کاربر ریشه
  • ترانسپرنت شدن با پروتکل هایی نظیر ssh  ، rsh
  • قابلیت استفاده بصورت anonymous  یا authenticated

برای شروع ابتدا مطمئن شوید که برنامه مورد نظر در سیستم شما نصب میباشد من از CentOS 5.2 استفاده می کنم کلیه دستوران نوشته شده در این مقاله هم بر اساس همین توزیع می باشد.

برای این کار می توانید دستور rsync را اجرا کنید و یا خروجی دستور زیر را ببینید :
#rpm  -q  rysnc
و خروجی بمانند زیر خواهد بود :
[root@centos-test ~]# rpm -q rsync
rsync-3.0.4-1.el5.rf
[root@centos-test ~]#

در صورتی که برنامه در سیستم شما نصب نبود از دستور زیر استفاده کنید :
#yum install rsync

در دیگر توزیع ها همانند fedora , redhat , debian  هماند بالا میباشد با این تفاوت که در debian می بایست از apt-get  استفاده نمایید .
برای راه اندازی شما باید Rsync  را بصورت Daemon   راه اندازی کنید این کار با استفاده از دستور  rsync –daemon   در کنسول انجام خواهد شد، فایل پیکربندی آن بصورت پیشفرض در آدرس  etc/rsyncd.conf/  قرار دارد و با کمک Vi  می توانید آن را ویرایش کنید. تمام دستگاهایی که برنامه Rsync  را اجرا کنند می توانند با دستگاهی که  در آن Rsync Daemon  راه اندازی شده است همزمان شودند.
برای این کار شما باید یک دستگاه را که قرار است نقش سرور را بازی کند بصورت Daemon  راه اندازی کنید همچنین کلاینت ها نیز می توانند برای بر قراری یک ارتباط دو طرفه در حالت daemon  راه اندازی شوند.
در زیر بعضی از فایل های پیکربندی Rsync  را مشاهده می کنید

motd file = /etc/rsyncd.motd
log file = /var/log/rsyncd.log
pid file = /var/run/rsyncd.pid
lock file = /var/run/rsync.lock

[simple_path_name]
path = /rsync_files_here
comment = My Very Own Rsync Server
uid = nobody
gid = nobody
read only = no
list = yes
auth users = username
secrets file = /etc/rsyncd.scrt



  • هر گاه شما بخواهید پیامی را به کاربران خود نمایش بدهید می توانید از فایل etc/rsyncd.motd/   استفاده کنید.
  • فایل بعدی فایلی است که لاگ های مربوط به Rsync  در آن ریخته خواهد شد.
  • دو فایل بعدی برای شماره پردازش Rsync  و قفل کردن آن می باشد.
  • در فایل پیکربندی گزینه Path  مسیری را که فایل ها به/ از آن همزمان خواهند شد را نشان می دهد,
  • Comment مختصری توضیح راجع به مسیر داده شده خواهد بود،
  • Uid &Gid  نام کاربری و گروهی که Rsync  با ان اجرا خواهد شد را نشان می دهد،
  • اگر شما بخواهید دسترسی را به یوزری که از قبل تعریف شده محدود کنید می توانید Auth Users  را برای آن مشخص کنید که این کار با فایلSecret  که بصورتplaintext   نوشته می شود و با فرمت Key/value  که نشان دهنده نام کاربری و رمز عبور است، انجام می شود.
rsync remote-update  یک پروتکل برای Rsync  می باشد که تنها  تغییرات صورت گرفته را بین دو ماشین منتقل می کند.
دو راه برای اتصال rsync  برای انتقال فایل وجود دارد ، یک راه استفاده از  برنامه های remote-shell  مانند ssh   بعنوان انتقال است و راه دیگر اتصال به daemon  برنامه Rsync  توسط پروتکل tcp  می باشد.
انتقال با remote-shell  هر زمانی که  مسیر های منبع و مقصد  محتوی  تنها یک کالن ( : ) بعد از مشخص کردن میزبان باشند استفاده میشود ،  اتصال به daemon  برنامه Rsync   تنها زمانی که مسیر های منبع و مقصد محتوی دو کالن  ( :: ) بعد از مشخص کردن میزبان  باشند و یا بکار بردن عبارت //:rsync  استفاده میشود


استفاده از Rsync  :

شما می توانید  با مشخص کردن مبدا و مقصد به راحتی از این برنامه استفاده کنید برای مثال :
rsync  -t *.c foo:src/
شاید این کلی ترین دستور برای  rsync   باشد ، در این دستور تمامی پترن های  شامل c.*  به سیستمی با نام foo ، و به دایرکتری src  انتقال پیدا می کند , اگر فایلی با نام مشابه در دایرکتری مقصد وجود داشته باشد ، rsync remote-update protocol برای بروز رسانی آن فایل ها تنها با فرستادن تغییرات شروع به کار می کند

rsync -avz foo:src/bar  /data/tmp

این دستور بصورت recursive  تمامی فایل های دایرکتری  src/bar را  در ماشین foo   به دایرکتری   data/tmp  در ماشین فعلی کپی می کند. فایل ها در حالت آرشیو منتقل می شوند که این کار باعث می شود تمامی مجوز ها و اطلاعات فایل در حین انتقال حفظ شود.
همچنین فشرده سازی باعث کاهش حجم فایل در حین انتقال خواهد شد.


در آخر هم سعی می کنیم یک مثال پیچیده را با هم بررسی کنیم

rsync --verbose  --progress --stats --compress --rsh=/usr/local/bin/ssh --recursive --times --perms --links --delete /www/* webserver:simple_path_name

خوب این دستور rsync   را با گزینه های verbose , progress , stats  راه اندازی می کند
گزینه Compress برای فشرده سازی اطلاعات و گزینه rsh  برای انتقال اصلاعات بصورت امن از طریق ssh  خواهد بود، خط بعد مشخص می کند که Rsync  چطور با فایل های  رفتار کند:
Recursive  به آن  می گوید که فایل ها و دایرکتری ها را بصورت recursive  انتقال دهد و اطلاعات زمان و مجوز ها را با دو عبارت times  و perms  انتقال دهد همچنین عبارت links نشان دهنده  کپی کردن symbolic ها و عبارت Delete  نشان دهنده پاک کردن دایرکتری و فایل ها در دایرکتری مقصد که در سیستم فعلی حذف شده اند می باشد */www/ فایل هایی از سیستم من که قرار است توسط Rsync  انتقال داده شوند را نشان می دهد و webserver:simple_path_name آدرس کامپیوتر مقصد خواهد بود.
با اجرای دستور فوق تمامی فایل های شاخه www  من به کامپیوتر webserver   با حفظ تمامی مجوز ها و اطلاعات ، انتقال پیدا خواهد کرد.

If the host computer is not running SSH (or RSH), we can configure and run rsync as a daemon in this computer. This would have rsync listening to the port 873 for incoming connections from other computers utilizing rsync. While this is not recommended for the transfer of files across unsecured networks, such as the Internet, because the actual data transfer is not encrypted, we can use this to keep information synchronized between different computers in internal networks, as well as perform backups.

There are two different approaches to have rsync running as a daemon, one is to launch the program with the --daemon parameter, and the other is to have inetd or xinetd to launchrsync and have it running as the other services that inetd and xinetd handles. But first, we must configure the file /etc/rsyncd.conf and create a file named rsyncd.secrets in /etc with the different usernames and passwords that will be allowed to connect to the rsync daemon.

As an example I am going to make available a folder called Documents inside my home folder (/home/juan) and show how to use a command to copy a directory from a different computer. All the uses that were covered in the post Synchronizing folders with rsync can be done with the rsync daemon, the only thing that changes is the addressing of either the source folder or the destination folder, whichever is the one that resides remotely.

Topics
Configuring rsyncd.conf
Creating the secrets file
Launching rsync with the --daemon attribute
Using inetd to handle the rsync daemon
Using xinetd to handle the rsync daemon
Connecting to the rsync daemon

Configuring rsyncd.conf

This file is located in the directory /etc, if it doesn't already exists, we need to create it there. We open the file in our preferred text editor, I am going to use gedit for the examples but we can use any editor such as kate in KDE, nano in a terminal, Vim, etc.

sudo gedit /etc/rsyncd.conf

In this file we add the following lines:

lock file = /var/run/rsync.lock
log file = /var/log/rsyncd.log
pid file = /var/run/rsyncd.pid

[documents]
    path = /home/juan/Documents
    comment = The documents folder of Juan
    uid = juan
    gid = juan
    read only = no
    list = yes
    auth users = rsyncclient
    secrets file = /etc/rsyncd.secrets
    hosts allow = 192.168.1.0/255.255.255.0

We can divide this file in two sections, the global parameters and the modules section. The global parameters define the overall behavior of rsync. Besides the three parameters that I use here and which I explain below, we can also configure things such as the port rsync will listen too, but we are going to go with the default 873.

  • lock file is the file that rsync uses to handle the maximum number of connections
  • log file is where rsync will save any information about it's activity; when it started running, when and from where does other computers connect, and any errors it encounters.
  • pid file is where the rsync daemon will write the process id that has been assigned to it, this is useful because we can use this process id to stop the daemon.

After the global parameters, we have the modules section, every module is a folder that we share with rsync, the important parts here are:

  • [name] is the name that we assign to the module. Each module exports a directory tree. The module name can not contain slashes or a closing square bracket.
  • path is the path of the folder that we are making available with rsync
  • comment is a comment that appears next to the module name when a client obtain the list of all available modules
  • uid When the rsync daemon is run as root, we can specify which user owns the files that are transfer from and to.
  • gid This allows us to set the group that own the files that are transferred if the daemon is run as root
  • read only determines if the clients who connect to rsync can upload files or not, the default of this parameter is true for all modules.
  • list allows the module to be listed when clients ask for a list of available modules, setting this to false hides the module from the listing.
  • auth users is a list of users allowed to access the content of this module, the users are separated by comas. The users don't need to exist in the system, they are defined by the secrets file.
  • secrets file defines the file that contains the usernames and passwords of the valid users for rsync
  • hosts allow are the addresses allowed to connect to the system. Without this parameter all hosts are allowed to connect.

Creating the secrets file

Once rsyncd.conf is properly set, we need to create the secrets file. This file contains all of the usernames and passwords that will be able to log in to the rsync daemon, this usernames and passwords are independent of the user that exist in the system, so we can create users whom already exist in the system without problems. As we specified the file/etc/rsyncd.secrets in rsyncd.conf, we will create and edit this file it in our favorite text editor:

sudo gedit /etc/rsyncd.secrets

In this file we add the usernames and the passwords, one per line, separated by a colon (I don't actually use passwords that are this simple, and you shouldn't either):

rsyncclient:passWord
juan:PassWord
backup:Password
user:password

Finally, change the permission of this file so it can't be read or modified by other users, rsyncwill fail if the permissions of this file are not appropriately set:

sudo chmod 600 /etc/rsyncd.secrets

Launching rsync with the --daemon attribute

Once everything is set, one of the ways to use rsync as a daemon is launching it with the --daemon parameter, if you followed the previous instructions you can simply use this command:

sudo rsync --daemon

We can check if it is running by seeing the log file that we defined in rsyncd.conf, in our example this is located in /var/log/rsyncd.log. Additionally, if the daemon is running, the file/var/run/rsyncd.pid will contain the process ID of rsync.

If we launched rsync in this manner, we can stop it by killing its process. We can obtaining the process ID by reading the contents of the file /var/run/rsyncd.pid and then invoke kill with this process ID. We can pass it directly to kill using:

sudo kill `cat /var/run/rsyncd.pid`

Using inetd to handle the rsync daemon

inetd, the InterNET Daemon, can handle all the services associated with Internet, such as FTP, telnet, and e-mail. While inetd is still used, due to security concerns it is being replaced by other more modern alternatives, a very popular one is xinetd (eXtended InterNET Daemon). Since the rsync daemon works using an Internet connection, we can add it to inetd or xinetdand allow either of them to handle it.

To enable rsync in inetd, we need to open the file /etc/inetd.conf in our favorite text editor and add the following line to it, assuming rsync is in /usr/bin as it should be in Linux distributions:

sudo gedit /etc/inetd.conf

Then add the following line:

rsync stream tcp nowait root /usr/bin/rsync rsync --daemon

When using inetd we need to get sure that the port 873 is appropriately mapped to rsync in the file /etc/services, by default it must be, we can check using:

cat /etc/services | grep rsync

It should show us this:

rsync 873/tcp

If you don't see this, then open the file /etc/services in a text editor and add that line.

Finally, restart the inetd daemon:

killall -1 inetd

Using xinetd to handle the rsync daemon

xinetd, the eXtended InterNET daemon, is a widely adopted replacement for inetd, as inetddoesn't offer security mechanisms. The handling of services is different from inetdxinetd may already have an entry for rsync that just needs to be enabled, the rsync configuration resides in the file /etc/xinetd.d/rsync, open this file in your text editor:

sudo gedit /etc/xinetd.d/rsync

and change the line disable = yes to disable = no.

If this file doesn't already exist, you can create it and edit it:

sudo gedit /etc/xinetd.d/rsync

And add the following lines to it:

service rsync
{
    disable         = no
    socket_type     = stream
    port            = 873
    protocol        = tcp
    wait            = no
    user            = root
    server          = /usr/bin/rsync
    server_args     = --daemon
    log_on_failure  += USERID
}

Unlike inetdxinetd doesn't need to have an entry in /etc/services, it can handle the port/protocol by itself. If rsync is defined in /etc/services, the lines port and protocol can be omitted. And now restart the xinetd daemon:

killall -1 xinetd

Connecting to the rsync daemon

To connect to rsync when it is running as a Daemon, instead of use a colon as we do when using SSH, we need to use a double colon, followed by the module name, and the file or folder that we want to copy or synchronize, we can use:

rsync -rtv user@host::module/source/ destination/

Another way to access the file would be using rsync:// followed by the host's address, the module, and finally the location of file or folder that we want to access:

rsync -rtv rsync://user@host/module/source/ destination/

For example, taking the parameters given in the example of rsyncd.conf that I posted, a way to transfer a folder called "source" inside the folder /home/juan/Documents of the host computer, would be using any of this two parameters, assuming the host is located at192.168.1.100

rsync -rtv juan@192.168.1.100::documents/source/ destination/
rsync -rtv rsync://juan@192.168.1.100/documents/source/ destination/

Just remember that the user that appears there is one of the users that we defined in/etc/rsyncd.secrets and not a user of the host computer.

  • ehsan gholami