I'd like to call mysqldump to create multiple backup files and then archive them in one tar.gz.

I am following this tutorial on mysqldump part: http://dev.mensfeld.pl/2013/04/backup-mysql-dump-all-your-mysql-databases-in-separate-files/ (at the end, it calls mysqldump in a loop for every database and creates one file at a time)

I can just save all files in temporary directory and 'then' put them into the archive, however, is there anyway how to skip this unnecessary temporary-directory part? I tried to learn about named pipes, thinking they may be what I need, however, haven't really understood them.


You could use the -r flag to tar to append a file to an existing archive. Since it's legal to append to an empty or non-existent archive, you could modify the looped instruction to be something like

$MYSQLDUMP --force --opt --user=$MYSQL_USER -p$MYSQL_PASSWORD --databases $db > "$BACKUP_DIR/mysql/$db"
tar rvf /path/to/tar.file "$BACKUP_DIR/mysql/$db" && rm "$BACKUP_DIR/mysql/$db"

The file will have to exist temporarily, as it needs to have a filename inside the tar index for extraction purposes (if you sent all the files to it via a pipe, think about how they might extract) - but you don't need to collect them all together then tar them all up.

|improve this answer|||||

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.