LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 06-22-2004, 01:36 AM   #1
onewhoknows
Member
 
Registered: May 2004
Posts: 30

Rep: Reputation: 15
How would I automate this?


I make daily backups for a server that currently names files based on date:

Here's the script I made that's cheap, but works.
Quote:
rm -rf staging/folder/* && mkdir staging/folder/21 && cp folder/aud/*2004-06-21.log staging/folder/21 && cp folder/aud/*06-21.log staging/folder/21 && cp folder/sta/*-062104.log staging/folder/21 && cp folder/*06-21.log staging/folder/21 && bzip2 staging/folder/21/*
What I did was manually change the dates "*06-21.log" would be "*06-22.log" the next day.

This worked fine when I was only making backups for one machine, but now I'm doing this for 10 machines and it's starting to take up a lot of time. What I'd like to know is if there's a way to make the dates change automatically. It's difficult since the naming convention is slightly different each time. Any tips or suggestions are very welcome!


Thank you
 
Old 06-22-2004, 01:58 AM   #2
scheidel21
Senior Member
 
Registered: Feb 2003
Location: CT
Distribution: Debian 6+, CentOS 5+
Posts: 1,323

Rep: Reputation: 100Reputation: 100
I would think if you wrote a command in there that processed the date into a variable you could simply use the variable instead of the date, and the date would come from the computer. ie

computer date ----->backupday

then change 2004-06-21.log to backupday.log

and if the folder changes as well I would think you should be able to trim the date and use the day. honestly I am not familiar with all of the commands in linux, but I am sure this can be done this way or similar, I am afraid due to my lack ok knowledge I can't tell you exactly what commands to use.

Alex
 
Old 06-22-2004, 02:04 AM   #3
onewhoknows
Member
 
Registered: May 2004
Posts: 30

Original Poster
Rep: Reputation: 15
Quote:
Originally posted by scheidel21
I would think if you wrote a command in there that processed the date into a variable you could simply use the variable instead of the date, and the date would come from the computer. ie

computer date ----->backupday

then change 2004-06-21.log to backupday.log

and if the folder changes as well I would think you should be able to trim the date and use the day. honestly I am not familiar with all of the commands in linux, but I am sure this can be done this way or similar, I am afraid due to my lack ok knowledge I can't tell you exactly what commands to use.

Alex
Thanks, Alex.

That's kind of what I was thinking, but I'm not sure how to factor in the different conventions for dates. I have YYYY-MM-DD, MM-DD, MMDDYY and a few different ones for other machines. *sigh*
 
Old 06-22-2004, 02:29 AM   #4
iluvatar
Member
 
Registered: Jul 2003
Location: netherlands
Distribution: debian
Posts: 403

Rep: Reputation: 30
You can insert the date in the filenames like in this example:

Code:
cp test test_$(date +%d-%m)_backup
using a $(...) construction, the output of the command inside the ( and ) is put in the filename you want to copy to. see "date --help" to list all output possibilities, any format like MM-DD-YYYY or DD-MM-YY is possible. My example above will create a file named: test_22-06_backup (it will at least today )

greetingz,
.-=~ iluvatar ~=-.

edit: here's just a list of the output possibilities for the 'date' command:
Code:
  %a   locale's abbreviated weekday name (Sun..Sat)
  %A   locale's full weekday name, variable length (Sunday..Saturday)
  %b   locale's abbreviated month name (Jan..Dec)
  %B   locale's full month name, variable length (January..December)
  %c   locale's date and time (Sat Nov 04 12:02:33 EST 1989)
  %d   day of month (01..31)
  %D   date (mm/dd/yy)
  %e   day of month, blank padded ( 1..31)
  %h   same as %b
  %H   hour (00..23)
  %I   hour (01..12)
  %j   day of year (001..366)
  %k   hour ( 0..23)
  %l   hour ( 1..12)
  %m   month (01..12)
  %M   minute (00..59)
  %n   a newline
  %p   locale's AM or PM
  %r   time, 12-hour (hh:mm:ss [AP]M)
  %s   seconds since 00:00:00, Jan 1, 1970 (a GNU extension)
  %S   second (00..60)
  %t   a horizontal tab
  %T   time, 24-hour (hh:mm:ss)
  %U   week number of year with Sunday as first day of week (00..53)
  %V   week number of year with Monday as first day of week (01..52)
  %w   day of week (0..6);  0 represents Sunday
  %W   week number of year with Monday as first day of week (00..53)
  %x   locale's date representation (mm/dd/yy)
  %X   locale's time representation (%H:%M:%S)
  %y   last two digits of year (00..99)
  %Y   year (1970...)
  %z   RFC-822 style numeric timezone (-0500) (a nonstandard extension)
  %Z   time zone (e.g., EDT), or nothing if no time zone is determinable
use "date +<format>"

Last edited by iluvatar; 06-22-2004 at 02:31 AM.
 
Old 06-22-2004, 02:43 AM   #5
onewhoknows
Member
 
Registered: May 2004
Posts: 30

Original Poster
Rep: Reputation: 15
Oh, awesome! I feel so stupid for not thinking of that

One other question... I might've missed it in --help, but is there a way to designate a day BEFORE? I'd have thought something like %d-1 would work, but I don't see anything for that in --help.

I really appreciate the help
 
Old 06-22-2004, 04:49 AM   #6
iluvatar
Member
 
Registered: Jul 2003
Location: netherlands
Distribution: debian
Posts: 403

Rep: Reputation: 30
hmmm you want the day before... tricky one %d-1 won't work, cause it might affect %m too when going to the previous month. you can write the current date to a file
Code:
date +%d-%m > date.txt
and use this file the next day, like
Code:
cp test test_$(cat date.txt)_backup
. after making the backup, you can write the current date to the file again, wich will then be used next day etc. etc.

greetz,
.-=~ iluvatar ~=-.

ps: you can use cron jobs to automaticly put the date in the file.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Automate Xlock? dauphinfay Linux - Software 2 08-14-2005 03:05 AM
automate installations alaios Slackware 6 05-29-2004 03:26 AM
automate vncserver hoover93 Linux - Software 2 10-23-2003 09:16 AM
Automate PPPD ashleyfrazer Linux - Networking 16 10-28-2002 10:42 AM
Automate FTP noodle123 Linux - General 1 10-15-2002 04:50 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 11:24 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration