Monthly Archives: January 2011

0

Updating .profile when changing Linux Distributions

by

Ever had one of those Twilight Zone experiences? You know where something works one minute, but doesn’t the next? It’s like the universe just getting suxed into a black hole?

Okay well my experience wasn’t quite that dramatic, but this took over a year to figure out.

Symptoms

If I was locally connected to a machine, .bash_aliases worked fine. (.bash_aliases are user added aliases to the bash shell). But, if I ssh’d (secure shell) into the machine, it was like, huh? What are you talking about?

I fought this for a year.

Solution
So someone on ubuntu (yofel) said: “Hey, look at your .profile!”

So I did. Here’s what I got:

nwayno@Phoenix:~$ ls -l -t -r .bash*

-rwxr-xr-x 1 nwayno nwayno 925 2007-05-20 00:45 .profile.old
-rw-r–r– 1 nwayno nwayno 675 2011-01-26 23:48 .profile

nwayno@Phoenix:~$ cat .profile.old
# Sample .profile for SuSE Linux
# rewritten by Christian Steinruecken
#
# This file is read each time a login shell is started.
# All other interactive shells will only read .bashrc; this is particularly
# important for language settings, see below.

Uh oh! SuSE .profile, on a Ubuntu machine? Who would have thought? So I renamed the .profile

copied it over from another machine (using scp)

logged out/back in again (to re-read .bashrc)

and TADA! .bash_aliases worked when I ssh’d into the box.

So, the lesson here is, if you change Linux distros, you will probably need to change the .profile file as well.

The weird flakiness is gone!

And so it goes….

Wayno

3

Scheduling a backup script with crontab for Ubuntu/Debian

by

In the previous post we saw a simple backup script, that backs up the entire /home/ directory.

While this is great, we would have to remember to run this everyday for it to be of any use.

Fortunately for us, we can do that very easily with crontab. crontab maintains a chronological (clock order) entry of all the tasks that need to be run at a certain time.

So now that we have the backup script, let’s schedule it for daily backup!

1. We want to use the system (root) crontab, so we enter:


sudo crontab -e

the sudo tells use we want temporary super user powers, to run crontab, and we want to edit it.

Enter you password, and you will see a script! Oh my gosh! Hey it’s not that hard. We’re going to add a line to the end of the script.

scroll down to the end of the script, and you will see a line that looks like:

# m h dom mon dow command

that’s sort of a template.

m – is the minute you want the script to run (0 – 59)
h – is the hour (24 hour military time) that you want the script to run. (0 – 23)
dom – is the day of the month (1 – 31)
mon – is the month (1 – 12)
dow – is the day of the week (0 – 6. 0=Sunday)

You can use 3 letter abbreviations for month and day of week, but for now we’ll stick with numbers.

2. so my entry looks like this:


05 01 * * * sh /etc/nwaynobkup/dailyhome.sh

So let’s decode this shall we?

05 – says run this at 5 minutes after the hour.
01 – says run it at 1 am.

so we are running the script at 1:05 am every day. Notice MINUTES are first, then hours.

But what are all the asterisks (*) doing?

The asterisks are a wild card. In this case it says, run any day of the month (dom), in any month, and on every day of the week. TADA! This script will run at 1:05 am EVERYDAY. Easy yes?

Still worried about the command part? Don’t be. Remember that is how we ran it manually in the previous post. We simply copy that here:


sudo sh /etc/nwaynobkup/dailyhome.sh

NOTE: IT IS NOT NECESSARY TO PUT SUDO IN CRONTAB SINCE WE ARE RUNNING THIS AS ROOT.

(or whatever you called your backup script!)

I happened to put my script in /etc/nwaynobkup/. I put it there, since I am not likely to change it. You could run this out of your home directory, just fully qualify it. (/home/nwayno/dailyhome.sh for example)

so the command is simply:


05 01 * * * sh /etc/nwaynobkup/dailyhome.sh

Again, or whatever you called your backup script.

remember to hit ENTER at the end of the line. Save it, and you are done. (for nano, hit control-o to write the file, and then control-x to exit)

3. Now if we go to /var/log/ we’ll see:


cd /var/log
ls -l waynodaily.log

-rw-r–r– 1 root root 90909 2011-01-23 01:09 waynodaily.log

4. Tail the output and we’ll see:


sudo tail waynodaily.log

Or whatever you called your log file. You will see something like this:

2011/01/23 21:57:04 [13326] .d..t…… nwayno/.purple/logs/yahoo/
2011/01/23 21:59:10 [13326] sent 827487594 bytes received 38810 bytes 3330086.
13 bytes/sec
2011/01/23 21:59:10 [13326] total size is 221400641467 speedup is 267.55

How did you do?

2

Simple rsync backup/restore bash scripts for Ubuntu/Debian

by

Two (2) Rules for Successful Computing:

1. Always have a path back to the way it was, before you messed it up!

2. Always follow Rule #1!

This is something I’ve been meaning to post for a long time. How to do simple command line backup and restore operations, using a very simple bash script.

As always anything with a # in a command is simply a comment and need NOT be coded. So here’s the simple backup script, you can just copy, paste and save it (mine is called dailyhome.sh, yours might be different), with your favourite editior: gedit, nano, vi. Whatever makes your boat float!

This post will seem excessively long, but I am trying to make this simple enough that a n00b could do this. Apologies in advance.

So, using the editor of your choice (gedit, nano, vi), create a file. Copy/Paste, save the following to something like dailyhome.sh


#!/bin/bash
#
#
# BACKUP the /home/ directory
# * this script is now executable!
# * fully qualified path for rsync
# * now invoked with bash since the ubuntu 'sh' is anemic
# * used end of line continuation character for greater readability
# * js
#
/usr/bin/rsync -azvu --log-file=/var/log/waynodaily.log --append
--exclude '.gvfs'
/home/ /media/bfdlinux/Homer/homerbkup/

well let’s see what we have here.

1. the options -azvu says:

a – archive mode. Files are compared against the backup,and only those files that have been added, changed, deleted, moved, since the last backup, are sent. This is an “incremental” backup. If this is the first time you are running this script, ALL files are backed up.

z – compress files during transfer
v – verbose mode (gives us a lot of debug info
u – update. skip files that are newer in the destination.

2.
–logfile points us to where we want to the logfile to appear. in this case we want the output to go to /var/log/ which is the same place we would find the system messages file: /var/log/messages (you can use dmesg to look at the last few entries in the system log.

the log will contain a list of all the files archived, debug messages, and other things like:

sent 4598381 bytes received 23273 bytes 70559.60 bytes/sec
total size is 234383146344 speedup is 50714.13

3.
–append simply says, hey append the output of rsync to the current file. Someday we’ll talk about logrotate but I just want to backup things.

4. By now you are wondering what the is all about. You will notice there is nothing after the backslash. This simply tells bash that more information is on the following line. We do this so we can read it! Remember there is NOTHING after the backslash. No spaces, no tabs. NOTHING!

5. on the next line, you’ll see:


--exclude='.gvfs'

what this tells rsync is to exclude the .gvfs directory. What’s that? It’s the gnome virtual file system. This way, we shouldn’t get any error codes returned from running this script.

6. The

/home/ /media/bfdlinux/Homer/homerbkup/

is the meat and potatoes here.

/home/ (yes it MUST have the trailing /) tells rsync that the source of the backup is the entire /home/ directory.

7. By now you might have guessed that


/media/bfdlinux/Homer/homerbkup/

points us to the destination. /media/ is used by Ubuntu, to point to usb devices. In my case a 1 t/b usb backup drive.

the /bfdlinux is the volume label (and MOUNT POINT) of the volume.

/Homer is the hostname of my computer. I use that because I do multiple machine backups, and I can easily identify what machine I want.

/homerbkup/ (yup MUST have the trailing /) is the location where I want to backup the home directory.

Replace the /media/ (and so on) with whatever is applicable in your case.

8. Since we are backing up the entire /home/ directory, we MUST have root access so we’d run the script:


sudo sh dailyhome.sh

9. Okay how are we doing so far? Not too hard, is it?

So now that we have the system backed up, and a user just accidently
shot himself in the foot (nobody EVER does that, right?) or we need to transfer the files to another machine. What do I do? First of all like Douglas Adams said: Don’t Panic!

10. Restoring is pretty dang easy. All you do is just swap the source and destination in the script. So to restore the last line in the script would read:


/media/bfdlinux/Homer/homerbkup/ /home/

The entire restore script is simply:


#!/bin/bash
#
# RESTORE the /home/ directory
#
# * this script is now executable!
# * fully qualified path for rsync
# * now invoked with bash since the ubuntu 'sh' is anemic
# * used end of line continuation character for greater readability
# * js
#
/usr/bin/rsync -azvu --log-file=/var/log/waynodaily.log --append
--exclude '.gvfs'
/media/bfdlinux/Homer/homerbkup/ /home/

And again, you can copy, paste to your favourite editor, and save it as say restore.sh

Replace the /media/ (and so on) with whatever is applicable in your case.

And to run it:


sudo sh restore.sh

or whatever you saved it as.

simple, huh?

but let’s say we only want to restore ONE user directory. How should that look?

easy:


/media/bfdlinux/Homer/homerbkup/mikey/ /home/

Again, replace the /media/ (and so on) with whatever is applicable in your case.

terminal output will look like:

mikey/
mikey/myfile

sent 4603815 bytes received 23279 bytes 52881.07 bytes/sec
total size is 234383634253 speedup is 50654.61
nwayno@Homer:/etc/nwaynobkup$ sudo sh restore.sh
sending incremental file list
./
myfile

sent 216 bytes received 34 bytes 500.00 bytes/sec
total size is 4691 speedup is 18.76

In the next post, I’ll show you how to automatically run the backup script using crontab.

So how did you do?

0

Break for awhile

by

Hi folks —

thought I’d take a moment to update you on a few things.

I live here in Tucson, Arizona, which over the last weekend, was the lightning rod for violence. So much so, that President Obama will be here later today. For those not familiar I’ll re-post the article I wrote (from my face book account)

=============

Latest on Congresswomen Gabrielle Giffords

by Wayno Guerrini on Saturday, 08 January 2011 at 18:57

Information continues to pour into Tucson in pieces regarding today’s shooting of U.S. Representative Gabrielle Giffords (D/Az/8th CD). A total of 6 people were killed today, including one child, a girl age 9.

Suspect Jared Lee Loughner was arrested shortly after the shooting. His entries on youtube and myspace paint the portrait of a disturbed young man, who is at the centre of this tragedy.

Amongst those killed today were Federal Judge John Roll. Judge Roll had received death threats in 2009 for certifying a law suit against an Arizona Rancher, brought by il-legal immigrants.

In a televised interview with Pima County Sheriff Clarence Dupnik, he stated that in his opinion, today’s actions were part of a conspiracy. He later stated that a semi-automatic weapon (calibre NOT disclosed) had been used.

Dr. Peter Rhee, the head trauma surgeon at UMC in a news conference earlier said that the bullet passed through and out of Giffords head. Representative Giffords remains hospitalised at University Medical Centre this evening, and is expected the make a full recovery. Other victims were taken to St. Joseph’s and St. Mary’s Hospitals here in Tucson.

People who were holding a vigil for Congresswoman Giffords near her office at the intersections of Pima and Swan, found a package and were cleared from the area, as police investigate the scene. No further information is available.

Meanwhile, Tucson Police Department Chief Roberto Villasenor has stated that some officials, like Giffords neighbour to the north, Congressman Raul Griljalva (D/Az/7th CD) have been extended police protection.

Governor Jan Brewer (R/Az) has ordered ALL flags at half staff.

Wayno Guerrini

interntionalpress.com