xl_backup.sh 2.23 KB
Newer Older
js's avatar
js committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21

#!/bin/bash
#      File Name: xl_backup.sh
#        Created: 20150213
#        Purpose: backup a file or folder in a specified backup folder 
#                 and moves it to the back up folder
#  Modifications: 20150213 initial release
#    Main Author: Extreme Logic

# TODO Expand backup folder to have sub folders for year and month 

MAIN_AUTHOR_EMAIL=support@extremelogic.ph

function displayUsage {
  echo ""
  echo "Usage: ${0##*/} [file or folder to backup]"
  echo ""
  echo "Report bugs to: "$MAIN_AUTHOR_EMAIL
  echo ""
}

js's avatar
js committed
22
23
24
25
26
27
28
29
# check argument count
if [ "$#" = 0 ]; then
  echo ""
  echo "Error. Invalid argument."
  displayUsage
  exit 1
fi

js's avatar
js committed
30
31
32
33
34
35
36
37
38
# check inputs
for CURRENT_ITEM in "$@"
do
  if   [ ! -d "${CURRENT_ITEM}" ] && [ ! -f "${CURRENT_ITEM}" ]; then
    echo "Error. ${CURRENT_ITEM} is not a valid file or directory.";
    displayUsage
    exit 1
  fi
done
js's avatar
js committed
39
40

PATH_FOLDER_BACKUP=backup
js's avatar
js committed
41
FLAG_ARCHIVE=true
js's avatar
js committed
42
43
44
45
46
47
48
49
50
51
PARAM_TARGET=$1
VAR_DATE=`date +%Y%m%d-%H%M%S`
PATH_FOLDER_BACKUP_NEW=$PATH_FOLDER_BACKUP/$VAR_DATE

if [ -d $PATH_FOLDER_BACKUP ]; 
then
  echo ""
  echo "Using folder '"$PATH_FOLDER_BACKUP"' to backup data."
else
  echo ""
js's avatar
js committed
52
53
54
55
56
57
  echo "Creating backup folder '"$PATH_FOLDER_BACKUP"'."
  mkdir $PATH_FOLDER_BACKUP
  if [ ! "$?" = 0 ]; then
    echo "Error. Unable to create backup folder. '"$PATH_FOLDER_BACKUP"'."
    exit 1
  fi
js's avatar
js committed
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
fi

if [ -d $PARAM_TARGET ];
then
  mkdir $PATH_FOLDER_BACKUP_NEW
else
  if [ -f $PARAM_TARGET ];
  then
    mkdir $PATH_FOLDER_BACKUP_NEW
  else
    echo "Error. Target $PARAM_TARGET does not exist. Will not proceed."
    exit 1
  fi
fi

if [ -d $PATH_FOLDER_BACKUP_NEW ];
then
  echo "Created folder '$VAR_DATE' under '$PATH_FOLDER_BACKUP'"
js's avatar
js committed
76
77
78
79
80
81
82
83
84

  for CURRENT_ITEM in "$@"
  do
    if   [ -d "${CURRENT_ITEM}" ]; then
      echo "Performing backup for directory '$CURRENT_ITEM'";
      cp -r $CURRENT_ITEM $PATH_FOLDER_BACKUP_NEW
    elif [ -f "${CURRENT_ITEM}" ]; then
      echo "Performing backup for file '$CURRENT_ITEM'";
      cp $CURRENT_ITEM $PATH_FOLDER_BACKUP_NEW
js's avatar
js committed
85
    fi
js's avatar
js committed
86
87
88
89
90
91
92
93
  done

  if [ "$FLAG_ARCHIVE" = "true" ]; then
    cd $PATH_FOLDER_BACKUP
    VAR_ARCHIVE_NAME=${VAR_DATE}.tar.gz
    tar -zcf $VAR_ARCHIVE_NAME $VAR_DATE
    rm -fr $VAR_DATE
    echo "Created archive '$VAR_ARCHIVE_NAME'"
js's avatar
js committed
94
  fi
js's avatar
js committed
95
96
97

echo Done backup.
exit 0