Search code examples
directorybackuptarbackup-strategies

How to create tar that gets updated for 2 weeks


I need to setup a bash script which tars and compresses a directory. The tar should be updated every day after that point with the same directory. This should happen for a duration of 14 days until the tar is finally removed and the process is restarted.

I could use a hand setting this up. Many thanks


Solution

  • You can use cron jobs to manage the archiving every day (at midnight or whatever). You can look it up using 'man cron'.

    One way to do this would be to make one cron job that archives the directory and updates it every day. Another cron job to compress that tarball.

    So for the first, something like:

    if [ ! -f directory.tgz ]; then
      tar cf directory.tgz directory_name/
    else
      tar uf directory.tgz directory_name/
    

    An example of a cron job that will run each day would look something like this:

    58 23 * * * script
    

    Which will run at the 23rd hour on the 58th minute (just prior to midnight). Obviously you will need to adjust this to suite your needs.

    EDIT: I should also add how to go about compressing it. It would be a smart idea to look up the advantages and disadvantages of the compression algorithms. For example, gzip will probably take less time to compress than bzip2; but the latter may provide a smaller compression (based on the data being compressed) than the former. They're usage is pretty simple, again use the 'man' pages for them. Here's an example:

    bzip2 -c directory.tar > directory.bz2