- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - HP-UX
- >
- Re: Script required to copy and delete files from ...
Categories
Company
Local Language
Forums
Discussions
Forums
- Data Protection and Retention
- Entry Storage Systems
- Legacy
- Midrange and Enterprise Storage
- Storage Networking
- HPE Nimble Storage
Discussions
Forums
Discussions
Discussions
Discussions
Forums
Discussions
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
- BladeSystem Infrastructure and Application Solutions
- Appliance Servers
- Alpha Servers
- BackOffice Products
- Internet Products
- HPE 9000 and HPE e3000 Servers
- Networking
- Netservers
- Secure OS Software for Linux
- Server Management (Insight Manager 7)
- Windows Server 2003
- Operating System - Tru64 Unix
- ProLiant Deployment and Provisioning
- Linux-Based Community / Regional
- Microsoft System Center Integration
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Community
Resources
Forums
Blogs
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-30-2008 12:55 AM
12-30-2008 12:55 AM
Script required to copy and delete files from source DIR
I have written a script to ensure all my ARCHIVE files are backed up retaining the directory structure.
The Script is--
#!/bin/sh
# Script to take backup of DCS Folders
DCSITIME=$(perl -e '($ss, $mm, $hh, $DD, $MM, $YY) = localtime(time());printf "%04d%02d%02d%02d%02d", $YY + 1900, $MM + 1, $DD, $hh, $mm')
mkdir /backup/destination/backup_files/OTHERS/$DCSITIME
for i in `ls -d /home/ftp/source/APPL/HELLO/*/ARCHIVE|cut -f1,2,3,4,5,6,7 -d /`; do cp -pr $i /backup/destination/backup_files/OTHERS/$DCSITIME/; done
But my requirement is…. I need to take the backup of these files and delete only copied files from original location (Inside ARCHIVE directory there wont be any Sub directories, it will have only files). I cannot delete using normal delete because these files are very dynamic and if I use only `rm ARCHIVE/*`, I have the risk of deleting files which got created between the time of my backup and deletion. I cannot simply move the directory and recreate it because; application may fail at that point of time.
I need help to take the backup with folder structure intact (I’m able to do in the above script) and delete only copied files inside ARCHIVE Folder.
I had gone through
http://forums13.itrc.hp.com/service/forums/questionanswer.do?admit=109447627+1230622095940+28353475&threadId=1114470
But I think that will not help me as files in these DIR are very dynamic and I have chance of loosing one or two files.
How can I do this??
Regards,
Nagu
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-30-2008 01:39 AM
12-30-2008 01:39 AM
Re: Script required to copy and delete files from source DIR
Any reason you don't change your loop to:
for ...; do
cp -pr $i /backup/destination/backup_files/OTHERS/$DCSITIME/
rm $i
done
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-30-2008 01:50 AM
12-30-2008 01:50 AM
Re: Script required to copy and delete files from source DIR
Here $i is a DIR not file. I dont want DIR to be deleted and recreated.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-30-2008 02:04 AM
12-30-2008 02:04 AM
Re: Script required to copy and delete files from source DIR
This should work.
By the way, you do not need Perl just to
create time-stamped directory (see below).
I added some extra tests because I wwould
hate to delete files if the copy failed :).
The script assumes (as you said) that
there are no subdirectories...
#!/bin/sh
# Script to take backup of DCS Folders
PATH=/usr/bin:/usr/sbin:/sbin:/bin; export PATH
#DCSITIME=$(perl -e '($ss, $mm, $hh, $DD, $MM, $YY) = localtime(time());printf "%04d%02d%02d%02d%02d", $YY + 1900, $MM + 1, $DD, $hh, $mm')
#
# Simpler method, no need for Perl
#
DCSITIME=`date '+%Y%m%d%H%M'`
FROMDIR="/home/ftp/source/APPL/HELLO/*/ARCHIVE"
TODIR="/backup/destination/backup_files/OTHERS/$DCSITIME"
if [ ! -d "$TODIR" ]
then
mkdir -p $TODIR
if [ $? -ne 0 ]
then
echo "ERROR: Cannot create directory $TODIR"
exit 1
fi
fi
for i in `find $FROMDIR -type f`
do
cp -p $i $TODIR
if [ $? -eq 0 ]
then
rm -f $i
else
echo "WARN: Failed to copy $i to $TODIR"
fi
done
exit 0
Cheers,
VK2COT
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-30-2008 02:22 AM
12-30-2008 02:22 AM
Re: Script required to copy and delete files from source DIR
Assuming it is dynamic and only files are created and this script is the only one to remove them:
for ...; do
touch ref_file
cp -pr $i /backup/destination/backup_files/OTHERS/$DCSITIME/
find $i -type f ! -newer ref_file -exec rm -f {} +
done
rm -f ref_file
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-30-2008 09:28 PM
12-30-2008 09:28 PM
Re: Script required to copy and delete files from source DIR
With your script, I will not be able to retain DIR structure in target (backup) folder. It just dumps all files in DCSITIME DIR.
But I need to retain my DIR structure in destination
/backup/destination/backup_files/OTHERS/$DCSITIME/*/ARCHIVE/
Can you please suggest?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-30-2008 10:43 PM
12-30-2008 10:43 PM
Re: Script required to copy and delete files from source DIR
This a bit different request but easy to
resolve anyway:
#!/bin/sh
# Script to take backup of DCS Folders
PATH=/usr/bin:/usr/sbin:/sbin:/bin; export PATH
#DCSITIME=$(perl -e '($ss, $mm, $hh, $DD, $MM, $YY) = localtime(time());printf "%04d%02d%02d%02d%02d", $YY + 1900, $MM + 1, $DD, $hh, $mm')
#
# Simpler method, no need for Perl
#
DCSITIME=`date '+%Y%m%d%H%M'`
FROMDIR="/home/ftp/source/APPL/HELLO/*/ARCHIVE"
TODIR="/backup/destination/backup_files/OTHERS/$DCSITIME"
for i in `find $FROMDIR -type f`
do
DIRNAME=`dirname $i`
NEWTODIR="${TODIR}${DIRNAME}/ARCHIVE"
if [ ! -d "$NEWTODIR" ]
then
mkdir -p $NEWTODIR
if [ $? -ne 0 ]
then
echo "ERROR: Cannot create directory $NEWTODIR"
else
cp -p $i $NEWTODIR
if [ $? -eq 0 ]
then
rm -f $i
else
echo "WARN: Failed to copy $i to $NEWTODIR"
fi
fi
fi
done
exit 0
Cheers,
VK2COT
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-30-2008 10:58 PM
12-30-2008 10:58 PM
Re: Script required to copy and delete files from source DIR
I forgot to add check if the destination directory already exists. Here is the version
with subroutine:
#!/bin/sh
# Script to take backup of DCS Folders
PATH=/usr/bin:/usr/sbin:/sbin:/bin; export PATH
DCSITIME=`date '+%Y%m%d%H%M'`
FROMDIR="/home/ftp/source/APPL/HELLO/*/ARCHIVE"
TODIR="/backup/destination/backup_files/OTHERS/$DCSITIME"
runit () {
cp -p $i $NEWTODIR
if [ $? -eq 0 ]
then
rm -f $i
else
echo "WARN: Failed to copy $i to $NEWTODIR"
fi
}
for i in `find $FROMDIR -type f`
do
DIRNAME=`dirname $i`
NEWTODIR="${TODIR}${DIRNAME}/ARCHIVE"
if [ ! -d "$NEWTODIR" ]
then
mkdir -p $NEWTODIR
if [ $? -ne 0 ]
then
echo "ERROR: Cannot create directory $NEWTODIR"
else
runit $i
fi
else
runit $i
fi
done
exit 0
I am not at work this week, so I could test it on my Fedora 10 server only. It should
be fine on HP-UX too.
VK2COT
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-07-2009 11:39 PM
01-07-2009 11:39 PM
Re: Script required to copy and delete files from source DIR
I could achieve it simply by using cpio command. Thanks for all the help
It can also be achieved by tar as well.
DCSITIME=`date '+%d'`
# Remove old Backup as we need backup for only 30days
rm -rf /backup/test/files/OTHERS/Backup_$DCSITIME
sleep 30
mkdir /backup/test/files/OTHERS/Backup_$DCSITIME
cd /source/Application/
for i in `find */ARCHIVE -type f `;do ls $i|cpio -pvadm /backup/test/files/OTHERS/Backup_$DCSITIME && rm -f $i;done
Regards,
Nagu