Search code examples
bashshellarchive

Bash script to archive files and then copy new ones


Need some help with this as my shell scripting skills are somewhat less than l337 :(

I need to gzip several files and then copy newer ones over the top from another location. I need to be able to call this script in the following manner from other scripts.

exec script.sh $oldfile $newfile

Can anyone point me in the right direction?

EDIT: To add more detail:

This script will be used for monthly updates of some documents uploaded to a folder, the old documents need to be archived into one compressed file and the new documents, which may have different names, copied over the top of the old. The script needs to be called on a document by document case from another script. The basic flow for this script should be -

  1. The script file should create a new gzip archive with a specified name (created from a prefix constant in the script and the current month and year e.g. prefix.september.2009.tar.gz) only if it does not already exist, otherwise add to the existing one.
  2. Copy the old file into the archive.
  3. Replace the old file with the new one.

Thanks in advance,
Richard

EDIT: Added mode detail on the archive filename


Solution

  • Here's the modified script based on your clarifications. I've used tar archives, compressed with gzip, to store the multiple files in a single archive (you can't store multiple files using gzip alone). This code is only superficially tested - it probably has one or two bugs, and you should add further code to check for command success etc. if you're using it in anger. But it should get you most of the way there.

    #!/bin/bash
    
    oldfile=$1
    newfile=$2
    
    month=`date +%B`
    year=`date +%Y`
    
    prefix="frozenskys"
    
    archivefile=$prefix.$month.$year.tar
    
    # Check for existence of a compressed archive matching the naming convention
    if [ -e $archivefile.gz ]
    then
        echo "Archive file $archivefile already exists..."
        echo "Adding file '$oldfile' to existing tar archive..."
        
        # Uncompress the archive, because you can't add a file to a
        # compressed archive
        gunzip $archivefile.gz
    
        # Add the file to the archive
        tar --append --file=$archivefile $oldfile
        
        # Recompress the archive
        gzip $archivefile
    
    # No existing archive - create a new one and add the file
    else
        echo "Creating new archive file '$archivefile'..."
        tar --create --file=$archivefile $oldfile
        gzip $archivefile
    fi
    
    # Update the files outside the archive
    mv $newfile $oldfile
    

    Save it as script.sh, then make it executable:

    chmod +x script.sh
    

    Then run like so:

    ./script.sh oldfile newfile
    

    something like frozenskys.September.2009.tar.gz, will be created, and newfile will replace oldfile. You can also call this script with exec from another script if you want. Just put this line in your second script:

    exec ./script.sh $1 $2