Find, backup and delete.


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Find, backup and delete.
# 1  
Old 03-06-2014
RedHat Find, backup and delete.

Is there any error while executing this script..

Code:
#!/bin/bash

FINDPATH=/home/ftpcdr/cdr/192.168.3.91
BACKPATH=/home/ftpcdr/backup
STATUS=$?

cd $FINDPATH
find -type d -mtime +30  -print > $BACKPATH/list.txt # FIND FILES THAT CREATED BEFORE 30 DAYS.

FIRST=$(ls $BACKPATH/list.txt | grep "[0-9]" | head -n 1)
LAST=$(ls $BACKPATH/list.txt | grep "[0-9]" | tail -n 1)
LISTFILE=/home/ftpcdr/backup/list.txt

if  [ -s "$LISTFILE" ] ;then # IF NO AVAILABLE OUTPUT IN list.txt THEN EXIT THE SCRIPT. OTHERWISE, GET BACKUP FOR THEM.( I ALWAYS GET HERE THE NEXT ECHO MESSAGES). 
echo "No data to backup!" && exit
else
cd $FINDPATH && tar -czf $BACKPATH/$FIRST\_$LAST.tar.gz `cat $BACKPATH/list.txt`
fi

if [[ $STATUS -eq 0 ]];then #IF THE LAST STEP OK, THEN DELETE THE SAME COMPRESSED FILES AS OF THE LAST STEP.
cd $FINDPATH
find -type d -mtime +30 -delete
fi

I don't know if there are any error in my script. Please your advice. Thanks

Last edited by leo_ultra_leo; 03-06-2014 at 05:50 AM.. Reason: Typo
# 2  
Old 03-06-2014
You're grepping for a number in a filename path that doesn't contain any numbers. (given that the file exists, ls $BACKPATH/list.txt is a less efficient way of saying echo $BACKPATH/list.txt)

Perhaps you are meaning to grep the contents of the file?

i.e. This...
Code:
grep "[0-9]" $BACKPATH/list.txt

Instead of this...
Code:
ls $BACKPATH/list.txt | grep "[0-9]"

# 3  
Old 03-06-2014
I forgot to mention that $FINDPATH contains a many directories named by dates (e.g. 20130809).
# 4  
Old 03-06-2014
But you're grepping on BACKPATH, not on FINDPATH - and on the name of the file, not its contents. And, BTW, if you ls a fully-qualified filename, the output will be a fully qualified filename.
# 5  
Old 03-06-2014
I'm grepping (because I want to give the TAR file a name like FIRSTDAT_LASTFATE.tar) list.txt wich stored in $BACKPATH for the directories that compressed)for the directories that stored. However, if I'm wrong, how can I fix this?
# 6  
Old 03-20-2014
Hey guys, I have fixed and tested it like this..

Code:
#!/bin/bash

# Environment Variables
FINDPATH=/home/ftpcdr/cdr/192.168.3.91
BACKPATH=/home/ftpcdr/backup

# Change into CDRs directory, find the CDRs which created before 30 days and create a list for them in list.txt

cd $FINDPATH && find -type d -mtime +30 | grep -o '[0-9]*' | sort > $BACKPATH/list.txt

# Environment Variables

FIRST=$(cat $BACKPATH/list.txt | grep "[0-9]" |  head -n 1)
LAST=$(cat $BACKPATH/list.txt | grep "[0-9]" | tail -n 1)
LISTFILE=/home/ftpcdr/backup/list.txt

# If we dont have a valid CDRs within this time, then exit the script Else, backup (and compress)
# these files into our backup directory and name it as of the period of CDR creation.

if  [ ! -s "$LISTFILE" ]
then
echo "No data to backup!" && exit
else
cd $FINDPATH && tar -czf $BACKPATH/$FIRST\_$LAST.tar.gz `cat $BACKPATH/list.txt`
fi

#If this operation completed successfully, then delete the original CDRs. Script job has done!

if [ "$?" = "0" ]; then
cd $FINDPATH && find -type d -mtime +30 -delete && echo "CDRs have been successfully archived from $FIRST to $LAST on $BACKPATH"
else
echo "CDRs BACKUP ERROR!"
fi

#End of script, EOT

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Advanced & Expert Users

AIX find duplicate backup files

I would like find and delete old backup files in aix. How would I go about doing this? For example: server1_1-20-2020 server1_1-21-2020 server1_1-22-2020 server1_1-23-2020 server2_1-20-2020 server2_1-21-2020 server2_1-22-2020 server2_1-23-2020 How would I go about finding and... (3 Replies)
Discussion started by: cokedude
3 Replies

2. Red Hat

RHEL 7: Backup Space and Delete is not working in console

Hi All, During my virtual machine power on i have rc3.d script to accept user inputs like IP address. This script gets executed during first time boot up. It was working fine till my VM is using RHEL6.5. Now we migrated to RHEL 7 environment. While accepting the user inputs in console, I... (4 Replies)
Discussion started by: kalpeer
4 Replies

3. Shell Programming and Scripting

Look for, backup and delete symlinks

Hi, My first post here: Was looking if someone can help enhancing my code. I am limited to sh or ash shell (android / busybox) I made a script to look for busybox symlinks, backup them and delete them I have these questions about the below code: - busybox tar do not has the options... (2 Replies)
Discussion started by: Phil3759
2 Replies

4. Shell Programming and Scripting

Script - Mysql backup and delete

Hello I have a production mysql server and archive server, unfortunitly its not possible to setup repliacation between the two, the reason is that the archive server is using some fancy storage engine which doesn't allow mysql replication. I'm trying to write a script using perl that does... (1 Reply)
Discussion started by: amlife
1 Replies

5. Shell Programming and Scripting

rsync backup mode(--backup) Are there any options to remove backup folders on successful deployment?

Hi Everyone, we are running rsync with --backup mode, Are there any rsync options to remove backup folders on successful deployment? Thanks in adv. (0 Replies)
Discussion started by: MVEERA
0 Replies

6. Shell Programming and Scripting

scripts to Initialise,backup,restore,delete and empty

you are to write scripts for a customer that would require a system enable the backing up of files and directories. The script should (these names should be used): 1. INITIALISE: Initialise the directory for the backup(called backup and should be in the home directory) and any other... (1 Reply)
Discussion started by: babby01
1 Replies

7. Shell Programming and Scripting

Find all .htaccess files and make a backup copy in respective directories

Hey guys, I need to know how to locate all .htaccess files on the server and make a backup of them in the folder they reside before I run a script to modify all of them. So basically taking dir1/.htaccess and copying it as dir1/.htaccess_bk dir2/.htaccess copying as dir2/.htaccess_bk... (5 Replies)
Discussion started by: boxx
5 Replies

8. Shell Programming and Scripting

Find and delete everything else...

Ok, new user, relatively new to scripting and need to move to the next stage with awk or sed or both. I have been using them both at a basic level in my script. I want to find a line, no problem, but then I want to delete everything on that line except for the word I am looking for AND the... (4 Replies)
Discussion started by: derekphl
4 Replies

9. Shell Programming and Scripting

Mp3 backup using find, mkisofs

Hi. I want to create a Linux script to find all local mp3 files, create an ISO image. Later I will attempt to use growisofs Something like this: find ~ -name "*.mp3" | xargs mkisofs -o musicbackup tried variations on this but no luck. (4 Replies)
Discussion started by: lancest
4 Replies

10. Shell Programming and Scripting

Find directory and create backup

What I'm attempting to do is create a script that will do a search for directories that meet the following criteria: find . -name "config" -type d this comes back with: ./dir1/anotherDir/test_dir/config ./dir1/anotherDir/test_dira/config ./dir2/test/test_dir/config The results could... (4 Replies)
Discussion started by: cbo0485
4 Replies
Login or Register to Ask a Question