Need some help with an archive script


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Need some help with an archive script
# 1  
Old 04-25-2010
Need some help with an archive script

I'm not sure how to solve the $month-1 thingy or the foreach sentence, so I need some help.
Code:
/*

    folders =
        folder1
        folder2
        folder3
        folder4
        folder5

*/

month=`date +%m`
    
if($month == 01) 
{
    prev_month = 12
} else
{
    prev_month = $month-1 // if month == 04, prev_month should return 03
}

foreach($folders as $f)
{
    cp /home/misc/images/($f)/img_($month)*.jpg /home/misc/images/archive/($f)/
    cp /home/misc/images/($f)/img_($prev_month)*.jpg /home/misc/images/archive/($f)/
    // delete all files from /home/misc/images/archive/($f)/ that isn't like `img_($month)*.jpg` or `img_($prev_month)*.jpg`
}

# 2  
Old 04-25-2010
Code:
    prev_month = $month-1 // if month == 04, prev_month should return 03

should be

Code:
prev_month=`date -d "-1 month" +%m`

# 3  
Old 04-25-2010
@itkamaraj
Your solution won't work for me:
Code:
$ month=`date +%m`
$ echo $month
04
$ prev_month=`date -d "-1 month" +%m`
usage: date [-jnu] [-d dst] [-r seconds] [-t west] [-v[+|-]val[ymwdHMS]] ... 
            [-f fmt date | [[[[[cc]yy]mm]dd]HH]MM[.ss]] [+format]
$

@JKMlol
Here is my solution, but please(!) don't run it immediately against your live data.
First make a backup of all the directories and files that will be affected (/home/misc/images),
so you can restore them, if something goes wrong.
Code:
#!/bin/sh

month=`date +%m`

if [ $month -eq 01 ] ; then

    prev_month=12

elif [ $month -eq 12 ] ; then

    prev_month=11

elif [ $month -eq 11 ] ; then

    prev_month=10

elif [ $month -eq 10 ] ; then

    prev_month=9

else

   prev_month=`echo -n 0 && echo $month-1 | bc`

fi

folders="folder1 folder2 folder3 folder4 folder5"

for f in `echo $folders`
do
# comment the next line out when you know exactly what it does
# rm -v /home/misc/images/archive/$f/* 
cp -v /home/misc/images/$f/img_$month*.jpg /home/misc/images/archive/$f/
cp -v /home/misc/images/$f/img_$prev_month*.jpg /home/misc/images/archive/$f/
done

# 4  
Old 04-25-2010
Quote:
Originally Posted by JKMlol
I'm not sure how to solve the $month-1 thingy or the foreach sentence, so I need some help.
Code:
/*

    folders =
        folder1
        folder2
        folder3
        folder4
        folder5

*/

month=`date +%m`
    
if($month == 01) 
{
    prev_month = 12
} else
{
    prev_month = $month-1 // if month == 04, prev_month should return 03
}

foreach($folders as $f)
{
    cp /home/misc/images/($f)/img_($month)*.jpg /home/misc/images/archive/($f)/
    cp /home/misc/images/($f)/img_($prev_month)*.jpg /home/misc/images/archive/($f)/
    // delete all files from /home/misc/images/archive/($f)/ that isn't like `img_($month)*.jpg` or `img_($prev_month)*.jpg`
}

What language is this, actually?
# 5  
Old 04-26-2010
Code:
kamaraj@kamaraj-laptop:~$ date +%m
04
kamaraj@kamaraj-laptop:~$ date -d "-1 month" +%m
03
kamaraj@kamaraj-laptop:~$

# 6  
Old 04-26-2010
@itkamaraj, Please don't assume that every one is using Linux/or whatever OS you're on.
# 7  
Old 04-26-2010
Quote:
Originally Posted by pseudocoder
*snip*
Thank you so much, but..
prev_month returned 0 and not 03. :/
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Script to archive logs and sftp to another archive server

Requirement: Under fuse application we have placeholders called containers; Every container has their logs under: <container1>/data/log/fuse.log <container1>/data/log/fuse.log.1 <container1>/data/log/fuse.log.XX <container2>/data/log/fuse.log... (6 Replies)
Discussion started by: Arjun Goswami
6 Replies

2. Shell Programming and Scripting

Need help in FTP script to archive

Hi Guys, I am unable to archive the FTP files from FTP source path to FTP archive path.Currently i am running this script from the Local server. Can someone help me here. Code: #!/bin/ksh set -x HOST='xxxx' USER='xxxx' PASSWD='xxxx' PORTNO='9999' FILE_NAME=$1 FTP_SRC_PATH=$2... (2 Replies)
Discussion started by: spidy
2 Replies

3. Shell Programming and Scripting

Image Archive Script

Hello, I am working through a test Linux script from a website, and am running into some issues. The point of the script is to find image files throughout the system, copy them to one centralized directory,ignore duplicate images with the same name, and output a text file of all the images copied.... (3 Replies)
Discussion started by: ComSciguy17
3 Replies

4. Shell Programming and Scripting

Shell script to call Oracle archive backup script when file system reaches threshold value

Hello All, I need immediate help in creating shell script to call archivebkup.ksh script when archive file system capacity reaches threshold value or 60% Need to identify the unique file system that reaches threshold value. ex: capacity ... (4 Replies)
Discussion started by: sasikanthdba
4 Replies

5. Windows & DOS: Issues & Discussions

Archive Unix script to Batch script

Hi All, I have a requirement to archive processed file and retain the archive based on the quantity.. but this is in windows. I have written a drafted shell script, and would like to have it translated into Batch script.. Please... Below is the drafted shell script ... (2 Replies)
Discussion started by: Dave Null
2 Replies

6. Shell Programming and Scripting

need help archive script

Hi all, I am new to linux and scripting so please forgive me. I need to create a script that will archive files but the max size of the archive need to be 500mb or under. I know about the archiving with parts but i want all the archives as individual archives. Can anyone point me in the correct... (7 Replies)
Discussion started by: craig0
7 Replies

7. Shell Programming and Scripting

script help .. archive

Hi All We have a landing directory where source system puts files.There are variable number of files and the file names are also varying.Each files successful transmission is identified by a .done file.If file name is xyz.dat then the confirmation file will be xyz.dat.done. I want to... (1 Reply)
Discussion started by: dr46014
1 Replies

8. Shell Programming and Scripting

Archive script

hi guru, can advise how to construct a housekeeping script using perl for the following ? find /var/tmp/logs -name "si*" -type f -exec gzip -f {} \; find /var/tmp/logs -name "*.gz" -type f -exec mv -f {} /var/tmp/log \; I found out those are not working in shell at when put them on... (1 Reply)
Discussion started by: rauphelhunter
1 Replies

9. Shell Programming and Scripting

script to archive and purge

Hi, I am writing a shell script for archive data from a table. The design is as follows. Step 1: Execute the select query and extract the data into a text file. Step 2: The primary key for this table is TRACKING_NUM, TRACKING_NUM_SUFFIX, TIMESTAMP_UPDATED. So These three fields will be read... (1 Reply)
Discussion started by: kmanivan82
1 Replies

10. Shell Programming and Scripting

making a archive script

Hi All: I'm not much of a script writer so I could use your input. Here's the objective... Need a script that will archive (tar) files based on date and then move them into an archive directory. The file names are as follows... S20070101.001 Year month day S20070102.001 As you can see... (1 Reply)
Discussion started by: jimmyc
1 Replies
Login or Register to Ask a Question