linux

如何用inotify实现实时备份

小樊
48
2025-06-05 02:18:57
栏目: 编程语言

inotify 是 Linux 系统中的一个内核子系统,它可以监控文件系统事件,如文件的创建、修改、删除等。要使用 inotify 实现实时备份,你可以使用 inotifywait 命令或者编写一个使用 inotify API 的程序。下面是两种方法的简要说明:

方法一:使用 inotifywait 命令

  1. 安装 inotify-tools 软件包(如果尚未安装):
sudo apt-get install inotify-tools  # 对于基于 Debian 的系统
sudo yum install inotify-tools      # 对于基于 RHEL 的系统
  1. 创建一个备份脚本(例如 backup.sh),并添加以下内容:
#!/bin/bash

SOURCE_DIR="/path/to/source"  # 源目录
BACKUP_DIR="/path/to/backup"  # 备份目录

# 使用 inotifywait 监控源目录中的文件变化
inotifywait -m -r -e create,modify,delete --format '%w%f' "${SOURCE_DIR}" | while read FILE
do
  # 获取文件的相对路径
  RELATIVE_PATH="${FILE#$SOURCE_DIR}"

  # 在备份目录中创建相同的目录结构
  mkdir -p "${BACKUP_DIR}/${RELATIVE_PATH%/*}"

  # 复制文件到备份目录
  cp "$FILE" "${BACKUP_DIR}/${RELATIVE_PATH}"
done
  1. 为脚本添加可执行权限:
chmod +x backup.sh
  1. 运行脚本:
./backup.sh

现在,每当源目录中的文件发生变化时,脚本都会将其复制到备份目录。

方法二:使用 inotify API 编写程序

  1. 安装 libinotify-dev 软件包(如果尚未安装):
sudo apt-get install libinotify-dev  # 对于基于 Debian 的系统
sudo yum install libinotify-devel      # 对于基于 RHEL 的系统
  1. 使用 C 语言编写一个使用 inotify API 的程序。下面是一个简单的示例:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/inotify.h>
#include <unistd.h>

#define EVENT_SIZE  ( sizeof (struct inotify_event) )
#define BUF_LEN     ( 1024 * ( EVENT_SIZE + 16 ) )

int main(int argc, char **argv)
{
    int length, i = 0;
    int fd;
    int wd;
    char buffer[BUF_LEN];

    // 检查命令行参数
    if (argc != 3) {
        printf("Usage: %s <source_dir> <backup_dir>\n", argv[0]);
        exit(EXIT_FAILURE);
    }

    // 初始化 inotify
    fd = inotify_init();
    if (fd < 0) {
        perror("inotify_init");
        exit(EXIT_FAILURE);
    }

    // 添加要监控的目录
    wd = inotify_add_watch(fd, argv[1], IN_CREATE | IN_MODIFY | IN_DELETE);
    if (wd < 0) {
        perror("inotify_add_watch");
        exit(EXIT_FAILURE);
    }

    // 读取 inotify 事件
    while (1) {
        length = read(fd, buffer, BUF_LEN);
        if (length < 0) {
            perror("read");
            exit(EXIT_FAILURE);
        }

        while (i < length) {
            struct inotify_event *event = (struct inotify_event *) &buffer[i];
            char *filename = event->name;

            // 跳过目录
            if (filename[0] == '.') {
                i += EVENT_SIZE + filename[1] * sizeof(char);
                continue;
            }

            // 构建源文件和备份文件的完整路径
            char source_path[PATH_MAX];
            char backup_path[PATH_MAX];
            snprintf(source_path, PATH_MAX, "%s/%s", argv[1], filename);
            snprintf(backup_path, PATH_MAX, "%s/%s", argv[2], filename);

            // 根据事件类型执行相应的操作
            if (event->mask & IN_CREATE) {
                printf("File %s created\n", source_path);
                // 复制新创建的文件到备份目录
                system("cp \"$(realpath \"$source_path\")\" \"$backup_path\"");
            } else if (event->mask & IN_MODIFY) {
                printf("File %s modified\n", source_path);
                // 复制修改后的文件到备份目录
                system("cp \"$(realpath \"$source_path\")\" \"$backup_path\"");
            } else if (event->mask & IN_DELETE) {
                printf("File %s deleted\n", source_path);
                // 删除备份目录中的文件
                system("rm \"$backup_path\"");
            }

            i += EVENT_SIZE + filename[1] * sizeof(char);
        }
        i = 0;
    }

    // 移除监控并关闭 inotify 文件描述符
    inotify_rm_watch(fd, wd);
    close(fd);

    exit(EXIT_SUCCESS);
}
  1. 编译程序:
gcc -o inotify_backup inotify_backup.c
  1. 运行程序:
./inotify_backup /path/to/source /path/to/backup

现在,每当源目录中的文件发生变化时,程序都会将其复制到备份目录。

这两种方法都可以实现实时备份。你可以根据自己的需求选择合适的方法。

0
看了该问题的人还看了