[Solved] How to tar data along with current system date and time.?

Hi all,

Following is my small script:-

#!/bin/ksh
for i in `cat /users/jack/mainfile-dr.txt`
do
sudo cp -r $i /users/jack/DR01/.
done
cd /users/jack/DR01/
sudo tar cvf system1-DR.tar *
scp system1-DR.tar backupserver:/DRFiles/system1
sudo rm -rf  system1-DR.tar

In this script I copy some imp files to one local folder then tar it and send it via scp to backupserver .

my question is how tar these content along with the current time and date of system when it gets compressed(tared).

something like

system1-DR.11Jan2013-08:12:31.tar 

Could you please guide me on this.
I am getting interest in scripting now, I am beginner, I need your advice.

Thanks in advance,
Manali

That is Dangerous Backticks

Use a while loop instead to read the file, here is the modified code to create tar file with time-stamp:

#!/bin/ksh
while read i
do
        sudo cp -r "$i" /users/jack/DR01/.
done < /users/jack/mainfile-dr.txt

cd /users/jack/DR01/
TS=$( date +"%d%b%Y-%H:%M:%S" )
sudo tar cvf system1-DR.${TS}.tar *
scp system1-DR.${TS}.tar backupserver:/DRFiles/system1
sudo rm -rf system1-DR.${TS}.tar

Thanks bipinajith for reply...

So you mean to say below script will also work ? with two addtional line above, I hope it will

#!/bin/ksh

cd /users/jack/DR01/
sudo cp /etc/default/passwd /users/jack/passwd-default

while read i
do
        sudo cp -r "$i" /users/jack/DR01/.
done < /users/jack/mainfile-dr.txt

cd /users/jack/DR01/
TS=$( date +"%d%b%Y-%H:%M:%S" )
sudo tar cvf system1-DR.${TS}.tar *
scp system1-DR.${TS}.tar backupserver:/DRFiles/system1
sudo rm -rf system1-DR.${TS}.tar

Why you say "That is Dangerous Backticks", can you explain in single line, I saw that link but no idea..

Thanks again,

I will check your date code in my script now.

There is a kernel limitation ARG_MAX . If the data fetched inside the back-ticks is not within this limit, your script will fail.

So it is always a good programming practice to avoid using such construct.

Hey bipinath,
Thanks for reply but
I get error. which doing scp

ssh: system1-DR.31Jan2013-21: node name or service name not known

Please advise.
Thanks,

---------- Post updated at 02:07 AM ---------- Previous update was at 02:07 AM ----------

however the tar file got removed by the script.

---------- Post updated at 02:09 AM ---------- Previous update was at 02:07 AM ----------

else I can write it down

scp *.tar backupserver:/DRFiles/system1

---------- Post updated at 02:09 AM ---------- Previous update was at 02:09 AM ----------

tar file showing Date but not Time

The colon : in the time-stamp is causing issue. Create time-stamp without colon :

TS=$( date +"%d%b%Y-%H-%M-%S" )

wait I need to ask something more

---------- Post updated at 02:22 AM ---------- Previous update was at 02:17 AM ----------

wow sir it worked!
but one question
system1-DR.31Jan2013-21-49-05.tar is the file created
and time on local system is:

Thursday, January 31, 2013  9:49:29 PM CET

and on target system is

Thu Jan 31 23:27:42 CET 2013

---------- Post updated at 02:22 AM ---------- Previous update was at 02:22 AM ----------

so what is the time tar file captured ???

---------- Post updated at 02:24 AM ---------- Previous update was at 02:22 AM ----------

I guess it is amsterdam local time..?
is it?

---------- Post updated at 02:25 AM ---------- Previous update was at 02:24 AM ----------

You gave me great things to learn, thanks for till now whatever You have taught me.

Local system time.

Because you captured the time-stamp and created your tar file in your local system.

Yes Sir,
it create tar with city Amsterdam local time.

---------- Post updated at 02:30 AM ---------- Previous update was at 02:28 AM ----------

Thank You very much sir.
I am beginner in scripting,but surely If I get such ideas I will do more practice on this stuff.

---------- Post updated at 02:32 AM ---------- Previous update was at 02:30 AM ----------

one more question:

drwxr-xr-x   2 sysmgr   staff        512 Jan 31 21:17 ./
drwxr-x---   8 sysmgr   staff       1024 Jan 31 21:12 ../
-rw-r--r--   1 root     root         393 Jan 31 21:17 dfstab
-rw-r--r--   1 root     root         401 Jan 31 21:17 group
-r-xr-xr-x   1 root     root         641 Jan 31 21:17 init*
-rw-r--r--   1 root     root        2982 Jan 31 21:17 passwd
-r--r--r--   1 root     root        1614 Jan 31 21:17 passwd-default
-rw-r--r--   1 root     root        1258 Jan 31 21:17 profile
-r--r--r--   1 root     root        5007 Jan 31 21:17 services
-r--------   1 root     root        1328 Jan 31 21:17 shadow
-r--r--r--   1 root     root           0 Jan 31 21:17 sharetab
-r--r--r--   1 root     root         132 Jan 31 21:17 shells
-rw-r--r--   1 root     root        7867 Jan 31 21:17 system
-rw-r--r--   1 root     root        2871 Jan 31 21:17 vfstab

these files will be overwritten next time when I execute same script right ?

Which path are you referring to here? If you are talking about the recursive copy that you are performing, then yes the files will be overwritten.

Wonderful explanation. You solved all my problems.