forked from Llewellynvdm/Backup-System
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathincl.sh
186 lines (176 loc) · 5.5 KB
/
incl.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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
#!/bin/bash
#/--------------------------------------------------------------------------------------------------------| www.vdm.io |------/
# __ __ _ _____ _ _ __ __ _ _ _
# \ \ / / | | | __ \ | | | | | \/ | | | | | | |
# \ \ / /_ _ ___| |_ | | | | _____ _____| | ___ _ __ _ __ ___ ___ _ __ | |_ | \ / | ___| |_| |__ ___ __| |
# \ \/ / _` / __| __| | | | |/ _ \ \ / / _ \ |/ _ \| '_ \| '_ ` _ \ / _ \ '_ \| __| | |\/| |/ _ \ __| '_ \ / _ \ / _` |
# \ / (_| \__ \ |_ | |__| | __/\ V / __/ | (_) | |_) | | | | | | __/ | | | |_ | | | | __/ |_| | | | (_) | (_| |
# \/ \__,_|___/\__| |_____/ \___| \_/ \___|_|\___/| .__/|_| |_| |_|\___|_| |_|\__| |_| |_|\___|\__|_| |_|\___/ \__,_|
# | |
# |_|
#/-------------------------------------------------------------------------------------------------------------------------------/
#
# @version 2.0.0
# @build 9th May, 2017
# @package Backup System
# @author Llewellyn van der Merwe <https://github.com/Llewellynvdm>
# @copyright Copyright (C) 2015. All Rights Reserved
# @license GNU/GPL Version 2 or later - http://www.gnu.org/licenses/gpl-2.0.html
#
#/-----------------------------------------------------------------------------------------------------------------------------/
##############################################################
############## ##########
############## FUNCTIONS ##########
############## ##########
##############################################################
# zip the Database
function zipDB {
# we move to tmp folder
cd "$tmpFolder"
# db server ip address
DBSERVER="$1"
# check if date use is set
if [ -z ${USEDATE+x} ]; then
USEDATE=0
fi
# set the file name
if [ "$USEDATE" -eq "4" ]; then
FILE=`date +"%Y-%m-%d:%H:%M:%S"`"_$2.sql"
elif [ "$USEDATE" -eq "3" ]; then
FILE=`date +"%Y-%m-%d"`"_$2.sql"
elif [ "$USEDATE" -eq "2" ]; then
FILE=`date +"%Y-%m"`"_$2.sql"
elif [ "$USEDATE" -eq "1" ]; then
FILE=`date +"%Y"`"_$2.sql"
else
FILE="$2.sql"
fi
#database to backup
DATABASE="$3"
# user that has access to do this
USER="$4"
# the user password
PASS="$5"
# use this command for a database server on localhost. add other options if need be.
mysqldump --opt -q --user=${USER} --password=${PASS} ${DATABASE} > ${FILE}
# gzip the mysql database dump file
gzip $FILE
# return file name
echo ${FILE}".gz"
}
function moveDB () {
# we move to tmp folder
cd "$tmpFolder"
# file name
FILE="$1"
# move file
if [ "$BACKUPTYPE" -eq "2" ]; then
$DROPBOX -q upload "$FILE" "${REMOTEDBPATH}${FILE}"
# to check the result
MOVEDBRESULT=$?
else
scp "$FILE" "$REMOTESSH:${REMOTEDBPATH}${FILE}"
# to check the result
MOVEDBRESULT=$?
fi
# for global messages
MOVEDBWHAT="$FILE"
}
function moveWEB () {
# we move to tmp folder
cd "$tmpFolder"
# local folder path
localFolder="$1"
# remote folder name
remoteFolder="$2"
# check if we should instead zip
if [ "$WEBBACKUPTYPE" -eq "2" ]; then
# set the file name
if [ "$USEDATE" -eq "4" ]; then
FILE=`date +"%Y-%m-%d:%H:%M:%S"`"_$2.zip"
elif [ "$USEDATE" -eq "3" ]; then
FILE=`date +"%Y-%m-%d"`"_$2.zip"
elif [ "$USEDATE" -eq "2" ]; then
FILE=`date +"%Y-%m"`"_$2.zip"
elif [ "$USEDATE" -eq "1" ]; then
FILE=`date +"%Y"`"_$2.zip"
else
FILE="$2.zip"
fi
# go to local folder
cd "${localFolder}"
# zip the website
zip -r -q "${tmpFolder}/${FILE}" *
# we move to tmp folder
cd "$tmpFolder"
# set the paths
PaTh="${FILE}"
remotePaTh="${REMOTEWEBPATH}${FILE}"
else
# set the paths
PaTh="${localFolder}"
remotePaTh="${REMOTEWEBPATH}${remoteFolder}"
fi
# move all file & folders
if [ "$BACKUPTYPE" -eq "2" ]; then
$DROPBOX -q upload "${PaTh}" "${remotePaTh}"
# to check the result
MOVEWEBRESULT=$?
else
rsync -ax "${PaTh}" "$REMOTESSH:${remotePaTh}"
# to check the result
MOVEWEBRESULT=$?
fi
# for global messages
MOVEWEBWHAT="${PaTh}"
}
# do some error handling for the moving
function checkMove () {
# check the area
if [ "$1" -eq "1" ]; then
if [ $MOVEDBRESULT -ne 0 ]; then
echo "Database ${MOVEDBWHAT} could not be moved!"
else
echo "Database ${MOVEDBWHAT} where moved!"
fi
else
if [ $MOVEWEBRESULT -ne 0 ]; then
echo "Website ${MOVEWEBWHAT} could not be not be moved!"
else
echo "Website ${MOVEWEBWHAT} where moved!"
fi
fi
}
function remoteHouseCleaning () {
setGit "$1"
}
# remove a folder and all its content
function rmFolder () {
local FOLDER="$1"
# ensure folder is removed
if [ -d "$FOLDER" ]
then
rm -fR "$FOLDER"
fi
}
# simple basic random
function getRandom () {
echo $(tr -dc 'A-HJ-NP-Za-km-z2-9' < /dev/urandom | dd bs=5 count=1 status=none)
}
#set git in folder
function setGit () {
cd "$1"
# use UTC+00:00 time also called zulu
actionTime=$(TZ=":ZULU" date +"%m/%d/%Y @ %R (UTC)" )
if [ -d .git ]; then
commitChanges "Updated $actionTime"
else
git init
commitChanges "First commit $actionTime"
fi;
}
# git function at end
function commitChanges () {
git add .
git commit -am "$1"
}