1

I tried to tar mysql backup files as below:

find /data/mysqldata \( ! -name "*mysql-bin*" -a ! -name "*.log" \) |
    xargs tar -zcf /data/backup.tgz

Actually, I executed this command with Popen and waited to read the output through select-poll method, however, I found that sometimes the backup file suddenly became very small after it was already very large, just like that the tar procedure was restarted. This case usually happened when the target file was extremely large and I was quite confused about that.

7
  • What do you want this to do? tar -zcf /data/mysqldata would create an archive called /data/mysqldata...
    – Kusalananda
    Sep 5, 2017 at 9:09
  • That would find all files and directories (and other things) and (potentially) overwrite the tar archive multiple times with each of these. Tell us instead what it is you'd like to do.
    – Kusalananda
    Sep 5, 2017 at 9:18
  • sorry, I made a mistake for the command just now, I have already fixed it.
    – perse
    Sep 5, 2017 at 9:20
  • I just want to tar some files under /data/mysqldata into an archive automatically.
    – perse
    Sep 5, 2017 at 9:22
  • like tar -czf /data/backup.tgz /data/mysqdata but without the mysql-bin.nnnnnn files?
    – Kusalananda
    Sep 5, 2017 at 9:24

2 Answers 2

4

Using GNU tar:

tar -cz -f /data/backup.tgz --exclude '*.log' --exclude 'mysql-bin.*' /data/mysqldata 

The issue that you ran into was that xargs executed tar multiple (at least two) times. The second time tar was run, the backup file would "shrink" (get overwritten).

This is what xargs does. It executes a utility with as many arguments as possible, and when it get more arguments (filenames in your case), it chops the list off and continues with the list in another invocation of the utility.

The issue occured because you sent every single directory and filename to xargs, generating a very long list.

3
  • I was just testing this method, I was confused mainly because there were some factors that affect my estimation, such as pipe, file size, and tar command itself.
    – perse
    Sep 5, 2017 at 9:35
  • that is to say, when the list is very long, xargs will restart the utility from some intermediate item?
    – perse
    Sep 5, 2017 at 9:45
  • @perse Correct.
    – Kusalananda
    Sep 5, 2017 at 9:46
1

Pipe the list of files to the archiving command directly instead of xargs which could (and does in some of your case) split the list into several archiver invocations.

Here using the standard pax command though with -print0/-0 non-standard extensions supported by some for added reliability:

find /data/mysqldata ! -name "*mysql-bin*" ! -name "*.log" -print0 |
  pax -0wd | gzip > file.tar.gz

(also note the -d without which all files would be included anyway as archiving /data/mysqldata (which matches neither exclusion pattern) would mean archiving everything like in your tar approach).

Beside pax, many tar implementations support getting the file list from stdin, but usually with different interface between implementations.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .