Backup script in bash
I have a backup script that I wrote in bash that needs a little tweaking. It currently will not backup hidden files because it is simply a recursive script that will call itself with a * on subdirectories. I'm going to add a switch to have it include hidden files such that when it recursively calls itself, it will do so twice - once for "*" and once for ".*". I just wanted to be sure that this will cover all files in a directory without exception. Is there any other way a file could be exluded from * apart from the name beginning with a period? Is there any way to have * include all files within a directory with "." and ".." being the only exceptions?
|
Maybe. Is the script too long to post here?
|
It's fairly long. I wouldn't mind hosting on my web server and linking it for download, though. Basically, the syntax is like this:
backup <options> <files_to_back_up> <location_of_backup> If you use the -R option (for recursion) it will call itself recursively for each subdirectory and replace the <files_to_back_up> with <path>/<subdirectory>/*. I was going to have it run twice, once for * and once for .*. However, if there is a better way to do it in a single command, that would be nice. |
Just use .*
. and .. are not included in .*, at least not in a proper shell like zsh, I don't know about bish or whatever it's called. |
Just tried .* in bash. Looks like it does include . and ..
But, as I said, zsh does not. Thus, using zsh would be the easiest solution. |
tried *.* perhaps? and maybe adding an if statement to check where it is just . or ..
Hope this helps |
If you use "/bin/ls --almost-all", you will get a listing of regular and hidden files, but not the "." or ".." entries. You could pipe the list to xargs to perform the backup.
Usually, the ls command is aliased. Try 'alias ls' Code:
> alias ls LS_OPTIONS=-AR Using shell globbing such as "cp .*" as may be in your program, or the "ls -AR *" as in my above example may not be the best way of doing it. The shell expands the wild card before it gets to the cp or tar command. If there are enough files, this will fail due to not enough memory. Another option is to use the find command. The two programs find and xargs are designed to work together. There is a find -print0 option that corresponds to xarg's -0 option). Using the find command would allow you to locate files modified since the last backup. Piping the output of find to xargs, you are able to limit the number of arguments handled at a time. Also, a filter between the two could for example, remove items from an exclude list. Using the tee command would allow you to log the files handled to a file. Good Luck! |
You may also want to look at using rsync. A simple command like:
rsync -avz /src/mydir/ /backup/mydir Will do a complete archival backup with compression. There is also tar and cpio. |
Sorry I forgot to answer your questions.
|
Quote:
BTW, don't use zsh unless you're happy to be bourne shell incompatible. It seems to me that your recursive solution is way over the top - have you thought of using the Right Tool For The Job instead, find(1)? You can even use its prune facility to exclude the stuff you don't want. |
I don't know why I didn't ask before, but why are you specifying all the files in the directory?
Why not just use the directory name itself? Quote:
Besides, zsh is more than (ba)sh compatible enough for most purposes. |
Quote:
Besides, I don't use obsolete shells. How many times to I have to say it, zsh does not match .* BY DESIGN. i.e. the developers are intelligent enough to know that you don't want . and .. with .*, unlike (ba)sh developers apparently. |
ever heard of find tar or cpio ?
|
If you are writing a backup script, you might want to print out the manuals for "find" and "tar". Installing the source will give you access to the documentation source also. After running ./configure, run 'make dvi' or 'make pdf' to generate the manual.
You will probably run into things in writing the script that the manuals cover. |
All times are GMT -5. The time now is 10:02 PM. |