Categotry Archives: bash

0

Why doesn’t bash aliases work over an ssh connection

by

I had this problem years ago. And it cropped up again, a few days ago. I didn’t write it down then…

Symptom: bash aliases work if I am logged in as a local user. However, if you use ssh to gain access, the bash aliases are gone!

What’s happening? Actually it is very easy to fix. You simply need to add the .profile file to your home directory. The profile file is something that is executed each time you login. It is like the old Windows autoexec.bat file.

Here is the text for the file, and an addition I made to make life easier. Using a text editor of choice (gedit, nano, or….vi) create a .profile file.


# ~/.profile: executed by the command interpreter for login shells.
# This file is not read by bash(1), if ~/.bash_profile or ~/.bash_login
# exists.
# see /usr/share/doc/bash/examples/startup-files for examples.
# the files are located in the bash-doc package.

# the default umask is set in /etc/profile; for setting the umask
# for ssh logins, install and configure the libpam-umask package.
#umask 022

# if running bash
if [ -n "$BASH_VERSION" ]; then
# include .bashrc if it exists
if [ -f "$HOME/.bashrc" ]; then
. "$HOME/.bashrc"
fi
fi

# set PATH so it includes user's private bin if it exists
if [ -d "$HOME/bin" ] ; then
#
# add /sbin to the path so we can use things like ifconfig
# without have to do sudo or getting
# bash: ifconfig: No such file or directory
# PATH="$HOME/bin:$PATH"
#
PATH="$HOME/bin:/sbin:$PATH"
fi

After you create the file, log out, and back in again, and the bash aliases will work over ssh.

0

What task number is running in Linux?

by

A lot of times, I want to see what the task number is of a running task. I might want to see if there are multiple occurrences of a task, or a zombie task (defunct).

Easy way is to:

ps -ef | grep taskname

ps says give me a snapsnot of all the running tasks. The -ef says give me all the tasks, and give me a full listing. That is then piped (that is the output of ps, becomes the input to grep and we are searching for some task name.

But that get’s tiresome.

Easier way? Sure!

Add an alias to .bash_aliases!


alias thog='ps -ef | grep $1'

So use your favourite editor, add the line above, then exit.

NOTE: IN ORDER FOR CHANGES TO BE READ, YOU MUST LOGOUT/BACK IN.

So we created a command called “thog” (could be anything as long as it doesn’t conflict with any installed programme) — and I pass the command line argument $1 to it. (The first command line argument)

So the output would look like this:

nwayno@Homer:~$ thog java
root 2031 1 0 Oct28 ? 00:13:16 java -Xmx100m -Dsubsonic.home=/var/subsonic -Dsubsonic.host=0.0.0.0 -Dsubsonic.port=4040 -Dsubsonic.httpsPort=0 -Dsubsonic.contextPath=/ -Dsubsonic.defaultMusicFolder=/var/music -Dsubsonic.defaultPodcastFolder=/var/music/Podcast -Dsubsonic.defaultPlaylistFolder=/var/playlists -Djava.awt.headless=true -verbose:gc -jar subsonic-booter-jar-with-dependencies.jar
nwayno 28470 28430 0 21:07 pts/0 00:00:00 grep java
That shows me the java (or whatever) task number. Might be useful if you want to kill a task.

Want to see all the Zombie tasks? (tasks that have no parent task)

nwayno@Homer:~$ thog defunct
root 28843 2067 1 21:11 ? 00:00:00 [miniserv.pl]
nwayno 28852 28430 0 21:11 pts/0 00:00:00 grep defunct

So now you know.

By the way, I corrected the spelling of Adventures in the title. Taking it a bit easy this week, still healing.

enjoy,

Wayno

11

How to mount a usb or micro sd card, when Linux does not automount

by

This is something I am asked by n00bs a lot. My Linux system doesn’t automount my USB or micro sd card / camera card, what do I do now?

(NOTE: You can usually tell an automounted device because it will have a mount point of

/media/volumelabel

First of all, like Douglas Adams said: Don’t panic!

The first thing you want to do, is to find out if Linux can first see the device? How? Easy.

1. The first thing we want to do, is see if Linux see’s the device? How? Easy! lsusb


lsusb

Sample lsusb output

nwayno@Homer:~$ lsusb
Bus 002 Device 003: ID 03f0:4211 Hewlett-Packard OfficeJet 7300 series
Bus 002 Device 002: ID 051d:0002 American Power Conversion Uninterruptible Power Supply
Bus 002 Device 001: ID 1d6b:0001 Linux Foundation 1.1 root hub
Bus 001 Device 010: ID 0951:1603 Kingston Technology DataTraveler 1GB/2GB Pen Drive
Bus 001 Device 006: ID 0488:0022 Cirque Corp.
Bus 001 Device 004: ID 0bc2:3000 Seagate RSS LLC
Bus 001 Device 002: ID 0409:005a NEC Corp. HighSpeed Hub
Bus 001 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub

and sure enough the device I am looking for is present:

Bus 001 Device 010: ID 0951:1603 Kingston Technology DataTraveler 1GB/2GB Pen Drive

It is NOT mounted, but I know that Linux can see it.

2. The second step is to determine what the device is called. How do you do that? blkid. blkid will list all the block devices attached to a system. NOT necessarily mounted. We want ALL devices, so we do that as root. Be patient! This can take a couple of minutes (Your patience will be rewarded.)


sudo blkid

Sample blkid output

nwayno@Homer:~$ sudo blkid
[sudo] password for nwayno:
/dev/sda1: LABEL=”Homer699″ UUID=”12345AB567″ TYPE=”ntfs”
/dev/sda2: UUID=”4dac-408816aecd1e” TYPE=”swap”
/dev/sda3: LABEL=”homer” UUID=”4e75-985fd59115e4″ TYPE=”ext4″
/dev/sda4: LABEL=”waynohome” UUID=”bc4-4c34-9d7d-3f5a56d13″ TYPE=”ext4″
/dev/sdb1: LABEL=”BFDNTFS” UUID=”FC1687414″ TYPE=”ntfs”
/dev/sdb2: LABEL=”bfdlinux” UUID=”143-4c0-4db-a392-55fb94a0f” TYPE=”ext4″
/dev/sdd1: UUID=”7E-4CEC” TYPE=”vfat”
/dev/sdd2: UUID=”42062a-9f99-a4ec71894″ TYPE=”ext4″
nwayno@Homer:~$

Notice that all the devices on my system were listed. Both mounted and umounted.

So the guy I am looking for is /dev/sdd1

YOUR DEVICE MAY BE A DIFFERENT NAME.. Remember the device is STILL not mounted at this point.

We know that /dev/sdd1 is vfat which is pretty typical of micro-sd cards. They are almost always vfat.

3. We have a couple of choices on mounting the device. We can use the a system mount point /mnt or create one with mkdir. I’ll only show the mount with the system mount point, but it could easily be anything that you create. Remember that to Linux, a device is just a mount point.

4. So become root, and mount the device:


sudo mount -t vfat /dev/sdd1 /mnt

YOUR DEVICE MAY BE A DIFFERENT NAME.

Remember, only root can mount/unmount (umount) devices.

The -t tells us that the device is vfat (windows). We got that from the blkid command. If it’s not vfat, the output of blkid well tell us what the file structure is. Use man mount for more information.

/dev/sdd1 was also obtained from the blkid information, tells us to associate this device with the mount point that follows: /mnt.

5. To access you files, launch your file browser (I use nautilus and just navigate (Filesystem/mnt directory) or just do a a cd to the /mnt directory:

/mnt directory

And the contents of /mnt are:

contents of /mnt


cd /mnt

6. List the files:


ls

and you should get some output:

A6VWN4.pdf
aztcs.htm
budgeting
discovery
FNPACK
ubuntu_11.04.png
ubuntu_11.04_unity.png
unity_11_04.png
vsftpd.conf_25K

7. And how much space do you have left on that device?


df -h

the df command displays used and free space on the device:

Sample df -h output

nwayno@Homer:~$ df -h
Filesystem Size Used Avail Use% Mounted on
/dev/sda3 50G 23G 25G 48% /
none 934M 268K 933M 1% /dev
none 940M 1.5M 939M 1% /dev/shm
none 940M 344K 940M 1% /var/run
none 940M 4.0K 940M 1% /var/lock
/dev/sda4 834G 306G 486G 39% /home
/dev/sdb2 773G 397G 337G 55% /media/bfdlinux
/dev/sdb1 147G 90G 58G 61% /media/BFDNTFS
/dev/sdd1 975M 847M 128M 87% /mnt
nwayno@Homer:~$

YOUR DEVICE MAY BE A DIFFERENT NAME.

In my case I have used 847M, and have 128M free on a 975M device (/dev/sdd1 above or /mnt)

8. All done? Umount the device, and sync the output.


umount /dev/sdd1

YOUR DEVICE MAY BE A DIFFERENT NAME.


sync

sync flushes the buffers, writes any pending data to the drive, and avoids data loss!

Easy! Painless!

Wayno

0

A mini guide on converting from Ubuntu to Debian

by

How to convert from Ubuntu to Debian

This article will encompass a LOT of previous posts. This is NOT recommended for n00bs.

1. While you are downloading the Debian iso, it would be a good first step to backup /home/ and /etc/.

2. You can find simple rsync backup/restore scripts here.

3. Once the debian iso is downloaded, you should md5sum check it first. You can find the debian cd iso image checksums here.

4. Burn the iso (not copy to a cd – why this is NOT for n00bs) to a cd or dvd, depending on what you downloaded.

5. Installing from the DVD is quite different then Ubuntu. This is NOT a live cd, so you can’t experiment first.

The rest of this article will ass/u/me that you were able to install Debian sucessfully. Configuring Debian, requires some work!

1. The first thing that bit me in the buttocks (can we say that here?) – is rebranded software. You won’t find Firefox or Thunderbird. Instead you will find re-branded software. So icedove instead of Thunderbird, and IceWeasel instead of Firefox. Those are in the Debian Repositories. The problem with re-branded software, is that it is NOT upstream compatible. You can always choose to install the branded stuff (Firefox/Thunderbird) from the Mozilla site. If you need 64 bit Firefox or Thunderbird you can get find that here.

NOTE: the following code block assumes sudo (#3 below is fixed:)

As always anything with an octothorpe (#) is a comment and need not be coded.


sudo apt-get install icedove # get the re-branded thunderbird
sudo apt-get install iceweasel # get the re-branded firefox

2. The second thing is .profile so you will want to deal with that next.

3. Next, was how to easily do sudo in debian

By default – sudo does NOT work in Debian. And as you know, you get sort of used to using sudo in Ubuntu. There are some crazy ideas on how to fix this, but this is pretty easy and straight forward.

4. You probably need java run time

Guess what? That does NOT come pre-installed on Debian! You can easily add the java runtime environment (jre) for Debian.

5. if you need to restart the gdm in Debian, it’s gdm3 NOT gdm as in Ubuntu. So


sudo service gdm3 restart

6. By default, /usr/sbin is NOT in your path in Debian, but is in Ubuntu. How to fix? This was a little more complicated then I thought. But Joe had a neat fix! (so none of the system tasks are available – things like I dunno gparted, vsftpd, useradd, etc — are not accessible.)

By default, when Linux comes up, it executes /etc/profile. Within /etc/profile it sources /etc/profile.d/bashrc.local.sh

What do I mean by “sources?” Joe explains:

Executing a script is the normal way to do it. The script executes, then it exits, and it’s environment is gone forever.

When you source a script, you execute all the commands in the script and (it) remain(s) in the environment – keeping all changes made to same.

execute: /path/to/script.sh

source: source /path/to/script or more succinctly:

. /path/to/script

And that is what the following script does/ It uses a function called pathmunge to add the missing paths:


# /etc/bashrc.local.sh for Linux
#
# Local environment variables
#
export ORGANIZATION="Mirai Consulting"

#if [ $SHELL == '/bin/bash' ]; then

#
# Set prompt and aliases to something useful for an interactive shell
#

case "$-" in
*i*)
#
# Set prompt to something useful
#
case "$is" in
bash)
set -p
if test "$UID" = 0 ; then
PS1="u@h:w> "
else
tty=`tty`
PS1=`uname -n`': $PWD n(tty${tty#/dev/tty}): bash: ! > '
fi
;;
esac

case $TERM in
xterm)
PROMPT_COMMAND='echo -ne "�33]0;${USER}@${HOSTNAME%%.*}:${PWD/$HOME/~}�07"'
;;
screen)
PROMPT_COMMAND='echo -ne "�33_${USER}@${HOSTNAME%%.*}:${PWD/$HOME/~}�33\"'
;;
*)
PROMPT_COMMAND=''
;;
esac

# fix broken non-root path -

pathmunge () {
if ! echo $PATH | /bin/egrep -q "(^|:)$1($|:)" ; then
if [ "$2" = "after" ] ; then
PATH=$PATH:$1
else
PATH=$1:$PATH
fi
fi
}

#
# Path manipulation
#

pathmunge /sbin
pathmunge /usr/sbin
pathmunge /usr/local/sbin
pathmunge $HOME/sbin

unset pathmunge

set histexpand
export HISTCONTROL=ignoredups

alias ltr='ls -latr'
alias ll='ls -laFL'
alias lll='ll | less'

alias cls=clear
alias f=finger

sudo=''

alias maillog='$sudo tail -20 /var/log/mail.log'
alias postlog='$sudo grep postfix /var/log/mail | tail -40'
alias poplog='$sudo grep pop3-login /var/log/mail | tail -40'
alias msgs='$sudo tail -20 /var/log/messages'
alias krnl='$sudo tail -20 /var/log/kernel'
alias cmo='ls -Lltr /var/spool/mail'
alias psu='ps -FHu'
alias mqt='mailq|tail'
alias dmesg='/bin/dmesg|tail -40'

/bin/rm -f ~/.project
set `date`
echo "" >> ~/.project
echo " $LOGNAME logged in on `hostname` $1 $2 $3 $4" >> ~/.project
echo "" >> ~/.project

esac

#[ -r /etc/dircolors.sh ] && . /etc/dircolors.sh

alias addkey="sudo apt-key adv --keyserver keyserver.ubuntu.com --recv-keys"

#fi

7. If you have Skype you will probably need to fix that as well.

This is just a first cut draft on the differences I noted between Ubuntu and Debian.

Thanks always to Joe and Loni.

0

How to add user comments to the Linux system log

by

So how do you add a comment to the system log?

It turned out to be MUCH easier then I thought.

Why would you want to do this? Maybe you added a new driver, or new hardware, and you need to identify when you did that. This is a handy way to note that.


logger
This is a test
ctrl d

ctrl d — means to HOLD and PRESS the ctrl key and the d key at the same time. Control + d is the Linux end-of-file. (NOT control+ z, like it is in Windows)

That would log: “This is a test” to the system log.

To display the log entries, I tail(ed) (see man entry on tail)
the output of the system log (/var/log/messages) and then piped that (the output of the cat, is the input to grep), and said hey find entries with just my userid.


sudo tail -150 /var/log/messages | grep nwayno

You should get back:

Jul 26 20:18:09 Homer nwayno: this is a test.

I also figured out how to add videos to a Sansa Fuse Mp3/Video player. Hope to have that up, tomorrow.

Wayno

1

Simple annotated bash script to zap a user

by

This is a really simple script, but it underscores some key concepts here. Variable assignments, while statements, checking for null input, reading input from the terminal, comparing strings, etc.

I tried to annotate this script so you could follow along the major concepts. This is pretty basic, and so are the concepts, but I hope it gives you enough to build upon.

Bash Pitfalls helps, but you need to be fairly familiar with Bash to begin with. This was over my head. YMMV applies.

Opera renders this page correctly, Chrome not so well.

Thanks Joe for the skill command/commentary

Wayno


#!/usr/bin/bash
#
#
# By. W. Guerrini 04/20/2011 V 1.0
#
# simple script to zap a user
#
#
# configuring parameters
#
# note a very common mistake is using a $ to assign a string a value
# here we are just defaulting all the string variables to null
#
# note that there is NO white noise around the equal (=) signs a very
# common mistake! (been there done that)
#
person="" # name of person we want to zap (string/not null)
ans="" # answer received from user (string/not null)
ok2nuke="Y" # the answer we expect if it's okay to zap user
#
# the echo command does just that. it echos the contents between the
# quote marks to the terminal
#
echo "Displaying Logged in Users"

#
# issue the who command to see who is logged in
#

who

#
# The who command listed all the people logged into the system.
# Choose one to terminate

echo "What person do you want to zap?"

#
# read waits for the person to enter some text on stdin, and puts
# the contents into the variable NOTE: person NOT $person
#
read person

#
# we are checking for a null string
# the while statement will loop, until the person string is NOT null
#
# the -z checks to see if the string is null. Notice here we use
# $string name ($person vs person) for the comparison
# the brackets [] are required because we are doing various type of
# operators
#
# also note the semi-colon ; at then end of the while statement --
# yup that's needed.
#
# so this while statement says, while the string $person is null
# echo hey the string is null and re-read the input
#
# once the condition is satifisfied (non null string) the while exits (done)

while [ -z "$person" ];
do
echo "Null string. Not permitted. Enter a person to zap: "
read person
done

echo "$person is NOT null."
#
# display to the user, the name of the person we want to zap.
# note again we want to display the contents of the string $person

echo "confirm you want to zap User" $person "(Y/N)"

read ans

echo "the answer is: " $ans

#
#
# again another while loop - the answer can't be a null (nothing) string
# keep prompting and waiting till the answer is not a null
#

while [ -z "$ans" ];
do
echo "$ans String is null."
read ans
done
#
#
#
echo "$ans is NOT null."

#
# now check the answer and make sure it is a CAPITAL Y
# remember we set ok2nuke to "Y" above
#
#
#
# again in the if statement note the $string names,
# the equal signs, the brackets [], and the semi-colon ;
#
# If we got a capital "Y" then execute the if statement (skill commented out)
#
# we want to compare the strings, so we enclose them in quotes (")
# NOTE: you could also use "Y" instead of $ok2nuke but this way
# it's easier to change the value
#
if [ "$ans" = "$ok2nuke" ];
then
echo "$ans was okay to zap"
# sudo skill -n -u $person
# where n is the signal you want to send
# (-1 is a graceful hangup, -15 is a termination, -9 is an immediate kill)
echo "He's Dead, Jim!"
fi
#
#
# if we didn't get a capital "Y" for the answer, it just falls through and
# exits.
#
echo "we are finished"

Script execution looks something like:


nwayno@Homer:~$ sh zap.sh
Displaying Logged in Users
nwayno tty7 2011-04-19 18:15 (:0)
nwayno pts/6 2011-04-20 14:25 (:0.0)
donuts tty9 2011-04-20 14:45 (:2)
nwayno pts/10 2011-04-20 16:16 (:0.0)
What person do you want to zap?

Null string. Not permitted. Enter a person to zap:
donuts
$person is NOT null.
confirm you want to zap User donuts (Y/N)
Y
the answer is: Y
$ans is NOT null.
$ans was okay to zap
He's Dead, Jim!
we are finished
nwayno@Homer:~$

2

lolbash.sh

by

lolcode.com

Just for fun!

The link for lolbash.sh doesn’t work so I copied it here:

In a previous article, we talked about enabling .bash_aliases. Now here’s some aliases you can use!


# LOL!!1
# additions to .bash_aliases for us lolcats lovers
#
alias wtf='dmesg'
alias onoz='cat /var/log/errors.log'
alias rtfm='man'
alias visible='echo'
alias invisible='cat'
alias moar='more'
alias icanhas='mkdir'
alias donotwant='rm'
alias dowant='cp'
alias gtfo='mv'
alias hai='cd'
alias plz='pwd'
alias inur='locate'
alias nomz='ps -aux'
alias nomnom='killall'
alias cya='reboot'
alias kthxbai='halt'

kthxbai

Wayno

4

How to enable .bash_aliases

by

This is one of those things, I sort of take for granted. It was asked on #ubuntu (irc.freenode.net)

bash aliases

What’s an alias you say? An alias is a short form of a long command string for the bash terminal.

so how do you make it work?

1. edit the file .bashrc (using like gedit)

Remember that anything with a number sign (# or octothorpe) is a comment, and does NOT need to be coded.


cp .bashrc .bashrc.bkup # make a backup copy first!
gedit .bashrc

yes the peroid (.) at the beginning fo the file, is significant! The period (.) at the beginning of the file, tells us it is a hidden file.

2. find and uncomment the lines:


#
# enable bash_aliases by uncom the next 3 lines GU 12/1/2008
#
#
if [ -f ~/.bash_aliases ]; then
. ~/.bash_aliases
fi

That will enable you to put user written aliases into a separate file called: .bash_aliases. Save and exit.

3. My .bash_aliases looks like this (again created .bash_aliases with the editor of your choice)

Note that if you already have a .bash_aliases file, you should backup that up first.


cp .bash_aliases .bash_aliases.bkup # make a backup copy first


gedit .bash_aliases

#
# note that when you see the # in a command it is a comment and
# does NOT need to be coded
#
alias ltr=’ls -l -t -r’ # ls command: long, sort by mod date, rev order
alias logc=’tail -150 /var/log/messages | more’ # tail last 150 lines of log
alias logdebug=’tail -f /var/log/messages’ # opens log in debug mode
alias ftplog=’sudo tail -150 /var/log/vsftpd.log | more’ # look at ftp log
alias sshmnd=’ssh -X lou@annlou’ # ssh into folks computer
alias clr=’clear’ # clear the screen
alias startvnc=’x11vnc -usepw -display :0′ # start vnc server

NOTE THAT YOU NEED TO LOGOUT/IN TO GET CHANGES MADE TO .bashrc or .bash_aliases TO WORK!

you can see typing those commands are pretty long.

the format is always:

alias (alias name)=’command strong’

the single quote marks ARE important.

So if I wanted to start my vnc server, instead of a long string, i’d just type:


startvnc

and everything after the equal sign (=) is then substituted.

Pretty neat!

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?