Register
It is currently Thu Sep 18, 2014 3:40 am

bdbs - bash directory backup script


All times are UTC - 6 hours


Post new topic Reply to topic  [ 8 posts ] 
Author Message
 PostPosted: Fri Jun 03, 2005 10:14 pm   
Site Admin

Joined: Tue May 17, 2005 7:31 pm
Posts: 251
Location: Georgia
i'm working on a really nice utility inspired by freeballer's request...
it will take a list of directories to back up and exclude any particular extension that you don't want to backup (be it size reasons or just no need for that file type)
i'm working on this one a little backwards than i usually work on scripts...i'm concentrating on the looks before the guts...so i can already tell there will be MANY updates :)
i won't release any of the code until i have a stable working copy that can atleast do a few of the things i want.
i will be moving the post in the backup script section that i had already started for this, as it's a work in progress and not fully functional yet :)


Top
 Profile  
 PostPosted: Fri Jun 03, 2005 10:17 pm   
Site Admin

Joined: Tue May 17, 2005 7:31 pm
Posts: 251
Location: Georgia
5-31-05
( moved 6-3-05 )

updated: 06-08-05

Code:
#!/bin/bash
# Author:   Josh Bailey
# Email:   jbsnake (at no spam) usalug.org
# Script name:   bdbs (bash directory backup script)
# Not fully ready for release.
# Still in alpha testing.

version="0.3.1"
BACKUP_FILENAME=`date '+%Y-%m-%d'`
BACKUP_DEST_DIR=""

declare -a BACKUP_DIRS;
index=0;
new_dir="nothing";
new_ext="nothing";
stop='.';
blank=' ';
fslash='/';
default=`echo ~`
cur_dir="${default}/.bdbs";

if [[ ! -d "${cur_dir}" ]]
then
   mkdir "${cur_dir}"
fi

function append_to_dir_list
{
   index=0;
   until ! read cur_object
   do
      BACKUP_DIRS[$index]="${cur_object}"
      ((index++))
   done < "${cur_dir}/dirs2backup.conf"
   
   echo ":: File being accessed"
   read -p "Press enter to continue..."
   rm -f "${cur_dir}/dirs2backup.conf"
}

function delete_dir_list
{
   rm -f "${cur_dir}/dirs2backup.conf"
}

function delete_excluded_file_list
{
   rm -f "${cur_dir}/excludedfiles.conf"
}

function create_dir_list
{

   clear;

   if [[ -e "${cur_dir}/dirs2backup.conf" ]]
   then
      echo "You already have a directory list"
      echo "You have three things you can do"
      echo "(d)elete the existing list"
      echo "(a)ppend to the existing list"
      echo "(c)ancel the create a list feature"
      read -p "Please enter one of the following [d|a|c]: " createChoice
      createChoice=${createChoice:0:1};
      createChoice=`echo "${createChoice}" | tr [:upper:] [:lower:]`
   
      case $createChoice in
         d)   delete_dir_list;
            delete_excluded_file_list;
            quit_dir_list='y';
            echo ":: Directory list deleted"
            read -p "Press enter to continue..."
            ;;
         a)   append_to_dir_list;
            delete_excluded_file_list;
            quit_dir_list='n';
            ;;
         c)   quit_dir_list='y';
            ;;
         *)   echo "Not a valid entry";
            read -p "Press enter to continue..."
            quit_dir_list='y';
            ;;
      esac
   else
      quit_dir_list='n';
   fi

   if [[ $quit_dir_list = 'n' ]]
   then
      clear;
   
      echo "You may list directories you wish to back up here."
      echo "You can not list more than one directory on a"
      echo "line.  You will be able to enter as many"
      echo "directories as you want.  Also keep in mind that"
      echo "the tar command will work recursively.  Meaning"
      echo "you do not need to make multiple entries under the"
      echo "same directory tree."
      echo "Example:  /home/user/downloads"
      echo "Will be included in the listing of /home/user"
      echo "Although redundant entries like the above won't harm"
      echo "anything, it will waste potential space for something"
      echo "more important."
      echo ""

      new_dir="nothing"
      until [[ "${new_dir}" = "${stop}" || ${#new_dir} = 0 || "${new_dir}" = "${blank}" ]]
      do
         echo "When done enter either a period '.' or a blank entry."
         read -p "Please enter a directory to backup: " new_dir

         if [[ ${new_dir} = ${stop} || ${#new_dir} = 0 || "${new_dir}" = "${blank}" ]]
         then
            break;
         fi
   
         if [[ ${new_dir:${#new_dir}-1:1} != $fslash ]]
         then
            new_dir="${new_dir}/"
         fi

         for ((i=0; i <= `echo "${#BACKUP_DIRS[*]}"`; i++))
         do
            if [[ ! -d "${new_dir}" ]]
            then
               echo ":: Error: ${new_dir} does not exist!"
               addit='n';
               break;
            fi

            if [[ "${new_dir}" = "${BACKUP_DIRS[$i]}" ]]
            then
               echo ":: Error: that directory will already be backed up";
               addit='n';
               break;
            else
               addit='y';
            fi
         done

         if [[ $addit = 'y' ]]
         then
            if [[ ${new_dir} != ${stop} ]]
            then
               BACKUP_DIRS[$index]="${new_dir}";
               ((index++));
            fi
         fi
      done

      for ((i=0; i < `echo "${#BACKUP_DIRS[*]}"`; i++))
      do
         echo "${BACKUP_DIRS[$i]}" >> "${cur_dir}/dirs2backup.conf";
      done
   
      echo "${cur_dir}"
      read -p "press enter to continue..."
      cat "${cur_dir}/dirs2backup.conf" | less
   fi

} # function create_dir_list done

function delete_ext_list
{
   rm -f "${cur_dir}/excludelist.conf"
}

function append_to_ext_list
{
   index=0;
   until ! read new_ext
   do
      EXCLUDE_LIST=${new_ext}
      ((index++))
   done < "${cur_dir}/excludelist.conf"
   
   echo ":: File being accessed"
   read -p "Press enter to continue..."
   rm -f "${cur_dir}/excludelist.conf"
}

function create_exclude_list
{

   if [[ -e "${cur_dir}/excludelist.conf" ]]
   then
      echo "You already have a filetype list"
      echo "You have three things you can do"
      echo "(d)elete the existing list"
      echo "(a)ppend to the existing list"
      echo "(c)ancel the create a list feature"
      read -p "Please enter one of the following [d|a|c]: " createChoice
      createChoice=${createChoice:0:1};
      createChoice=`echo "${createChoice}" | tr [:upper:] [:lower:]`
   
      case $createChoice in
         d)   delete_ext_list;
            delete_excluded_file_list;
            quit_ext_list='y';
            echo ":: Filetype list deleted"
            read -p "Press enter to continue..."
            ;;
         a)   append_to_ext_list;
            delete_excluded_file_list;
            quit_ext_list='n';
            ;;
         c)   quit_ext_list='y';
            ;;
         *)   echo "Not a valid entry";
            read -p "Press enter to continue..."
            quit_ext_list='y';
            ;;
      esac
   else
      quit_ext_list='n';
   fi

   if [[ $quit_ext_list = 'n' ]]
   then
      clear;
   
      echo "Here is where you can type the file extensions of"
      echo "files you wish to exclude from your archive.  "
      echo "There can be many reasons for wishing to not backup"
      echo "particular filetypes.  Whether its size restrictions,"
      echo "files that aren't needed, or you just don't want to "
      echo "backup that particular filetype."
      echo "This script will only keep from adding more than one"
      echo "of the same file extension, it will NOT check whether"
      echo "a file extension is valid."
      echo "You can either enter the extension with or without the"
      echo "dot (.) at the beginning"
      echo "Some useful extensions to ignore are:"
      echo "iso, mp3, flac, wav, divx, avi, and tar"
      echo ""
   
      new_ext="nothing";
      until [[ ${new_ext} = ${stop} || ${#new_ext} = 0 || ${new_ext} = ${blank} ]]
      do
         echo "When done enter either a period '.' or a blank entry."
         read -p "Please enter an extension to exclude: " new_ext
      
         if [[ ${new_ext} = ${stop} || ${#new_ext} = 0 || ${new_ext} = ${blank} ]]
         then
            break;
         fi
   
         if [[ ${new_dir:0:1} = $stop ]]
         then
            new_ext="${new_ext:1}"
         fi

         for ((i=0; i <= `echo "${#EXCLUDE_EXT[*]}"`; i++))
         do
            if [[ "${new_ext}" = "${EXCLUDE_EXT[$i]}" ]]
            then
               echo ":: Error: that extension will already be excluded";
               addit='n';
               break;
            else
               addit='y';
            fi
         done

         if [[ $addit = 'y' ]]
         then
            if [[ ${new_ext} != ${stop} ]]
            then
               EXCLUDE_EXT[$index]="${new_ext}";
               ((index++));
            fi
         fi
      done

      for ((i=0; i < `echo "${#EXCLUDE_EXT[*]}"`; i++))
      do
         echo "${EXCLUDE_EXT[$i]}" >> "${cur_dir}/excludelist.conf";
      done

      cat "${cur_dir}/excludelist.conf" | less;
      clear;
      echo "Now we will create our excluded files list"
      read -p "Press enter to continue..."
   
      create_excluded_files_list;
   fi

} # function create_exclude_list done

function create_tar_file
{
   tar --exclude-from "${cur_dir}/excludedfiles.conf" -czvf "${BACKUP_DEST_DIR}${HOSTNAME}--${BACKUP_FILENAME}.tgz" `cat "${cur_dir}/dirs2backup.conf"`

   echo ":: File: '${BACKUP_DEST_DIR}${HOSTNAME}--${BACKUP_FILENAME}.tgz' created"
   read -p ":: Press enter to continue..."
}

function headerfile
{
   clear;
   echo " ---------------------------------------"
   echo -ne "|\t    __        ____           \t|
|\t   / /_  ____/ / /_  _____   \t|
|\t  / __ \/ __  / __ \/ ___/   \t|
|\t / /_/ / /_/ / /_/ (__  )    \t|
|\t/_.___/\__,_/_.___/____/     \t|
|\t                        ${version}\t|
"
   echo " ---------------------------------------"
   echo -e "|\tbash directory backup script\t|"
   echo " ---------------------------------------"
}

function main_menu
{
   echo -ne "|\t1)  Create directory list       |\n|\t2)  Create filetypes list       |\n|\t3)  View backup directory list  |\n|\t4)  View excluded filetypes     |\n|\t5)  View excluded files         |\n|\t6)  Set backup directory        |\n|\t7)  Create backup file          |\n|\t0)  Done                        |\n"
   echo " ---------------------------------------"
   read -p "Please enter one from above [0-7]: " choice
   case $choice in
      0)   echo "Thanks for using this script!";
         exit;
         ;;
      1)   create_dir_list;
         ;;
      2)   create_exclude_list;
         ;;
      3)   view_backup_dir_list;
         ;;
      4)   view_exclude_list;
         ;;
      5)   view_excluded_files_list;
         ;;
      6)   set_backup_dir;
         ;;
      7)   create_tar_file;
         ;;
      *)   echo "That choice is not valid!";
         echo "Please try again";
         ;;
   esac

} # function main_menu done

function set_backup_dir
{
   if [[ ${#BACKUP_DEST_DIR} = 0 ]]
   then
      echo ":: You have not yet set a backup destination"
      echo ":: directory.  Please enter the directory"
      echo ":: where you would like the backup stored"
      echo ":: '${default}/.bdbs/' is the default"
      read -p "-> " BACKUP_DEST_DIR
      if [[ ${#BACKUP_DEST_DIR} = 0 ]]
      then
         BACKUP_DEST_DIR="${default}/.bdbs/"
      fi

      if [[ ! -d "$BACKUP_DEST_DIR" ]]
      then
         echo ":: Error: ${BACKUP_DEST_DIR} does not exist!"
         echo ":: You will have to try again"
         BACKUP_DEST_DIR="";
      fi
   fi
}

function check_dir_list
{
   if [[ -e "${cur_dir}/dirs2backup.conf" ]]
   then
      return 0;
   else
      return 1;
   fi
}

function view_backup_dir_list
{
   clear;
   if check_dir_list
   then
      cat "${cur_dir}/dirs2backup.conf" | less;
   else
      echo "You have not yet made a directory list"
      read -p "Press enter to continue..."
   fi
}

function view_exclude_list
{
   clear;
   
   if check_dir_list
   then
      if [[ -e "${cur_dir}/excludelist.conf" ]]
      then
         cat "${cur_dir}/excludelist.conf" | less
      else
         echo "You have not yet made an excluded filetype list"
         read -p "Press enter to continue..."
      fi
   else
      echo "You have not yet made a directory list"
      read -p "Press enter to continue..."
   fi
}

function view_excluded_files_list
{
   clear;

   if check_dir_list
   then
      if [[ -e "${cur_dir}/excludedfiles.conf" ]]
      then
         cat "${cur_dir}/excludedfiles.conf" | less;
      else
         echo "You have not yet made an excluded files list"
         read -p "Press enter to continue..."
      fi
   else
      echo "You have not yet made a directory list"
      read -p "Press enter to continue..."
   fi
}

function create_excluded_files_list
{

   until ! read dir
   do
      until ! read extension
      do
         find "${dir}" -name "*.${extension}" 2> /dev/null >> "${cur_dir}/excludedfiles.conf"
      done < "${cur_dir}/excludelist.conf"
   done < "${cur_dir}/dirs2backup.conf"
   
   sort "${cur_dir}/excludedfiles.conf" | uniq > "${cur_dir}/excludedfiles2.conf"
   mv "${cur_dir}/excludedfiles2.conf" "${cur_dir}/excludedfiles.conf"
}

while true
do
   headerfile;
   main_menu;
done


Last edited by jbsnake on Wed Jun 08, 2005 10:37 pm, edited 3 times in total.

Top
 Profile  
 PostPosted: Fri Jun 03, 2005 10:18 pm   
Site Admin

Joined: Tue May 17, 2005 7:31 pm
Posts: 251
Location: Georgia
6-02-05

(moved 6-3-05)

:: creating a function that will take user input to build a list of directories to backup

:: creating a function that will take user input to build a list of extensions to ignore during backup

:: creating a function that will take user input to build a configuration file to read from so that the script may be used automatically and no user intervention will be necessary.

so far that's all the ideas i have and are working on :) :)


Top
 Profile  
 PostPosted: Tue Jun 07, 2005 10:30 pm   
Site Admin

Joined: Tue May 17, 2005 7:31 pm
Posts: 251
Location: Georgia
:: creating a function that will take user input to build a list of directories to backup (done)

:: creating a function that will take user input to build a list of extensions to ignore during backup (done)

:: creating a function that will take user input to build a configuration file to read from so that the script may be used automatically and no user intervention will be necessary.

:: still working on this one...i have worked mostly on the iface so far...i like the way it looks, and would like some user input on atleast the way it works...granted it does not work fully functional yet (atleast i havn't tried to back anything up with it) it has most of the guts working along with the spiffy iface...now i just need to add a little more error checking and a couple more features such as automatic use. :)


Top
 Profile  
 PostPosted: Wed Jun 08, 2005 7:01 am   
Site Admin
User avatar

Joined: Sun May 15, 2005 9:36 pm
Posts: 667
Location: Des Moines, Iowa
Code:
---------------------------------------
|           __        ____              |
|          / /_  ____/ / /_  _____      |
|         / __ \/ __  / __ \/ ___/      |
|        / /_/ / /_/ / /_/ (__  )       |
|       /_.___/\__,_/_.___/____/        |
|                               0.2.4   |
---------------------------------------
|       bash directory backup script    |
---------------------------------------
|       1)  Create directory list       |
|       2)  Create filetypes list       |
|       3)  View backup directory list  |
|       4)  View excluded filetypes     |
|       5)  Set backup directory        |
|       6)  Create backup file          |
|       0)  Done                        |
---------------------------------------
Please enter one from above [0-6]:


Number 1. worked okay....
2 Did not.
3. Took me a minute to figure out how to exit (TYPE q ) ??
4 Did not
5 Did not
6 Did not

the interface looks suprisingly familiar :lol:

Setting up a function to make it automatic ..... IE: creating a cron job.... would be nice. Then you could setup certain directories to backup and just "ADD" to them as needed with the script..... or REMOVE them as needed for that matter.

Perhaps a function to UPLOAD backups to a remote location ?
Maybe a function to ENCRYPT the tar file before upload ??

Looks like a useful script to me :)


Top
 Profile WWW  
 PostPosted: Wed Jun 08, 2005 9:36 am   
Site Admin

Joined: Tue May 17, 2005 7:31 pm
Posts: 251
Location: Georgia
EDIT: i looked at a screen shot of bbips and i see just how close the iface of this looks to that project...i'm going to have to do some major modifications...i wonder if having seen it a few times had subconsiously guided my development...even the version number appears in almost exactly the same place....scary

as far as what worked and what didn't...i had forgotten to re-upload once i was done working on it...most of the options work now...ofcourse i need to test it more :)

i was pretty agravated at figlet...only the slant font seems to work well with echo...i tried to incase the whole thing inside a box to better isolate the header descriptions....hopefully the spacing comes out better than what you have posted :)

what it looks like on my Konsole
Image


Top
 Profile  
 PostPosted: Wed Jun 08, 2005 11:14 am   
Site Admin
User avatar

Joined: Sun May 15, 2005 9:36 pm
Posts: 667
Location: Des Moines, Iowa
Works ok.......

I couldn't get the

6) Set backup directory

to work.... maybe i did something wrong lol.
It did create a backup tar.gz file for me.

A couple quick suggestions.

1. Let people enter the backup name for the tar.gz file (so we know what to look for lol.)
2. For all the .dat and config files..... would it be better to have the script create a directory in ~/ .......... say something like .bdbs and store all the config data there so it doesn't get lost ???

also, when the backup is done, it would be nice if the program echo'd back the name and location of the backup...... took me a minute to realize it was named linux ;)

Worked good though. I did try to "exclude" .tar.gz files.... and it didn't put that in there.. not sure why.

Anyway, hope that helps some. :D


Top
 Profile WWW  
 PostPosted: Wed Jun 08, 2005 10:39 pm   
Site Admin

Joined: Tue May 17, 2005 7:31 pm
Posts: 251
Location: Georgia
crouse wrote:
I couldn't get the

6) Set backup directory

to work.... maybe i did something wrong lol.
It did create a backup tar.gz file for me.

A couple quick suggestions.

1. Let people enter the backup name for the tar.gz file (so we know what to look for lol.)
2. For all the .dat and config files..... would it be better to have the script create a directory in ~/ .......... say something like .bdbs and store all the config data there so it doesn't get lost ???

also, when the backup is done, it would be nice if the program echo'd back the name and location of the backup...... took me a minute to realize it was named linux


:: fixed the backup destination directory issue
:: still have to work on the backup name change
:: created a bdbs directory under ~
:: added notice after backup creation
:: fixed a few minor bugs and made it a little more user friendly
:: please break it and let me know how :)


Top
 Profile  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 8 posts ] 

All times are UTC - 6 hours


Who is online

Users browsing this forum: No registered users and 2 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  


BashScripts | Promote Your Page Too
Powered by phpBB © 2011 phpBB Group
© 2003 - 2011 USA LINUX USERS GROUP