The command is getting truncated because %
has a special meaning in cron
. From man 5 crontab
:
The ``sixth'' field (the rest of the line) specifies the command to be
run. The entire command portion of the line, up to a newline or %
character, will be executed by /bin/sh or by the shell specified in the
SHELL variable of the crontab file. Percent-signs (%) in the command,
unless escaped with backslash (\), will be changed into newline charac‐
ters, and all data after the first % will be sent to the command as
standard input. There is no way to split a single command line onto
multiple lines, like the shell's trailing "\".
which explains why the logged command looks something like:
CMD (find /home/steeldriver/forums/tests -type f -mtime +1 -printf ')
To demonstrate that escaping works, given:
$ ls -l tests
total 12
-rw-rw-r-- 1 steeldriver steeldriver 0 Jan 22 14:22 A8eVAmK.txt
-rw-rw-r-- 1 steeldriver steeldriver 22 Oct 27 22:18 A9E27.txt
-rw-rw-r-- 1 steeldriver steeldriver 10 Oct 27 22:19 ffn2eG6.txt
-rw-rw-r-- 1 steeldriver steeldriver 43 Jan 22 14:22 sBHFgkv.txt
Then adding the crontab as:
* * * * * find /home/steeldriver/forums/tests -type f -mtime +1 -printf '\%P\0' | rsync -0av --exclude-from=- /home/steeldriver/forums/tests/ /home/steeldriver/forums/newtests/ 2>&1 > /home/steeldriver/forums/backup.log
(note this uses the null-terminated form \0
) produces the log file:
$ cat backup.log
sending incremental file list
created directory /home/steeldriver/forums/newtests
./
A8eVAmK.txt
sBHFgkv.txt
sent 250 bytes received 113 bytes 726.00 bytes/sec
total size is 43 speedup is 0.12
indicating that the backup ran successfully and copied only the newer files:
$ ls -l newtests
total 4
-rw-rw-r-- 1 steeldriver steeldriver 0 Jan 22 14:22 A8eVAmK.txt
-rw-rw-r-- 1 steeldriver steeldriver 43 Jan 22 14:22 sBHFgkv.txt
Note that you won't be able to test the escaping outside of your crontab - in the terminal, it will result in an error from find
.
%
is a special character tocron
(denoting end-of-line - which explains why the logged command is truncated) - try escaping it. . . -printf '\%P'
. Should have worked OK inside a script though. – steeldriver Jan 22 '17 at 18:14-printf '\%P\0'
and then eitherrsync -0avz --exclude-from=- . . .
or (equivalently)rsync -avz --from0 --exclude-from=- . . .
– steeldriver Jan 22 '17 at 19:26find: warning: unrecognized escape
%p'and then
rsync: send_files failed to open "/path/serveruser/.viminfo": Permission denied (13)` (even with root). After that it proceeds syncing files in path/serveruser/backup without giving any info of how many files it wants to sync. Really strange... – Ayys Jan 22 '17 at 19:50