Score:0

How to compress a directory, using tar, and then delete the directory it compressed?

cd flag

On my server there is an application directory that I need to make a full copy of it but the copy takes up a lot of space, I need to compress it and then remove it. I already use the command

tar -czf bkp-mydir-$date.tar.gz mydir/

But I need it to be removed after the compression is done. In that case I could create a .sh file with the commands below:

 tar -czf bkp-mydir-$date.tar.gz mydir/
 rm -Rf mydir

I wanted to do this in Bacula.

Nikita Kipriyanov avatar
za flag
You don't need tar with bacula, it is a "tar" by itself, so to speak. And, bacula is a *backup* tool, not *data rearrange* tool. It is designed to copy data into its storage and leave it in place. If you want to do what you described, bacula is totally inappropriate tool. The power of solution like bacula stems from its scheduling, deduplication, filtering and other management; for the task as you described it, your "tar+rm" script (or some other way to archive files, like 7zip and so on) is probably the most appropriate way.
José Ferreira Neto avatar
cd flag
I understand how Bacula works (but thanks for the tip), but the idea is to compress the source directory and copy only the compressed file...
Nikita Kipriyanov avatar
za flag
Bacula/BareOS can store data compressed (enable compression in the corresponding FileSet resource of the director configuration). That compression happens on the client, so it is transferred to the storage already compressed, saving network bandwidth. // It won't remove files from the source though. You can schedule tasks with Client Run Before Job and Client Run After Job, which can create tar file, backup it and remove everything afterwards, but that's silly. Seriously, this is not how Bacula or BareOS is supposed to be used. This is intended to extract databases into flat files to backup.
Score:0
sm flag

Testing in my machine, the && worked very well in this case

tar -czf bkp-mydir-${date}.tar.gz mydir/ && rm -rf mydir/

You'll also need to use variable interpolation with the $date var. Check here: https://stackoverflow.com/questions/17622106/variable-interpolation-in-the-shell

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.