nixCraft Linux Forum

nixCraft

Linux / UNIX Tech Support Forum

copy file with standard name by inserting file-date and time in name

This is a discussion on copy file with standard name by inserting file-date and time in name within the File Servers forums, part of the Mastering Servers category; Hi, I run some server-based webcams, they place non-descriptive files in a folder each x seconds. "camshot.jpg", "camshot1.jpg" and "camshot2.jpg" ...


Go Back   nixCraft Linux Forum > Mastering Servers > File Servers

Linux answers from nixCraft.


File Servers Discussion about Samba, NFS and other UNIX / Linux file servers.

Reply

 

LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 06-07-2009, 08:10 PM
Junior Member
User
 
Join Date: Feb 2007
Posts: 26
Thanks: 1
Thanked 0 Times in 0 Posts
Rep Power: 0
meowing
Lightbulb copy file with standard name by inserting file-date and time in name

Hi,

I run some server-based webcams, they place non-descriptive files in a folder each x seconds. "camshot.jpg", "camshot1.jpg" and "camshot2.jpg" (for example). These files get overwritten each time the webcams put them there. (Since this is hardcoded in the webcams, I can't change that..)

Now I would like to be able to archive those files with a cron command. To achieve that I need to copy a file the webcam drops (in its dedicated folder) to a different folder, and then insert a date-time in the name, like so:

/www/netcam/camshot.jpg
/www/netcam/camshot1.jpg

will be copied as/to (for example):

/www/archive/20090615_22h50m24s_camshot.jpg
/www/archive/20090615_22h52m12s_camshot1.jpg

Inserted time and date should be obtained from the file-creation stamp (last modified time).
Anyone have a script I can use for this? (Can be perl, bash or whatever runs on a debain-based server.)

There's a Windows script I found for a similar task here, but don't know how to best use it in a linux-world..


Thanks for any and all hints,

M

Last edited by meowing; 06-07-2009 at 08:13 PM.
Reply With Quote
  #2 (permalink)  
Old 06-07-2009, 10:23 PM
jaysunn's Avatar
Powered By Linux
User
 
Join Date: Apr 2009
Location: 41.332032,-73.089775
OS: RHEL - OSX
Scripting language: BASH - Learning Ruby
Posts: 604
Thanks: 61
Thanked 80 Times in 72 Posts
Rep Power: 10
jaysunn is a splendid one to behold jaysunn is a splendid one to behold jaysunn is a splendid one to behold jaysunn is a splendid one to behold jaysunn is a splendid one to behold jaysunn is a splendid one to behold jaysunn is a splendid one to behold jaysunn is a splendid one to behold
Default Hello

Give this a try. You can modify the variables as needed. Make sure you change the admin email to yours. I have this running nightly to backup data on one of my RHEL servers. Should be portable to Debian. I have used your data to modify the script. However this will backup the directory of individual files. You can modify it if you need each file tarred.

Create the script.sh and add the perl code below.

Code:
#vi script.sh
Make the script executable.

Code:
chmod +x script.sh
Create a cron job for the server to execute the script nightly or whenever.

Code:
crontab -e 

0 0 * * * /path_to_script/script.sh


Code:
#!/usr/bin/perl

#trim subroutine added
sub trim {
    my @out = @_;
    for (@out) {
        s/^\s+//;
        s/\s+$//;
    }
    return wantarray ? @out : $out[0];
}



# Define some global variables. 
$backupDirectory ="/www/archive/";

#folder or files to backup
$folderToBackup = "/www/netcam/";

# Create the temporary directory to keep backups
`mkdir -p /tmp/backups/`;


#get date string 
$dateString = `date +%Y%m%d`;
chop($dateString);

$hostName = `/bin/hostname`;
$hostName = trim($hostName);

#form backup file name 
$backupFileName = $hostName . "_netcam_backup_" . $dateString . ".tar.gz";


# Tar/gzip data 
`tar czf /tmp/backups/$backupFileName $folderToBackup 1>/dev/null 2>/dev/null`;

#move data to backup folder
`cp -f /tmp/backups/$backupFileName $backupDirectory/`;


#check if file was created if not email 
if(!(-e "$backupDirectory/$backupFileName"))
{
   `echo $hostName www Backup Failed | mail admin\@domainname.com` ;
}



#detele all tmp files and directory
`rm -f /tmp/backups/$backupFileName`;
`rm -rf /tmp/backups/`;
__________________
Have a look at what I have been working on
http://www.shellasaurus.com

Last edited by jaysunn; 06-07-2009 at 10:35 PM.
Reply With Quote
  #3 (permalink)  
Old 07-07-2009, 05:21 AM
Junior Member
User
 
Join Date: Feb 2007
Posts: 26
Thanks: 1
Thanked 0 Times in 0 Posts
Rep Power: 0
meowing
Default

Hmm.. this doesn't work for what I want. It tars folders and ads a hostname. I only need to copy a file and have part of the name of the file changed.

Basically, all I need is:

camshot.jpg copied to some other folder as 20090713_22h50m24s_camshot.jpg

where that date-time string is crucial and should look exactly like this;
YYYYMMDD_HHhMMmSSs_originalfilename.jpg

What part of your script causes the date+time insertion? Because I see $folderToBackup return only once, and that's in the tar.gz part.

Last edited by meowing; 07-07-2009 at 05:23 AM.
Reply With Quote
  #4 (permalink)  
Old 08-07-2009, 06:55 PM
jaysunn's Avatar
Powered By Linux
User
 
Join Date: Apr 2009
Location: 41.332032,-73.089775
OS: RHEL - OSX
Scripting language: BASH - Learning Ruby
Posts: 604
Thanks: 61
Thanked 80 Times in 72 Posts
Rep Power: 10
jaysunn is a splendid one to behold jaysunn is a splendid one to behold jaysunn is a splendid one to behold jaysunn is a splendid one to behold jaysunn is a splendid one to behold jaysunn is a splendid one to behold jaysunn is a splendid one to behold jaysunn is a splendid one to behold
Default

The portion of the script that inserts the date is:

Code:
#get date string 
$dateString = `date +%Y%m%d`;
chop($dateString);

Change that to:

Code:
#get date string 
$dateString = `date +%Y%m%d_%Hh%Mm%Ss_`;
chop($dateString);
From my test that outputs:

Code:
[root@server ~]# date +%Y%m%d_%Hh%Mm%Ss_camshot.jpg
20090708_08h46m28s_camshot.jpg
[root@server ~]#
Sorry that this is not exactly what you are looking for. I think you basically just want to copy the files in the current location to the archive location and append its original date from the original copy to the file in the correct date format.

I will try to write something that does that. I am still learning scripting. I may be over thinking this as well.

Jaysunn


Jaysunn
__________________
Have a look at what I have been working on
http://www.shellasaurus.com
Reply With Quote
The Following User Says Thank You to jaysunn For This Useful Post:
meowing (08-07-2009)
  #5 (permalink)  
Old 08-07-2009, 07:05 PM
Junior Member
User
 
Join Date: Feb 2007
Posts: 26
Thanks: 1
Thanked 0 Times in 0 Posts
Rep Power: 0
meowing
Default

Quote:
Originally Posted by jaysunn View Post
Code:
[root@server ~]# date +%Y%m%d_%Hh%Mm%Ss_camshot.jpg
20090708_08h46m28s_camshot.jpg
[root@server ~]#
This uses the current date, and does not insert it in the filename, but we're getting there..

Thanks for the effort anyway

Last edited by meowing; 08-07-2009 at 08:25 PM.
Reply With Quote
  #6 (permalink)  
Old 20-07-2009, 07:04 PM
Junior Member
User
 
Join Date: Feb 2007
Posts: 26
Thanks: 1
Thanked 0 Times in 0 Posts
Rep Power: 0
meowing
Default

bump
If anyone has something useful for this, that'd be great..
Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads

Thread Thread Starter Forum Replies Last Post
Shell date command for manipulation and formatting date newbie4 Shell scripting 4 28-10-2008 07:33 PM
Find Unix Linux File / Directory by date And Then Copy / Move File asim.mcp CentOS / RHEL / Fedora 1 10-08-2008 03:30 AM
shell script to search specific file from txt file inside zip file and extract it aasif.shaikh Shell scripting 2 31-05-2008 06:44 PM
How can I redirect the output of time command to file warren Linux software 13 25-08-2006 11:17 AM
How can I rename a file by its date? warren Linux software 5 30-03-2006 12:11 PM


All times are GMT +5.5. The time now is 03:59 AM.


Powered by vBulletin® Version 3.8.5 - Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.2
©2005-2010 nixCraft. All rights reserved

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38