-
Notifications
You must be signed in to change notification settings - Fork 0
/
backup_script.sh
82 lines (77 loc) · 2.52 KB
/
backup_script.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
#!/bin/bash
DATETIME=`date +%y%m%d-%H_%M_%S`
SRC=$1
DST=$2
GIVENNAME=$3
CLEANUP=$4
SRC_TAIL=$(dirname "$SRC") # Path to the source folder/file
SRC_HEAD=$(basename "$SRC") # folder/file name
showhelp(){
echo "\n\n############################################"
echo "# bkupscript.sh #"
echo "############################################"
echo "\nThis script will backup files/folders into a single compressed file and will store it in the current folder."
echo "In order to work, this script needs the following four parameters in the listed order: "
echo "\t- The full absolute path for the folder or file you want to backup."
echo "\t- The name of the Space where you want to store the backup at (not the url, just the name)."
echo "\t- The full absolute path name of the backup file (timestamp will be added to the beginning of the filename)\n"
echo "\t- An optional boolean value 'true', 'false' to indicate if local back file need to be locally delete or not. (If not given the file will be delete) \n"
echo "Example: sh bkupscript.sh /path/../testdir testSpace /path1/path2/backupdata\n"
echo "Example: sh bkupscript.sh /path/../testdir testSpace /path1/path2/backupdata true\n"
}
tarandzip(){
echo "\n##### Gathering files #####\n"
if tar -C $SRC_TAIL -czvf $GIVENNAME-$DATETIME.tar.gz $SRC_HEAD; then
echo "\n##### Done gathering files #####\n"
return 0
else
echo "\n##### Failed to gather files #####\n"
return 1
fi
}
movetoSpace(){
echo "\n##### MOVING TO SPACE #####\n"
if s3cmd put $GIVENNAME-$DATETIME.tar.gz s3://$DST; then
echo "\n##### Done moving files to s3://"$DST" #####\n"
return 0
else
echo "\n##### Failed to move files to the Space #####\n"
return 1
fi
}
cleanUp(){
echo "\n##### CLEAN UP #####\n"
if rm $GIVENNAME-$DATETIME.tar.gz ; then
echo "\n##### clean up file "$GIVENNAME"-"$DATETIME".tar.gz #####\n"
return 0
else
echo "\n##### Failed to clean up file #####\n"
return 1
fi
}
if [ ! -z "$GIVENNAME" ]; then
if tarandzip; then
if movetoSpace; then
if [ "$4" != "false" ]; then
if cleanUp; then
echo "\n##### BACKUP DONE #####\n"
return 0
else
showhelp
return 1
fi
fi
echo "\n##### BACKUP DONE #####\n"
return 0
else
showhelp
return 1
fi
else
showhelp
return 1
fi
else
showhelp
return 1
fi