inotify
是 Linux 系統中的一個內核子系統,它可以監控文件系統事件,如文件的創建、修改、刪除等。要使用 inotify
實現實時備份,你可以使用 inotifywait
命令或者編寫一個使用 inotify
API 的程序。下面是兩種方法的簡要說明:
方法一:使用 inotifywait 命令
inotify-tools
軟件包(如果尚未安裝):sudo apt-get install inotify-tools # 對于基于 Debian 的系統
sudo yum install inotify-tools # 對于基于 RHEL 的系統
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
chmod +x backup.sh
./backup.sh
現在,每當源目錄中的文件發生變化時,腳本都會將其復制到備份目錄。
方法二:使用 inotify API 編寫程序
libinotify-dev
軟件包(如果尚未安裝):sudo apt-get install libinotify-dev # 對于基于 Debian 的系統
sudo yum install libinotify-devel # 對于基于 RHEL 的系統
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);
}
gcc -o inotify_backup inotify_backup.c
./inotify_backup /path/to/source /path/to/backup
現在,每當源目錄中的文件發生變化時,程序都會將其復制到備份目錄。
這兩種方法都可以實現實時備份。你可以根據自己的需求選擇合適的方法。