LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Issue tarring data (https://www.linuxquestions.org/questions/linux-newbie-8/issue-tarring-data-254064/)

Johnny Mac 11-12-2004 12:55 PM

Issue tarring data
 
I have written a short script and created a daily cron to run it. The script runs well except for the 3rd line. Here is the script...
-------------------------
#!/bin/sh
tar -uf /backups/mysql/mysql.`date +%Y-%m-%d`.tar /var/lib/mysql
tar -uf /backups/mysql/mysql.appended.tar /backups/mysql/*.tar
gzip -c /backups/mysql/mysql.appended.tar > /backups/mysql/mysql.appended.tar.gz
cp -f /backups/mysql/mysql.appended.tar.gz /mnt/floppy/
--------------------------

As you can see, it takes all the data in my MySQL databases and makes a tar file using the date in the file name. Then it is supposed to take all the dated files and append them to a different tar file, only adding the dates files that don't already exist in this "mysql.appended.tar" file. It then takes the appeneded file, compresses it, then copies it to the floppy, overwriting the previous copy that existed on the floppy from the day before.

The problem I am having is that when creating the appended file from the dated files, instead of adding only the newest file to the archive, it is adding them all to the file and needlessly increasing the file. Am I using the incorrect switch in tar or should I be adding an additional switch? The man page is leading me to believe that my switches should be correct but I am obviously doing something incorrectly. Any thoughts or ideas would be greatly appreciated.

LasseW 11-12-2004 01:24 PM

I think there's a slight flaw in your logic here: the first tar command will create a new file. All files in the mysql directory will be new to the dated file, so the u flag isn't selective at all, it's the same as using the c flag. I'm also wondering what the second tar command does: note that the target file mysql.appended.tar will match the source pattern *.tar.

bigrigdriver 11-12-2004 02:33 PM

As far as the third line is concerned, if you want to compress the tarball, it's only necessary to enter the command gzip /backups/mysql/mysql.appended.tar. The result will be /backups/mysql/mysql.appended.tar.gz.
Or, you could add the -z option to the tar command to compress the tarball.

Johnny Mac 11-12-2004 02:41 PM

Quote:

Originally posted by LasseW
I think there's a slight flaw in your logic here: the first tar command will create a new file. All files in the mysql directory will be new to the dated file, so the u flag isn't selective at all, it's the same as using the c flag. I'm also wondering what the second tar command does
I wish to create a daily backup of the mysql datases:
#tar -uf /backups/mysql/mysql.`date +%Y-%m-%d`.tar /var/lib/mysql

The first day it runs it gives me:
mysql.2004-11-08.tar

Then the dated archive is tarred into a master archive named "mysql.appended.tar":
#tar -uf /backups/mysql/mysql.appended.tar /backups/mysql/*.tar
Resulting in "mysql.appended.tar.gz"

The next day the script runs again and creates "mysql.2004-11-09.tar".
Now I have two dated tar files in my backup directory:

mysql.2004-11-08.tar
mysql.2004-11-09.tar

When the second step of the script runs, it takes the existing tar files (*.tar) then and appends them to the master tar file (mysql.appended.tar). Because of the "u" switch, it should ignore the file from the 8th since it already exists in the master archive and add only the tar from the 9th.

The same thing happens on the 10th, creating "mysql.2004-11-10.tar". when the files are appended to the master archive the ones from the 8th and 9th should be ignored and only the one from the 10th should be added. And so on...


What I have instead is a master tar file that doubles in size each time the script runs. For instance, on the second day I found that the script was appending all the tar files to the master archive instead of only the newest one that was not already in the archive. When I extract the existing archive I found two copies of each dated archive file. I want to know why the "u" switch is not working as I thought it should.

[root@xxxxxxx floppy]# tar zxvf mysql.appended.tar.gz
backups/mysql/mysql.2004-11-08.tar
backups/mysql/mysql.2004-11-09.tar
backups/mysql/mysql.2004-11-08.tar
backups/mysql/mysql.2004-11-09.tar


Since my databases are small enough to fit a weeks worth of zipped backups onto a floppy this provides me a small level of security should my hdd fail. In case a floppy goes bad I also want to leave a copy of the dated archives on the hdd as well in case I need to restore the databases. I suppose I can simply move the dated archive file, once added to the master archive, to a different directory so it will not be added again to the master archive the next day but I don't understand why it is duplicating the dated files in the first place.

Quote:

Originally posted by LasseW
note that the target file mysql.appended.tar will match the source pattern *.tar.
I thought of that but if it did, why does it not appear in the extracted file list when I extract the master archive? Instead of...

[root@xxxxxxx floppy]# tar zxvf mysql.appended.tar.gz
backups/mysql/mysql.2004-11-08.tar
backups/mysql/mysql.2004-11-09.tar
backups/mysql/mysql.2004-11-08.tar
backups/mysql/mysql.2004-11-09.tar

Why do I not see...

[root@xxxxxxx floppy]# tar zxvf mysql.appended.tar.gz
backups/mysql/mysql.2004-11-08.tar
backups/mysql/mysql.2004-11-09.tar
backups/mysql/mysql.appended.tar
backups/mysql/mysql.2004-11-08.tar
backups/mysql/mysql.2004-11-09.tar
backups/mysql/mysql.appended.tar

Just curious as to why it is doing what it is doing. TIA! :)

Johnny Mac 11-12-2004 02:53 PM

Quote:

Originally posted by bigrigdriver
As far as the third line is concerned, if you want to compress the tarball, it's only necessary to enter the command gzip /backups/mysql/mysql.appended.tar. The result will be /backups/mysql/mysql.appended.tar.gz.
Or, you could add the -z option to the tar command to compress the tarball.

I use the "-c" because I want to keep the original file as well as the zip. If I just gzip it will simply convert the original file, no? Or am I even more confused than I thought? :D

Johnny Mac 11-12-2004 02:58 PM

I modified the script so that only the dated tar files are appended to the master archive

----------------------------------------------
#!/bin/sh
tar -uf /backups/mysql/mysql.`date +%Y-%m-%d`.tar /var/lib/mysql
tar -uf /backups/mysql/mysql.appended.tar /backups/mysql/mysql.200*.tar
gzip -c /backups/mysql/mysql.appended.tar > /backups/mysql/mysql.appended.tar.gz
cp -f /backups/mysql/mysql.appended.tar.gz /mnt/floppy/
-----------------------------------------------

That should resolve the issue of copying the master tar file into itself each day. For all I know this alone may have resolved my whole issue. What do you think? I suppose tomorrow I will find out after the script runs.


All times are GMT -5. The time now is 12:18 PM.