레이블이 sync인 게시물을 표시합니다. 모든 게시물 표시
레이블이 sync인 게시물을 표시합니다. 모든 게시물 표시

20130613

알림형 sync 만들어 보기...

서버간의 실시간 동기화를 해야 하는데....

무작정 cron으로 rsync를 하기 보다는 inotify를 사용하여 동기화를 시도해보았다.

물론 이건 엄청난 단점을 가지고 있다.. ㅡㅡ;;

그건 알아서들 분산하거나 보완하기 바란다.


간단한 예제를 만들어 보았다.

소스는 간단하니... 뭐.. 설명할 게 없다.
 

#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/inotify.h>
#define EVENT_SIZE  ( sizeof (struct inotify_event) )
#define BUF_LEN     ( 1024 * ( EVENT_SIZE + 16 ) )
int main( int argc, char **argv ) {
    char target[20]; /* monitoring directory name */
    int fd;
    int wd; /* watch desc */
    char buffer[BUF_LEN];
    fd = inotify_init();
    if (fd < 0) {
        perror("inotify_init");
    }
    if (argc < 2) {
        fprintf (stderr, "Watching\n");
        strcpy (target, ".");
    } else {
        fprintf (stderr, "Watching '%s' directory\n", argv[1]);
        strcpy (target, argv[1]);
    }
    wd = inotify_add_watch(fd, "/root/inotify", IN_MODIFY | IN_CREATE | IN_DELETE | IN_MOVED_FROM | IN_MOVED_TO | IN_MOVE_SELF | IN_CLOSE_WRITE);
    while(1) {
        int length, i = 0;
        length = read(fd, buffer, BUF_LEN);
        if (length < 0) {
            perror("read");
        }
        while (i < length) {
            struct inotify_event *event = (struct inotify_event *) &buffer[i];
            printf ("[debug] wd=%d mask=%d cookie=%d len=%d dir=%s\n", event->wd, event->mask, event->cookie, event->len, (event->mask & IN_ISDIR)?"yes":"no");
            if (event->len) {
                switch (event->mask) {
                    case IN_CLOSE_WRITE:
                        if (event->mask & IN_ISDIR) {
                            printf("The directory %s was created[IN_CLOSE_WRITE].\n", event->name);
                        } else {
                            printf("The file %s was created[IN_CLOSE_WRITE].\n", event->name);
                        }
                        break;
                   
                    case IN_CREATE:
                        if (event->mask & IN_ISDIR) {
                            printf("The directory %s was created[IN_ISDIR]\n", event->name);
                        } else {
                            printf("The file %s was created[IN_ISDIR].\n", event->name);
                        }
                        break;
                    case IN_DELETE:
                        if (event->mask & IN_ISDIR) {
                            printf("The directory %s was deleted[IN_DELETE].\n", event->name);
                        } else {
                            printf("The file %s was deleted[IN_DELETE].\n", event->name);
                        }
                        break;
                    case IN_MODIFY:
                        if (event->mask & IN_ISDIR) {
                            printf("The directory %s was modify[IN_MODIFY].\n", event->name);
                        } else {
                            printf("The file %s was modify[IN_MODIFY].\n", event->name);
                        }
                        break;
               
                    case IN_MOVED_FROM || IN_MOVED_TO || IN_MOVE_SELF:
                        if (event->mask & IN_ISDIR) {
                            printf("The directory %s was moved[MOVE].\n", event->name);
                        } else {
                            printf("The file %s was moved[MOVE].\n", event->name);
                        }
                        break;
                }
            }
            i += EVENT_SIZE + event->len;
        }
    }
  /*
  inotify_rm_watch(fd, wd);
  close(fd);
  */
  return 0;
}

Articles