Wednesday, May 16, 2012

Painless Backup with Bash Script

This is a bash script I made to make my files backup process a little bit easier. The script has four basic options:
  1. Show usage
  2. Create backup
  3. Save the backup on my backup drive on remote machine
  4. Retrieve backup from remote machine

The script is written to backup the files and directories I want in my computer but it should be pretty easy to configure it to suit your needs.

I don't want this post to be too long so I'm not posting here the whole text. If you want to download the whole script, here is the link.

Run the script with the "b" option to see a brief description of usage, options and actions.
Here are just a few comments about how it works and how to modify it to make you own backups.


Configuration

Lines 23-52 determine what files and directories to backup, what programs to use, etc. Modify these lines to suit your needs.
conf.sh
    23  DATE=$(date --rfc-3339=date)
    24  BASE="$HOME"
    25  BASE_SYS="/etc"
    26  HOME_CONF_DIR=".config"
    27  BACKUP_DIR="backup"
    28  SYNC_DIR="sync"
    29  DOCS_DIR="documents"
    30  DOWN_DIR="downloads"
    31  PACK_DIR="packages"
    32  PICS_DIR="pictures"
    33  SCRIPTS_DIR="scripts"
    34  VIDS_DIR="videos"
    35  LOCAL_HOST="$(uname -n)"
    36  BACKUP_HOST="linuxmachine"
    37  BACKUP_HOST_BASE_DIR="/mnt/backup"
    38  TAR_OPTS="cf"
    39  SYNC_OPTS="-avz --human-readable --progress"
    40  BZIP_OPTS="-9 -v -f"
    41  MIN_PARM_NUM=1
    42  MAX_PARM_NUM=2
    43  PROGS_LIST="bzip2 rsync"
    44  
    45  # options
    46  VALID_MODES=(b s r h)
    47  VALID_ACTIONS=(home documents downloads packages pictures scripts videos conffileshome 
                      conffilessys conffilesall full)
    48  
    49  # What will be backed up. Files and directories
    50  CONF_DIRS_HOME=$(echo "$HOME_CONF_DIR/"{openbox,tint2,conky})
    51  CONF_FILES_HOME=".Xresources .xinitrc"
    52  CONF_FILES_SYS=$(echo "$BASE_SYS/"{mkinitcpio.conf,inittab,fstab,vimrc,rc.conf,bash.bashrc,
                       modprobe.d/modprobe.conf})

Backing up

Making a "full" backup which includes home directories, user's configuration files and system wide configuration files:

Sync with remote host

 Retrieving backup from remote host

Main part description

Basically the script works as follows:
  1. parameter check
  2. options check
  3. check programs
  4. perform requested action
These steps correspond to the last lines on the script:

main.sh
   254  #####################################################################
   255  ########################## parameter checks #########################
   256  #####################################################################
   257  
   258  # check parameters and programs
   259  check_parm_num $# $MIN_PARM_NUM $MAX_PARM_NUM
   260  [[ $? != 0 ]] && usage
   261  check_valid_parm $1 $2
   262  [[ $? != 0 ]] && usage
   263  check_programs "$PROGS_LIST"
   264  
   265  #####################################################################
   266  ############################# main part #############################
   267  #####################################################################
   268  
   269  
   270  # builds the list of actions to be done (only needed when backing up)
   271  [[ "$1" == "b" ]] && build_action_list "$2"
   272  
   273  # perform requested actions
   274  case "$1" in
   275    "b")
   276      # create backup directory for current date and localhost
   277      [[ -d "$BASE/$BACKUP_DIR/$LOCAL_HOST/$DATE" ]] || mkdir -p 
   "$BASE/$BACKUP_DIR/$LOCAL_HOST/$DATE"
   278      cd "$BASE/$BACKUP_DIR/$LOCAL_HOST/$DATE"
   279      for action in $ACTION_LIST; do backup "$action"; done
   280    ;;
   281    "s")
   282      sync "$BACKUP_HOST" "$BACKUP_HOST_BASE_DIR" "$LOCAL_HOST"
   283    ;;
   284    "r")
   285      retrieve "$BACKUP_HOST" "$BACKUP_HOST_BASE_DIR" "$BACKUP_DIR" "$LOCAL_HOST"
   286    ;;
   287    "h")
   288      usage
   289    ;;
   290  esac

I tried to keep the main part as small and clear as possible by using functions. This functions are commented including parameters that they need and what they do.
For instance here is the "build_action_list":

build_action_list.sh
    59  #builds a list of "actions" (what is going to be backed up)
    60  # $1: action
    61  function build_action_list () {
    62    case "$1" in
    63      "full")
    64        ACTION_LIST="documents downloads packages pictures scripts videos conffileshome 
                          conffilessys" 
    65      ;;
    66      "home")
    67        ACTION_LIST="documents downloads packages pictures scripts videos"
    68      ;;
    69      "conffilesall")
    70        ACTION_LIST="conffileshome conffilessys"
    71      ;;
    72      *)
    73        ACTION_LIST="$1"
    74      ;;
    75    esac
    76    
    77    return 0
    78  }

Any comments or questions?
Hope this will make your backing up a little bit easier.

1 comment:

  1. Thank you for sharing this tutorial of making the backup files. This is necessary for us to learn to back up files because the computer can get corrupted any time.

    ReplyDelete