1

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.

David162795
  • 135
  • 1
  • 9

1 Answers1

4

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.

MadHatter
  • 78,442
  • 20
  • 178
  • 229