Linux – Tar – split files

compressionlinuxtar

i am using the following command to backup and sql file:

tar -zcvf "$BACKUP_DST/$FILE_NAME.tgz" "$BACKUP_DST/$FILE_NAME.sql"

i want to make sure the compressed file wont be larger then 300mb, if it exceeds 300mb, split it into several files.

any thoughts?

Best Answer

I don't think tar has functionality built-in to split at an arbitrary size (there is -M for multi-volume, but this relies on the destination media detecting an out-of-space condition), but there are two things you could do:

  1. Generate a single tar.gz file in the usual way, then use the split command to divide it into sections (and cat to join them back together for decompression).
  2. Use dar instead of tar, since this has splitting functionality built in (amongst a lot of other features that tar doesn't have). For example

    $ dar -c "$BACKUPNAME" -g "$INPUTFILE.sql" -s300M -z