Bash script to archive files and then copy new ones
I need help as my scripting skills are slightly less than l337 :(
I need to gzip multiple files and then copy the newer ones from above from somewhere else. I need to be able to call this script in the following way from other scripts.
exec script.sh $oldfile $newfile
Can anyone point me in the right direction?
EDIT: To add more details:
This script will be used for monthly updates of some documents uploaded to the folder, old documents need to be zipped into one compressed file, and new documents, which may have different names, are copied over the old one, the script needs to be called in the document by document from another script. The main thread for this script should be -
- The script file should create a new gzip archive with the specified name (created from the prefix constant in the script and the current month and year, for example prefix.september.2009.tar.gz), only if it doesn't already exist, otherwise append to the existing one.
- Copy the old file to the archive.
- Replace the old file with the new one.
Thanks in advance
Richard
EDIT: Added file details in archive
source to share
Here's a modified script based on your clarifications. I have used archives tar
compressed with gzip
to store multiple files in one archive (you cannot store multiple files with just using gzip
). This code has only been superficially tested - it probably has one or two bugs, and you should add additional code to test the success of the command, etc., if you use it in anger. But that should make you most of the way.
#!/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 follow these steps:
./script.sh oldfile newfile
something like 'frozenskys.September.2009.tar.gz , will be created, and
newfile will replace
oldfile . You can call this script with
exec` from another script if you like. Just put this line in your second script:
exec ./script.sh $1 $2
source to share
Good replication for any bash script Advanced Bash-Scripting Guide .
This tutorial explains each bash script.
The main approach I would take is:
Move the files you want to zip to a directory your create.
(commands mv and mkdir)
zip the directory. (command gzip, I assume)
Copy the new files to the desired location (command cp)
In my experience, bash scripts mostly know how to use this command correctly, and if you can run it on the command line, you can run it in your script.
Another useful command is
pwd - this returns the current directory
source to share