-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbu.sh
executable file
·84 lines (79 loc) · 1.89 KB
/
bu.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
#!/bin/bash
#######################################################################
# bu.sh B(ack)U(p)
# arielvb - http://www.arielvb.com
# Simple backup comand line tool for files/folders on any *nix
# version: 0.1
# Changelog:
# 0.1 first version
#######################################################################
#######################################################################
# Config
#######################################################################
BUDIR="$HOME/backups/" # Backup dir
BUHISTORY=$BUDIR".bu_history" # history file
USAGE="Usage: bu [-t] [file|dir]";
USAGET="Usage: bu -t file|dir";
#######################################################################
# end Config
#######################################################################
if [[ $BASH_ARGC < 0 ]]; then
echo 'B(ack)U(p) History'
echo '__________________'
cat $BUHISTORY;
echo '__________________'
exit;
fi
isfile=1;
isdir=1;
utar=0;
FILE=$1
if [[ $1 == '-t' ]]; then
# tar enabled
if [[ $BASH_ARGC < 2 ]]; then
echo $USAGET;
exit;
fi
utar=1;
FILE=$2
fi
######################
# File check
######################
dir=`dirname $FILE`;
base=`basename $FILE`;
cd $dir;
FILE=`pwd`/$base; # absolute route
test -f "$FILE"; # Check if is file
if [[ $? == 1 ]]; then
isfile=0;
fi
test -d "$FILE"; # Check if is dir
if [[ $? == 1 ]]; then
isdir=0;
fi
if [[ ($isfile == 0) && ($isdir == 0) ]]; then
echo "Error: directory or file required"
exit;
fi
firstchar=`echo $base | cut -c1`;
if [[ $firstchar == '.' ]]; then
NEW=`echo $base | cut -c2-`
else
NEW=$base;
fi
###################
# BACKUP
###################
DATE=`date "+%Y_%m_%d_%H_%M_%S"`
if [[ $utar == 1 ]]; then
# use tar
tar -cf $BUDIR$NEW.$DATE.tar.bu "$FILE"
elif [[ $isdir == 1 ]]; then
cp -r $FILE $BUDIR$NEW.$DATE.bu
else
cp $FILE "$BUDIR$NEW.$DATE.bu"
fi
if [[ $? == 0 ]]; then
printf "$NEW\t$FILE\t$DATE\n" >> $BUHISTORY
fi