Reputation: 95
When the following command runs from crontab tar errors occur when a file with spaces in the filename are encountered.
/tar -C / -zcvf /root/archive-of-files-with-spaces-in-filename.tar.gz `find /var/www/html -mmin -1450 -print | sed 's|^/||'`
Changing the switch to -print0 or removing the -print switch altogether does not solve the problem:
/tar -C / -zcvf /root/archive-of-files-with-spaces-in-filename.tar.gz `find /var/www/html -mmin -1450 -print0 | sed 's|^/||'`
OR
/tar -C / -zcvf /root/archive-of-files-with-spaces-in-filename.tar.gz `find /var/www/html -mmin -1450 | sed 's|^/||'`
The same errors are thrown by tar.
How can the command be re-written to allow filenames with spaces to be served to tar without being broken up into directory names that do not exist?
Sample of an error produced with filename: "2016-12-15 Name File Repair exportICS.php"
tar: var/www/html-calendar.northpawfamilycloud.xyz/pages/2016-12-15:
Cannot stat: No such file or directory
tar: Name: Cannot stat: No such file or directory
tar: File: Cannot stat: No such file or directory
tar: Repair: Cannot stat: No such file or directory
tar: exportICS.php: Cannot stat: No such file or directory
NOTE | sed 's|^/||'` strips leading slash off finds so tar error "removing leading slash does not occur.
Upvotes: 2
Views: 5981
Reputation: 755114
Since the only filtering you're doing is to remove leading slashes from names under /var/www/html
, you can use tar
alone — avoiding a myriad problems with find
:
tar -C / -czvf /root/archive-of-files-with-spaces-in-filename.tar.gz var/www/html
Given a directory, tar
backs up the contents of the directory. If you have sub-directories under /var/html
, your archive was probably twice as big as necessary. This one won't be bigger than necessary.
Apparently, the find
command includes options. In that case, you have to do things the other way:
(cd /; find var/www/html -type f -mmin -1450 -print0) |
tar -C / --null -T - --czvf /root/archive-of-files-with-spaces-in-filename.tar.gz
The cd /; find var/html
avoids leading slashes from find
. The -print0
uses null terminators for the file names. The --null
tells tar
to expect file names to be null-terminated; the -T -
says read the list of file names from standard input.
You can put all that on a single line if you wish. It isn't the way I'd do it. I would create a script that would be run by the cron
(crontab
) system that would do the job, especially since the output file name would inevitably be time-stamped if I was running it, and it would probably identify which site was being backed up, etc. The site to be backed up would be a parameter to the script, supplied by the crontab
entry. That's my preference; there are plenty of people who write long lines into their crontab
files.
Also, GNU tar
(noisily) omits the leading slash from archive names, so that there are never absolute names inside a tar
file generated by GNU tar
:
tar: Removing leading `/' from member names
That means you could avoid the sub-shell and simply use find /var/www/html …
to generate the names, living with the warning message. Since you have verbose output anyway, that's not likely to be much of a problem.
Upvotes: 2
Reputation: 5950
Archive with spaces:
Escape spaces with backslashes or quote the filename. Example:
tar czvf /my/path/filename\ with\ spaces.tgz files...
If the files to be archived have spaces in their names:
Use find ... -print0
to separate filenames with NULL tar ... --null
to only consider NULL terminated files. From GNU tar documentation:
find /path ... -print0 | tar ... --null -T -
Upvotes: 0