ASS..A Simple Script

Here is a stupid little script called ‘Pkg’ which i frequently use to backup and restore deb package files.
The script is very very simple and easy to understand

Code:

#!/bin/bash
# Pkg — script to backup and restore deb package files
#
# Copyright (c) 2009 Suhas Bharadwaj <srbharadwaj@gmail.com>
#
# This program is free software. Feel free to redistribute and/or
# modify it under the terms of the GNU General Public License v3
# as published by the Free Software Foundation.
#
# This program is distributed in the hope that it will be useful
# but comes WITHOUT ANY WARRANTY; without even the implied warranty
# of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
# See the GNU General Public License for more details.
#
# In case you modify the script according to your wish then please
# give credit to the author

VERSION=”0.1″

usage_suhas ()
{
printf “\nAbout:-\n”
printf “%6s Script  : 33[1mPkg33[0m\n”
printf “%6s Version : 33[1m$VERSION33[0m\n”
printf “%6s Author  : 33[1mSuhas Bharadwaj33[0m\n\n”
printf “%6s This is a simple script to backup and restore deb package files\n”
printf “%6s Visit33[1m https://srbharadwaj.wordpress.com/ 33[0m\n”
printf “%6s for further details and information.\n\n”
printf “Usage:-\n”
printf “%6s Run this script as root without any arguments\n”
printf “%6s to backup deb files to default directory\n\n”
printf “%6s 33[1m sudo ./pkg [options] 33[0m \n\n”
printf “Available options:-\n”
printf “%6s -h   List available arguments and usage (this message).\n”
printf “%6s -b   Backup deb pkg files to the specified dir or default dir.\n”
printf “%6s -r   Restore deb pkg files from the specified dir or default dir.\n”
printf “%6s -d   Specify the dir to use for backup or restore instead of default dir.\n”
printf “%6s -c   Clear all the deb pkg files in the specified dir or default dir.\n”
printf “\n”
}

banana()
{
printf “//\\ \nV%2s\\ \n \\%2s\\ \n%2s\\ 33[1;33m.\`-.\n%3s|\\ \`. \`.\n%3s( \\%2s\`. \`-.%24s_,.-:\\ \n%4s\\ \\%3s\`.%2s\`-._%13s__..–‘ ,-‘;/\n%5s\\ \`.%3s\`-.%3s\`-..___..—‘%3s_.–‘ ,’/\n%6s\`. \`.%4s\`-._%8s__..–‘%4s,’ /\n%8s\`. \`-_%4s \`\`–..”%6s _.-‘ ,’\n%10s\`-_ \`-.___%8s__,–‘%3s,’\n%13s\`-.__%2s\`—-”’%4s__.-‘\n%18s\`–..____..–’33[0m\n\n”
}

# Default values

BACKUP=0
RESTORE=0
DST_DIR=”`pwd`/backup_pkgs”
SRC_DIR=”/var/cache/apt/archives”

# “:” decides which options require an argument
# In the example below options “d” will require a value to be passed along

while getopts d:brch opt
do
case “$opt” in
d) #echo “d value is $OPTARG”;;
DST_DIR=$OPTARG;;
b) #echo “b value is $OPTARG”;;
BACKUP=1;;
r) #echo “r value is $OPTARG”;;
RESTORE=1;;
h) #echo “h value is $OPTARG”;;
usage_suhas
exit 0;;
c) echo “:-) UNDER CONSTRUCTION . Pls dont use -c option :-)”
exit 0;;
*) printf “ERROR: Run 33[1m$0 -h33[0m for details.\n”
exit 0;;
esac
done

# Exit if it is not run as root
if [ “$(whoami)” != root ]; then
printf “\n You should run this script as root\n\n”
exit 0
fi

if [ “$BACKUP” = 1 ] && [ “$RESTORE” = 1 ]; then
printf “\n BOTH CANT BE THE SAME\n\n”
exit 0
fi

if [ “$BACKUP” = 0 ] && [ “$RESTORE” = 0 ]; then
printf “\n BOTH CANT BE THE ZERO U HAVE TO SELECT one option\n\n”
exit 0
fi

if [ -d “$DST_DIR” ]; then
#printf  “\nDIR already present\n”
DUMMY=0
else
if [ “$BACKUP” = 1 ]; then
printf  “\n33[1m$DST_DIR33[0m NOT present, so creating it\n”
mkdir “$DST_DIR”
else
printf  “\nERROR: 33[1m$DST_DIR33[0m NOT present so cannot restore. Aborting. \n”
exit 0
fi
fi
if [ -d “$DST_DIR” ]; then
if [ “$BACKUP” = 1 ]; then
printf  “\nBacking up all deb files from 33[1m$SRC_DIR33[0m to 33[1m$DST_DIR33[0m\n”
printf “\nCopying….\n”
cp “$SRC_DIR”/*.deb “$DST_DIR”/
else
printf  “\nRestoring all deb files from 33[1m$DST_DIR33[0m to 33[1m$SRC_DIR33[0m \n”
printf “\nCopying….\n”
cp “$DST_DIR”/*.deb “$SRC_DIR”/
fi
else
printf  “\nERROR DIR NOT CREATED\n”
exit 0
fi

This script can also be used to copy some of the pkg files that your friends might have which you don’t have
Copy this script into a pen-drive, give it execution permission and u are ready to go.
Just execute ‘sudo ./pkg -b’ from any ubuntu machine from where u want to backup pkg files. All the deb files will be stored on your pen-drive
Now put the pen-drive in your machine and execute ‘sudo ./pkg -r’ All the pkg files will be moved to archives
now just do dpkg -i’ to install all the packages

Simple!!! 🙂

NOTE:-  The script is not perfect there are a few glitches, i will make it clean in the next version till then pls co-operate 🙂

Advertisements

About Suhas

The toughest question to answer :P
This entry was posted in Uncategorized. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s