LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Server
User Name
Password
Linux - Server This forum is for the discussion of Linux Software used in a server related context.

Notices


Reply
  Search this Thread
Old 05-18-2010, 03:56 AM   #1
BHuckfield
LQ Newbie
 
Registered: May 2010
Location: Gauteng, South Africa
Distribution: SLES 10 and 11 and OpenSuSE 11, Ubuntu 10.04
Posts: 8

Rep: Reputation: 0
Script query - trying to create a folder with an environment variable


Hey guys,

I hope this is on the right forum, if not, please excuse me.

I am busy learning shell scripting, and I managed to get my first few right, but now I am trying to do something that I know how to do in Windows, but not in Linux. Basically, during a file copy, I want to create a new date for each folder on a daily basis to copy backups into.

so, in windows the syntax would be "xcopy %systemroot%\backups\blah.blah \\servername\backups\%date%"

in linux I am running a "cp blah.gz /mount/blah" I want to create a folder with the date based on the current date.

anyone know how to get the same right in shell script? I am new to linux environment variables, so this question may be a tad lame for some, any help would be appreciated however.


thanks in advance
B
 
Old 05-18-2010, 04:03 AM   #2
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,578
Blog Entries: 31

Rep: Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208
Essentially mkdir "$(date)" but you probably want to use some date command options to get a directory name you like the look of, maybe with a format so there are no spaces in the name. The double quotes keep the output of the date command as a single word, in case there are spaces in the date output.

EDIT

If you want the script to know the directory name then something like this
dir="/mount/blah/$(date)"
mkdir "$dir"
cp blah.gz "$dir"

Last edited by catkin; 05-18-2010 at 04:06 AM.
 
Old 05-18-2010, 05:25 AM   #3
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,358

Rep: Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751
You may find these useful
http://rute.2038bug.com/index.html.gz
http://tldp.org/LDP/Bash-Beginners-G...tml/index.html
http://www.tldp.org/LDP/abs/html/
 
Old 05-18-2010, 05:30 AM   #4
jschiwal
LQ Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682
Using tar is another option. It is the native Linux backup tool and will preserve ownership and permissions. I you copy to a Windows share, these would be lost.
 
Old 05-18-2010, 05:43 AM   #5
BHuckfield
LQ Newbie
 
Registered: May 2010
Location: Gauteng, South Africa
Distribution: SLES 10 and 11 and OpenSuSE 11, Ubuntu 10.04
Posts: 8

Original Poster
Rep: Reputation: 0
thank you both. I think I understand it now. it's just the formatting of the output I gotta get right, but I think I should get it.
thanks for your help again.
 
Old 05-18-2010, 05:46 AM   #6
BHuckfield
LQ Newbie
 
Registered: May 2010
Location: Gauteng, South Africa
Distribution: SLES 10 and 11 and OpenSuSE 11, Ubuntu 10.04
Posts: 8

Original Poster
Rep: Reputation: 0
I use tar and gzip to create my source files during the backup. it's after I have created them that I am copying them via a cifs mount point to a Windows box.
the files in the tar and gzip should retain their permissions as long as they are still in the archive, is that right?
 
Old 05-18-2010, 05:52 AM   #7
BHuckfield
LQ Newbie
 
Registered: May 2010
Location: Gauteng, South Africa
Distribution: SLES 10 and 11 and OpenSuSE 11, Ubuntu 10.04
Posts: 8

Original Poster
Rep: Reputation: 0
aaah, OK, got it!!!

export foldername=`date +%Y%m%d`
mkdir /mount/blah/$foldername




whhoooooop!!!

thanks everyone for your help.
 
Old 05-20-2010, 12:25 AM   #8
jschiwal
LQ Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682
Quote:
Originally Posted by BHuckfield View Post
I use tar and gzip to create my source files during the backup. it's after I have created them that I am copying them via a cifs mount point to a Windows box.
the files in the tar and gzip should retain their permissions as long as they are still in the archive, is that right?
Correct. You may need to pay attention to security attributes as well, although this is more likely important if you are backing up all the files on a computer. The `star' program may be what you are looking for in this case.

Also scan through the info manual for tar. It has some examples, such as piping the archived tar stream output to a subshell where it is extracted. This could be used to copy a large directory tree to another drive or if using ssh, to a drive on another machine on the internet.

Also look at section 5.2 for performing incremental dumps.
 
Old 05-20-2010, 03:57 AM   #9
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,578
Blog Entries: 31

Rep: Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208
Quote:
Originally Posted by BHuckfield View Post
export foldername=`date +%Y%m%d`
mkdir /mount/blah/$foldername
That works but there is no need for the export. The export command makes the named variable an environmental variable, meaning it is part of the process environment and thus available to sub-processes.
 
Old 05-21-2010, 12:49 AM   #10
BHuckfield
LQ Newbie
 
Registered: May 2010
Location: Gauteng, South Africa
Distribution: SLES 10 and 11 and OpenSuSE 11, Ubuntu 10.04
Posts: 8

Original Poster
Rep: Reputation: 0
thank you both again. That is valuable information.
 
Old 05-21-2010, 02:42 AM   #11
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,578
Blog Entries: 31

Rep: Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208
Quote:
Originally Posted by BHuckfield View Post
the files in the tar and gzip should retain their permissions as long as they are still in the archive, is that right?
Right but they are by numeric UID and GID not by username and groupname so they will only be restored to the original users and groups if the username/UID are the same in /etc/passwd and the groupname/GID are the same in /etc/group on the system where you restore as they were on the system where you backed up.
 
  


Reply



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
Script to copy specific directory based on variable to folder with that variable name fluxburn Programming 7 01-07-2010 07:59 PM
Create a custom environment variable that stores hostname Zebe Linux - General 5 09-03-2008 04:14 PM
Squirrelmail: Query: CREATE "INBOX.Sent" Reason Given: Cannot create this folder. Chiragrs Linux - Server 2 03-10-2008 11:37 AM
create new environment variable ctlqt12 Linux - Newbie 5 05-10-2006 04:26 AM
Query about script variable phantomdude Linux - Newbie 1 03-18-2004 11:21 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Server

All times are GMT -5. The time now is 07:07 AM.

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