I've been working on this for the better part of three days and I'm pretty frustrated at this point, so I thought I'd ask for some help.
I'm trying to put together a script that will check for files older than 18 months and move them to a compressed tar file and I need this script to be able to be run on multiple different folders. It's a bit of a daunting task for me as this is the first script that I've been asked to write, so I decided to break it up.
First I came up with the find command
Code:
find /mnt/networkshares/[Sharename]/ -type f -mtime +589 > /mnt/networkshares/[Sharename]/[Sharename]-Archive-Log-`date +"%Y-%m-%d"`.txt
Second I came up with the tar command that would take that list and put them into a compress tar file.
Code:
tar --create --file=[Sharename]-Archive-`date +"%Y-%m-%d"`.tgz --files-from=[Sharename]-Archive-Log-`date +"%Y-%m-%d"`.txt --gzip
Then I tried to do something similar with the rm command to remove the original files, but I've hit a crick wall, the list that was originally created isn't escaped and causes errors when it's being fed into rm because there are spaces in a good portion of the file names. So I came up with:
Code:
find /mnt/networkshares/sales/ -type f -mtime +589 -ls > /mnt/networkshares/sales/Sales-Archive-Log-`date +"%Y-%m-%d"`.txt
This gives me a list of the files with the necessary escapes, but it also gives me a ton of other information that I don't need, so I came up with:
Code:
find /mnt/networkshares/sales/ -type f -mtime +589 -ls | cut -c68- > /mnt/networkshares/sales/Sales-Archive-Log-`date +"%Y-%m-%d"`.txt
but this leaves some spaces in front of some of the lines and cuts it off perfectly on others.
I spoke w/ my manager about this and he was saying that there is a more efficient way of doing this using for and next loops. I've spent the majority of the morning trying to sort that out, but have been having equally as many if not more problems. If someone could help me out I'd greatly appreciate it. This is what I've got so far:
Code:
#!/bin/bash
SHARES= ls /mnt/networkshares/ | grep -v testscript.sh
for s in $SHARES
do
FILES= find /mnt/networkshares/$s/ -type f -mtime +580 > /mnt/$s/$s-Archive-Log-`date +"%Y-%m-%d"`.txt
tar --create --files=$s-Archive-`date +"%Y-%m-%d"`.tgz --files from=$s-Archive-Log-`date +"%Y-%m-%d"`.txt --gzip
done
and I'll continue to update as I progress, but if you've got any questions, comments, suggestions they'd all be greatly appreciated.