Script query - trying to create a folder with an environment variable
Linux - ServerThis 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.
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.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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.
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"
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.
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.
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?
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.
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.
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.