Shell script to provide monthly backup of your blog

This is just a quick bash script I put together last night that performs a wget on your blog.
It should be self documenting, so should be pretty easy to follow.
You’ll just have to change three variables at the top of the file to suit your environment.

#! /bin/bash

# Update fetch_domain to the domain you wish to fetch the web content from.
readonly fetch_domain='binarymist.net'
# Update fetch_domain_files to the sub domain where your files are located.
readonly fetch_domain_files='binarymist.files.wordpress.com'
# Update wgetmonthly_dir to the local directory location you wish to save web content to.
readonly wgetmonthly_dir='/media/EXTERNAL/Documents/PenDrive/BinaryMist-Blog/WGetMonthly'
readonly help_message="Enter year and month that you want to wget from $fetch_domain in the following format: yyyy-mm

wget will fetch to the directory: ${wgetmonthly_dir}/yyyy-mm"

function print_blankline_then_message {
   echo
   echo "$1"
}

function usage {
   if [ "$1" != "" ]; then
      print_blankline_then_message "$1"
   fi
   print_blankline_then_message "$help_message"
   exit 1 #exit shell script
}

function check_if_help_required {
   if [ "$1" -eq 0 ]; then
      usage "No arguments detected."
   fi
   if [ "$2" == "help" ]; then
      usage
   fi
}

function validate_year_month {
   if [ "${#year_month}" -ne "$year_month_expected_length" ]; then
      usage "The length of the yyyy-mm should be $year_month_expected_length"
   fi

   readonly seperator='-'
   readonly seperator_index=4

   if test ${year_month:seperator_index:1} != $seperator; then
      usage "The seperating character '$seperator' should be the fifth character in the argument."
   fi

   readonly year_index=0
   readonly month_index=5

   year=${year_month:year_index:4}
   month=${year_month:month_index:2}

   # check that the year and month are positive numbers
   if [ $year -eq $year -a $year -gt -1 > /dev/null ]; then
      echo > /dev/null
   else
   usage "The year \"${year}\" is not a positive number."
   fi
   if [ $month -eq $month -a $month -gt -1 > /dev/null ]; then
      echo > /dev/null
   else
      usage "The month \"${month}\" is not a positive number."
   fi
}

function prepare_download_dir {
   cd "$wgetmonthly_dir"

   if [ -d "$year_month" ]; then
      print_blankline_then_message "Directory \"$wgetmonthly_dir/${year_month}\" already exists."
   if [ -d ${year_month}-old ]; then
   print_blankline_then_message "Directory \"$wgetmonthly_dir/${year_month}-old\" already exists. Will now delete."
   rm -rf ${year_month}-old
   fi
   print_blankline_then_message "Moving directory \"$wgetmonthly_dir/${year_month}\" to \"$wgetmonthly_dir/${year_month}-old\""
   mv $year_month ${year_month}-old
   fi
   print_blankline_then_message "Creating directory: \"$wgetmonthly_dir/$year_month\""
   mkdir "$year_month"
}

check_if_help_required $# $1
readonly year_month=$1
readonly year_month_expected_length=7
year=
month=
validate_year_month
prepare_download_dir
print_blankline_then_message "Moving into directory: $wgetmonthly_dir/$year_month"
cd "$year_month"
print_blankline_then_message "Starting wget at `date`"
print_blankline_then_message "Retreiving web content from http://$fetch_domain/$year/$month"
echo
wget --random-wait --limit-rate=20k -kmnp -N -E -H -D $fetch_domain,$fetch_domain_files $fetch_domain/$year/$month/

Advertisements

Tags: ,

One Response to “Shell script to provide monthly backup of your blog”

  1. Dave Boerger Says:

    I dugg some of you post as I thought they were handy very helpful

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


%d bloggers like this: