如何记录使用 crontab 的脚本运行的 rsync 备份的输出?

如何记录使用 crontab 的脚本运行的 rsync 备份的输出?

我想记录 rsync 生成的所有输出;我自己在终端中运行脚本时看到的输出。我希望此记录从我的脚本内部进行。

这是我的备份脚本,它运行良好。BackupPC.sh:

#!/bin/bash

#current date and time to be used as filename
CDT=`date +%d-%b-%Y-%H-%M`

echo "[START]  $(date)" >> /home/pi/logs/${CDT}.txt
rsync "-avXP" "[email protected]:/home/bassam/Desktop" "/media/hitachi/backup"
echo "[END]  $(date)" >> /home/pi/logs/${CDT}.txt

这是我的 crontab 条目:

0 2 * * * ~/scripts/BackupPC.sh >/dev/null 2>&1

答案1

巴萨姆:

rsync 命令有 --log-file 选项。在手册中:

$ -> man -P cat rsync | grep 'log-file=FILE'
            --log-file=FILE         log what we're doing to the specified FILE

然后,如果我想将文件从源/示例移动到目标并将结果转储到结果文件中,例如:

rsync -avz source/Example destination/ --log-file=result

结果是:

)$ -> cat result 
2020/06/11 22:09:04 [13891] building file list
2020/06/11 22:09:04 [13891] >f+++++++++ Example
2020/06/11 22:09:04 [13891] sent 101 bytes  received 35 bytes  272.00 bytes/sec
2020/06/11 22:09:04 [13891] total size is 0  speedup is 0.00

或者您可以使用“--log-file=filename -q”它有更多信息:

cat result 
2020/06/11 22:20:47 [16240] building file list
2020/06/11 22:20:47 [16240] [sender] make_file(Example,*,0)
2020/06/11 22:20:47 [16240] send_file_list done
2020/06/11 22:20:47 [16240] send_files starting
2020/06/11 22:20:47 [16240] send_files phase=1
2020/06/11 22:20:47 [16240] send_files phase=2
2020/06/11 22:20:47 [16240] send files finished
2020/06/11 22:20:47 [16240] total: matches=0  hash_hits=0  false_alarms=0 data=0
2020/06/11 22:20:47 [16240] sent 61 bytes  received 12 bytes  146.00 bytes/sec
2020/06/11 22:20:47 [16240] total size is 0  speedup is 0.00
2020/06/11 22:20:47 [16240] [sender] _exit_cleanup(code=0, file=main.c, line=1196): about to call exit(0)

编辑:

这也可能有帮助:是否可以让 rsync 在远程系统上创建日志文件?

您的日志文件中还有其他有用的信息:

$ -> rsync --info=help
Use OPT or OPT1 for level 1 output, OPT2 for level 2, etc.; OPT0 silences.

BACKUP     Mention files backed up
COPY       Mention files copied locally on the receiving side
DEL        Mention deletions on the receiving side
FLIST      Mention file-list receiving/sending (levels 1-2)
MISC       Mention miscellaneous information (levels 1-2)
MOUNT      Mention mounts that were found or skipped
NAME       Mention 1) updated file/dir names, 2) unchanged names
PROGRESS   Mention 1) per-file progress or 2) total transfer progress
REMOVE     Mention files removed on the sending side
SKIP       Mention files that are skipped due to options used
STATS      Mention statistics at end of run (levels 1-3)
SYMSAFE    Mention symlinks that are unsafe

ALL        Set all --info options (e.g. all4)
NONE       Silence all --info options (same as all0)
HELP       Output this help message

Options added for each increase in verbose level:
1) COPY,DEL,FLIST,MISC,NAME,STATS,SYMSAFE
2) BACKUP,MISC2,MOUNT,NAME2,REMOVE,SKIP

相关内容