为 rsync 中的 --out-format 自定义 date/time 格式

custom date/time format for --out-format in rsync

根据man页,%t用于输出当前日期和时间--out-format。无论如何指定要用于 date/time 的格式?

$ rsync --out-format="%t" "/source" "/destination"
2019/01/19 23:12:57 | home/nacho/backup_test/source 1
2019/01/19 23:12:57 | home/nacho/backup_test/source 1/001
2019/01/19 23:12:57 | home/nacho/backup_test/source 1/002
2019/01/19 23:12:57 | home/nacho/backup_test/source 1/003
2019/01/19 23:12:57 | home/nacho/backup_test/source 1/100
2019/01/19 23:12:57 | home/nacho/backup_test/source 1/file 100
2019/01/19 23:12:57 | home/nacho/backup_test/source 1/folder
2019/01/19 23:12:57 | home/nacho/backup_test/source 1/folder/004
2019/01/19 23:12:57 | home/nacho/backup_test/source 2
2019/01/19 23:12:57 | home/nacho/backup_test/source 2/006

好像没有,没有。查看 rsync 源代码,%t 格式转义导致调用以下 timestring 函数,您可以看到它调用 strftime() 硬-编码格式字符串 "%Y/%m/%d %H:%M:%S":

char *timestring(time_t t)
{
        static char TimeBuf[200];
        struct tm *tm = localtime(&t);
        char *p;

#ifdef HAVE_STRFTIME
        strftime(TimeBuf, sizeof TimeBuf - 1, "%Y/%m/%d %H:%M:%S", tm);
#else
        strlcpy(TimeBuf, asctime(tm), sizeof TimeBuf);
#endif

        if ((p = strchr(TimeBuf, '\n')) != NULL)
                *p = '[=10=]';

        return TimeBuf;
}

您可以使用解决方法(基于 Jorge Marquez solution to add the current user to rsync log):

    $ rsync --archive --log-file=mylog --log-file-format='t:%t user:'$USER' myt:'$(date +%Y%m%d%H%M) source target

这将导致

    $ cat mylog
    ...
    2021/11/26 10:53:29 [275059] t:2021/11/26 10:53:29 user:user myt:202111261053
    ...

注意空格。