0

So, I'm a complete newbie when it comes to scripting, like this is my first one. I have created the following script:

# This simply declares which states we are using to create tarballs from
for blah in ar ky ms ny; 
do 
    # This will go into each tomcat-state directory and create a tarball in /home/ec-user for all files older than 90 days
    find /var/www/apps/tomcat-${blah}/logs -type f -mtime +90 | xargs tar -cvzf /home/ec2-user/archive-${blah}.tar.gz; 

    # This creates a master archive tarball containing all the archive-state.tar.gz files
    tar -cvzf /home/ec2-user/ebd-log-archives.tar.gz /home/ec2-user/archive-${blah}.tar;

    # Since in step one, we archives all files older than 90 days, this step removes them.
    # find /var/www/apps/tomcat-${blah}/logs -type f  -mtime +90 -exec rm {} \;

    # And since we have the master archive file, this removes the archive-state files
    rm /home/ec2-user/archive-${blah}.tar.gz;
done

I have commented out the deleting of the logs for testing.

The problem is, when I get the master tarball, it only has NY in it, or whichever I put as my last in the loop (I tested this to make sure it wasn't just a odd NY thing)

Not sure what's going on, I tested it against an older script SIMILAR to this in wording on that server, and that one runs fine.

Edit: Thanks for the responses, I have updated the script to:

# This creates the master tarball we will be using
tar -cvzf /home/ec2-user/ebd-log-archives.tar.gz

# This simply declares which states we are using to create tarballs from
for blah in ar ky ms ny; 
do 
    # This will go into each tomcat-state directory and create a tarball in /home/ec-user for all files older than 90 days
    find /var/www/apps/tomcat-${blah}/logs -type f -mtime +90 | xargs tar -cvzf /home/ec2-user/archive-${blah}.tar.gz; 

    # This creates a master archive tarball containing all the archive-state.tar.gz files
    tar -rvzf /home/ec2-user/ebd-log-archives.tar.gz /home/ec2-user/archive-${blah}.tar;

    # Since in step one, we archives all files older than 90 days, this step removes them.
    # find /var/www/apps/tomcat-${blah}/logs -type f  -mtime +90 -exec rm {} \;

    # And since we have the master archive file, this removes the archive-state files
    rm /home/ec2-user/archive-${blah}.tar.gz;
done

I'll try running with this and posting an update

1
  • Try without the semi-colon after the ny.
    – harrymc
    Commented Jun 3, 2019 at 18:23

1 Answer 1

1

Every time you run tar -c, the tool creates a new archive. If you tell it to use an existing name (ebd-log-archives.tar.gz in your case), the old archive will be overwritten.

You can create an empty archive ("a master archive") before the loop and then tar -r (instead of tar -c) inside the loop.

From man 1 tar:

-c, --create
Create a new archive. […]

[…]

-r, --append
Append files to the end of an archive. Arguments have the same meaning as for -c (--create).

1
  • I'll update the primary post with what I got in there now to verify
    – dragos_kai
    Commented Jun 3, 2019 at 19:51

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .