Quickly backup files with this bash script

<img class="alignright size-full wp-image-1256" style="margin: 4px; border: 2px solid black;" alt="Bash script" src="http://www best slimming pills.solarum.com/wp-content/uploads/2013/12/script_icon-sm.jpg” width=”150″ height=”103″ />This is something that I use on a regular basis on all of my servers. How many times have you been ready to edit a file and either don’t make a backup copy or make one but by now are real tired of typing out copy one file to another name with a date stamp and blah blah blah. It’s not hard to do, but it gets old quick typing the same thing over and over again, plus you might not always name them the same thing or the same way, so now your backup files have different naming patterns and whatnot.

Don’t worry, I have an easy solution. I created a simple script to backup the file specified and append a time and date stamp to the end of it. I symlink this to the command ‘bu’ in someplace like /usr/bin so it’s always in the path of whatever user I might be (myself, root, backup, whoever?), and then POW, it’s easy to backup files plus they are always named the same way – you just type “bu filename”. Now, if you don’t like the way I name my file copies, feel free to customize this to suit your needs. Also, I currently have the script make the copy right next to the original file, but it would be easy to always copy the files to a backup directory somewhere if you wanted, the possibilities are endless!

OK, on to the script goodness:

if [ "$1" == "" ]; then
  echo "No input given, stopping"
YEAR=`date | awk '{print $6}'`
MONTH=`date | awk '{print $2}'`
DAY=`date | awk '{print $3}'`
TIME=`date | awk '{print $4}' | awk -F: '{print $1"-"$2"-"$3}'`
echo -n "Backing up the file named $1 ... "
/bin/cp -p $1 $1_${YEAR}.${MONTH}.${DAY}_${TIME} > /tmp/bu_run.log 2>&1
echo "done."

There you have it, a simple file backup script it bash that can save you time and many, many keystrokes. Drop me a comment and let me know what you think, or if you have any suggestions or improvements.

Command Line Encryption And Decryption Of Files Made Easy!

Encryption iconHey folks, here’s a fun little tidbit for you. Did you know that you can easily and quickly encrypt and decrypt files using one tiny little command on your super cool Linux or UNIX (Yes, OSX counts) and even Windows command line? For those that haven’t yet heard of it, it’s a command called ‘ccrypt‘. Check it out …

First we need to install ccrypt on on your system. For Debian and Ubuntu (which is based on Debian), you can simply use the apt package manager to do this. Remember that you can use the -s flag to test or simulate the install before you actually go through with it in order to make sure there are no surprises waiting for you. Logged in as your un-privileged account, the command would look like this:

sudo apt-get -s install ccrypt

Assuming everything went off as planned, you could then run the real thing:

sudo apt-get install ccrypt

For Redhat (CentOS, and others based on Redhat), they have RPM packages available for download. Along with those they have Debian, Solaris (SPARC and i386), OS/2, SuSE, OpenBSD, and FreeBSD packages as well as pre-compiled binaries for lots of platforms and OS’s, so go crazy people!!

OK, now that you have the package installed, you can have some fun whiling away the afternoon encrypting and decrypting files like mad!

To encrypt a file, run this command:

ccrypt file_name

It’s just that easy.

Naturally, you would replace ‘file_name’ with your real file information. You will be asked to enter a key or password two times. Once complete, the encrypted file will have an extension of ‘.cpt’, and the original un-encrypted file will be replaced by the encrypted file.

To decrypt the file, run the same command the same way and simply add the -d flag.

ccrypt -d file_name

You will be asked for the encryption key or password that you gave it when you encrypted it in the first place, so don’t lose it! As always you can use the ‘–help’ flag or hit up the man pages for more detailed information. Hope you enjoy it!

**Danger, Will Robinson!**
Cheesy I know, but I hope it’s working. One more time – please note that when you run the command to encrypt a file, the original source file, the un-encrypted file gets replaced by the newly encrypted file. So if you are simply making an encrypted copy for example, the original is gone. If you lose or forget the encryption key or password you will be out of luck. I’m sure it can be cracked by someone, but boy that would be a pain in the arse! So, keep that in mind when you encrypt a file, the file you are encrypting goes bye, bye! It works the same way when un-encrypting, but that’s not as potentially dangerous.

A little history for all us starnix guys (and gals) out there

<a href="http://www.solarum weight reduction pills.com/wp-content/uploads/2012/08/ken-and-den-1024.jpg” target=”_blank”>Ken Thompson (seated) and Dennis RitchieIf you spend any amount of time working with or administering UNIX and/or linux »”>Linux servers, especially unix »”>UNIX, you should be familiar with the text editor ‘vi’ and some commands like ‘sed’ and ‘awk’. If you have been around a while, or had the good(?) fortune of working on some old(er) systems, you might even remember the line editor ‘ed’. I’ll show my age here and recall fond memories of using ‘ed’ to write code many years back.

OK, on to the point, I was looking through Wikipedia for something entirely un-related, but ran across a tidbit of information that I thought was really cool, and that I knew I had to share with Solarum’s readers. It gives a bit of history about some of the tools that we use and love today.

From Wikipedia:

“ed is a line editor for the Unix operating system. It was one of the first end-user programs hosted on the system and has been standard in Unix-based systems ever since. ed was originally written in PDP-11/20 assembler by Ken Thompson in 1971. Ken Thompson was very familiar with an earlier editor known as qed from University of California at Berkeley, Ken Thompson’s alma mater; he reimplemented qed on the CTSS and Multics systems, so it is natural that he carried many features of qed forward into ed. Ken Thompson’s versions of qed were the first to implement regular expressions, an idea that had previously been formalized in a mathematical paper, which Ken Thompson had read. The implementation of regular expressions in ed is considerably less general than the implementation in qed.

ed went on to influence ex, which in turn spawned vi. The non-interactive Unix command grep was inspired by a common special use of qed and later ed, where the command g/re/p means globally search for the regular expression re and print the lines containing it. The Unix stream editor, sed implemented many of the scripting features of qed that were not supported by ed on Unix; sed, in turn, influenced the design of the programming language AWK, which in turn inspired aspects of PERL »”>Perl.”

It’s pretty cool how stuff flows and comes together. Who knew or would have thought that a couple simple commands or programs would turn into what we have today.

*Note: starnix refers to the combination of UNIX, Linux and any other ix/ux OS that we work with.

Learn Solaris UNIX or Linux Today, The Real Way

OK Folks, I added this post so I could tell you about a new article that I just posted.  Just like the title here suggests, I talk about learning Solaris UNIX and/or Linux the real way or maybe it would be better said as the right way.  I don’t suppose there is a wrong or right way, but still.  In this article I reach down into the depth of not only my own knowledge but I lean heavily on my own experience as well, and use that information to share all I know and can rant about learning solid UNIX and Linux skills for the up and coming nix jockies out there.  I really hope that this article can help someone and maybe more than just one naturally.  It’s some (I think) good advice on how to get started and some of the best ways to dig in there and learn some good stuff.  Maybe in the future I’ll post more lower level hard core how to stuff and see how that goes over.  Some of that kind of stuff I have posted already (like SVM disk info and Symantec Storage Foundations (formerly Veritas Volume Manager)) has been real popular.  Anyway, for anyone interested, head on over to the Library and check out the new article on Learning Solaris UNIX and Linux today, you might find something helpful in there!  If you have any thoughts about it or something you think I should add, please drop me a line and let me know.  Thanks!

New Advisory Pages!

Whoah!  We have been busy, busy over here setting up a bunch of new advisory pages.  Just in case you are wondering, we take an RSS feed and set it up to display on a page.  So, you can click on that page link and get the latest information from that RSS feed right there on the page here at Solarum dot com.  We have many of the major feeds that IT folk would be interested in, from Microsoft and Cisco security advisories to Linux and UNIX as well.  We even include feeds from NIST, US Cert, OSVDB and more so we can keep an eye on apps and everything else too.  Not to mention that we add feeds any time we can, and especially when we find good information to share.  Enough talking, why don’t you go check them out, they are in the middle column near the top, all the information you need!  If you know of a feed that we should carry, please let us know so we can add it!!

Bash function for making locate find exact matches

This is one of the coolest and most useful things to add to my UNIX/Linux profile that I have come across in a long time. I use the locate command a lot (slocate naturally) as I am sure all of us command line monkeys do. How many times have you been frustrated by the billions of lines of results flying by your screen, piping through more or less, trying to find the one nugget of goodness that you really need? Especially when you actually know the correct name of it, just not where it lives? This is where this comes in handy (this is where this? man I am eloquent)! Add this function to your bash profile (for some that’s .bash_profile and for others it might be .bashrc, depending on your nix flavor) and you can stop all of that. I haven’t tried this with other shells aside from bash, but I don’t see why it wouldn’t work.

Basically, this function uses the locate command to find whatever you are looking for just like you do, only it uses a bit of scriptology to filter it down to the exact match of what you are looking for. Yep, that’s right, the exact match! This little tidbit can really help out when you are looking for something, take a look:

## BASH locate function for exact match
## Thanks Dark_Helmet : http://solarum.com/v.php?l=1149LV99
function flocate
  if [ $# -gt 1 ] ; then
  while [ ${current_argument} -lt ${total_arguments} ] ; do
    if [ "${display_divider}" = "1" ] ; then
      echo "----------------------------------------"
      echo "Matches for ${current_file}"
      echo "----------------------------------------"
    filename_re="^\(.*/\)*$( echo ${current_file} | sed s%\\.%\\\\.%g )$"
    locate -r "${filename_re}"
    (( current_argument = current_argument + 1 ))

It’s just that easy! Copy and paste this into your profile and add a cool helper addon companion function thingy 🙂 I wish I could say I came up with this myself, but I didn’t, I found it in some forums posted by someone named Dark_Helmet (just like the attribution link in the script). I don’t know who you are Mr. Helmet, but I thank you for your sharing this with us all, and I am passing it on! Enjoy!