| Linux - Server This forum is for the discussion of Linux Software used in a server related context. |
| Notices |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
 |
GNU/Linux Basic Guide
This 255-page guide will provide you with the keys to understand the philosophy of free software, teach you how to use and handle it, and give you the tools required to move easily in the world of GNU/Linux. Many users and administrators will be taking their first steps with this GNU/Linux Basic guide and it will show you how to approach and solve the problems you encounter.
Click Here to receive this Complete Guide absolutely free. |
|
 |
05-18-2010, 03:56 AM
|
#1
|
|
LQ Newbie
Registered: May 2010
Location: Gauteng, South Africa
Distribution: SLES 10 and 11 and OpenSuSE 11, Ubuntu 10.04
Posts: 8
Rep:
|
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
|
|
|
|
05-18-2010, 04:03 AM
|
#2
|
|
LQ 5k Club
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian Squeeze (server), Slackware 13.37 (netbook), Slackware64 14.0 (desktop),
Posts: 8,367
|
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.
|
|
|
|
05-18-2010, 05:25 AM
|
#3
|
|
Guru
Registered: Aug 2004
Location: Brisbane
Distribution: Centos 6.4, Centos 5.9
Posts: 15,261
|
|
|
|
|
05-18-2010, 05:30 AM
|
#4
|
|
Moderator
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733
|
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.
|
|
|
|
05-18-2010, 05:43 AM
|
#5
|
|
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:
|
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.
|
|
|
|
05-18-2010, 05:46 AM
|
#6
|
|
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:
|
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?
|
|
|
|
05-18-2010, 05:52 AM
|
#7
|
|
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:
|
aaah, OK, got it!!!
export foldername=`date +%Y%m%d`
mkdir /mount/blah/$foldername
whhoooooop!!!
thanks everyone for your help.
|
|
|
|
05-20-2010, 12:25 AM
|
#8
|
|
Moderator
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733
|
Quote:
Originally Posted by BHuckfield
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.
|
|
|
|
05-20-2010, 03:57 AM
|
#9
|
|
LQ 5k Club
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian Squeeze (server), Slackware 13.37 (netbook), Slackware64 14.0 (desktop),
Posts: 8,367
|
Quote:
Originally Posted by BHuckfield
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.
|
|
|
|
05-21-2010, 12:49 AM
|
#10
|
|
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:
|
thank you both again. That is valuable information.
|
|
|
|
05-21-2010, 02:42 AM
|
#11
|
|
LQ 5k Club
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian Squeeze (server), Slackware 13.37 (netbook), Slackware64 14.0 (desktop),
Posts: 8,367
|
Quote:
Originally Posted by BHuckfield
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.
|
|
|
|
| Thread Tools |
Search this Thread |
|
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
All times are GMT -5. The time now is 08:12 PM.
|
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|