Backup of files in the directory.

I was working on some scripts and the changes that I was making in the scripts was very dynamic, which I did want to keep backing up in the version control system. But for the peace of my mind, I wanted to keep a copy of the scripts, whenever it was in working state.

Since I had multiple files, so it would make more sense to have a script that could copy all the files in the current directory to “old” directory without over-writing the existing files. So, I wrote a script that would postfix the files with a number. With this approach, finally what I had was the following:

#!/bin/bash -
#===============================================================================
#
#          FILE:  backup.sh
#
#         USAGE:  ./backup.sh
#
#   DESCRIPTION:  Backup all the current files.
#
#       OPTIONS:  ---
#  REQUIREMENTS:  ---
#          BUGS:  ---
#         NOTES:  ---
#        AUTHOR: Amit Agarwal (aka), [email protected]
#       COMPANY:
#       CREATED: 08/19/2011 02:43:32 PM IST
#      REVISION:  ---
#===============================================================================

count=$(cat old/count)
if [[ ! -f old/count ]]
then
  count=$(ls -1 old |sed 's/.*\.//'|grep -vi "[a-z]"|sort|tail -1)
fi
((count++))
echo $count >old/count
[[ ! -d old ]] && mkdir old
for i in *
do
	[[ -f $i ]] && cp $i old/${i%%.*}.$count
done
echo "Backed up to $count"
Enhanced by Zemanta

Creating a chroot environment in Fedora with bash and other utils.

Table of contents for chroot

  1. Creating a chroot environment in Fedora with bash and other utils.
  2. Creating a chroot environment – the script.

[ad#ad-2]

I am testing some of my scripts to work on a very old system and there the versions of the most popular applications are very old, real old :(. So, some of things that I am very used to since last couple of years, do not seem to work as expected and I need to keep verifying a lot of things on the server, very inconvinient to keep testing the script on the server (need to connect on VPN) just to test some very simple things.

But if I want to test it on my local desktop or laptop then I need to donwgrade all the applications on my system. Other option is changing the scripts or the paths to get the lower version to work. I found the third option which I liked best and gives me a very controlled environment – chroot.

So, what do we need to achieve this. Here are the steps

  • Create a empty directory
  • Create the minimum required directories under this directory
   mkdir -p {root,home,dev,etc,lib,usr,bin}
   mkdir -p usr/bin
   mkdir -p libexec/openssh
  • Copy the minimim files required for the chroot
   mknod -m 666 dev/null c 1 3
   cd etc
   cp /etc/ld.so.cache .
   cp -avr /etc/ld.so.cache.d/ .
   cp -avr /etc/ld.so.conf.d/ .
   cp /etc/ld.so.conf .
   cp /etc/nsswitch.conf .
   cp /etc/passwd .
   cp /etc/group .
   cp /etc/hosts .
   cp /etc/resolv.conf .
  • Copy the required libs for the commands that you would like to have in the chroot
ldd

This will have a output like this:
linux-gate.so.1 => (0x00e77000)
libselinux.so.1 => /lib/libselinux.so.1 (0x46bf1000)
librt.so.1 => /lib/librt.so.1 (0x46bba000)
libcap.so.2 => /lib/libcap.so.2 (0x46020000)
libacl.so.1 => /lib/libacl.so.1 (0x47fb3000)
libc.so.6 => /lib/libc.so.6 (0x46a09000)
libdl.so.2 => /lib/libdl.so.2 (0x46bb3000)
/lib/ld-linux.so.2 (0x469e8000)
libpthread.so.0 => /lib/libpthread.so.0 (0x46b97000)
libattr.so.1 => /lib/libattr.so.1 (0x46960000)
Here you would need to copy all the files mentioned after the => sign in the output to your lib directory for the chrooted environment. Also make sure that if the above are links then you copy the actual files also to ensure that you have the actual libs for the commands to work.

  • Copy the actual command to the chroot environment.

Copy the command with cp command to the bin directory of your chroot environment.

Once all this is done, you can simply chroot to this directory with the command

chroot

Voila, you are done.

Check that you have all the utils/commands that you have copied and nothing less/nothing more.

Enhanced by Zemanta

bash completion

I was working on some functions in bash to make my life easier and realized that if I added custom completion to my functions, it will be really good. So I headed over to google and searched for what I wanted. I did get a lot of information on bash completion but not a single concrete example that could help me do what I wanted. So, with the help of man pages and some results from the Google pages, I was finally able to accomplish what I wanted. So here it is:

Google link that might help here.

What : Change to config file directory. The structure of directory looked something like this

somedirectory

| —- a

| —-| —-config

| —-b

| —-| —-config

and so on..

Solution

Create a function in the .bashrc

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
_conf()

{

local curw

local PATH="somedirectory"

COMREPLY=()

curw=${COMP_WORDS[COMP_CWORD]}

COMPREPLY=( $(cd $PATH/; compgen -W $(ls -d $curw*)) )

return 0

}

And then add the completion to .bashrc

1
complete -F _conf conf

compgen can be used with in-built functions to define completions with users, directories or with “-W” to complete with custom words.

and complete is used to define the function (_conf) to use with the command (conf).

Enhanced by Zemanta