-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbackup
executable file
·269 lines (230 loc) · 8.88 KB
/
backup
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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
#!/bin/bash
# TODO: adding hostname to backupfile
# TODO: gathering some info from args 1.where to save backup 2. which directories
# TODO: changing scp to rsync
# TODO: changing log method
# TODO: read about log levels
# TODO: gathering some info from env
# TODO: get rid of these
# TODO: adding clients
# our method of backing up is pulling from client nodes to backup server and currentlly this software just supports git/filesystem/mysql backups
#---------------------- clients actions
# #ssh-keygen
# adding their public key to backup server where this script is running
# specifying thire addresses at the end of this script
#---------------------- Some Global variables
NOW=$(date +"%Y-%m-%d %H:%M:%S")
LOGFILE="/var/log/backup.log"
BACKUP_DEST="sdg@172.20.90.16:/home/$USER/backup/"
red=`tput setaf 1`
green=`tput setaf 2`
reset=`tput sgr0`
set -e
clear
echo "#################################################################"
echo "######## This piece of code is GPL V3 and is free to use ########"
echo "#################################################################"
if ! [ -d ${BACKUP_DEST} ]; then
echo "${BACKUP_DEST} does not exist creatig directory ..."
mkdir ${BACKUP_DES0T}
fi
echo "Backup destination is ${green}${BACKUP_DEST}${reset}"
echo "Logs are saved in ${green}${BACKUP_DEST}:${LOGFILE}${reset}"
echo "git & mysql is not working yet"
# Parse input parameters
usage() {
echo "Usage: $0 [options]
Options are:
--mysql|-m (-a <user>:<pass>@<ip address>) (--user|-u <user> --password|-p <password>)
--files|-f <file or folder address> ((--increamental|-i) | (--snapshot|-s))
--address <ip address>
--git|-g <git address>
examples:
backup -m -a user:pass@127.0.0.1
backup -m -u root -p pass --address 127.0.0.1
backup --files|-f </etc/> --increamental|-i --snapshot|-s --address 127.0.0.1
backup --git|-g http://172.20.90.11/address.git
For more info: man $0"
# TODO: no man page exists yet
exit 1
}
#-----------------------------------------------------------------------------------
# the following methods need to improve ASAP
# to backup git repositories
#git_backup(){
# git_address=$1
# # TODO: merge this sction to script -- gits
#
# dir = "a"
# echo "---------------------- $NOW git backup starts ---------------------" >> $LOGFILE 2>&1
# for dir in `ssh gitdev@172.20.5.240 | awk '{print $2}' | tr '\n' ' '`
# do
# if [ $dir = "support@xamin.ir," -o $dir = "W" ]; then
# continue
# else
# path="/home/backup/git_172.20.5.240/$dir"
# if [ -d "${path}" ]; then
# cd $path
# echo "$(date) :: dir=$dir path=$path" >> $LOGFILE 2>&1
# echo -n "$(date) :: $dir :: " >> $LOGFILE 2>&1
# git pull >> $LOGFILE 2>&1
# else
# cd /home/backup/git_172.20.5.240/
# echo "$(date) :: dir=$dir path=$path" >> $LOGFILE 2>&1
# echo -n "$(date) :: $dir :: " >> $LOGFILE 2>&1
# git clone gitdev@172.20.5.240:$dir $path >> $LOGFILE 2>&1
# fi
#
# fi
# done
# echo "-------------------------------------------------------------------" >> $LOGFILE 2>&1
#};
# Test Case: git_backup <git address url>
#######################################################################
files_backup(){
# Destination should be a directory name
# backup --files|-f <"/etc/"> --increamental|-i --snapshot|-s --address 127.0.0.1
# TODO: adding hostname to backupfile
${SRC_DIRS}=${1}
${METHOD}=${2}
DEST_FILE="${NOW}.tar.bz2"
echo "$(date +"%Y-%m-%d %H:%M:%S") -- Backing up data to $FILE file" >> $LOGFILE 2>&1
#logger "$(date +"%Y-%m-%d %H:%M:%S") -- Backing up data to $FILE file"
if [ ${METHOD} = "scp" ]; then
tar cvjf /tmp/$DEST_FILE ${SRC_DIRS}
echo "$(date +"%Y-%m-%d %H:%M:%S") -- Moving to $FILE file to ${BACKUP_DEST}" >> $LOGFILE 2>&1
#logger "$(date +"%Y-%m-%d %H:%M:%S") -- Backing up data to $FILE file"
scp -i /root/.ssh/id_rsa /tmp/$FILE 172.20.90.16@${BACKUP_DEST}:/home/backup/
echo "$(date +"%Y-%m-%d %H:%M:%S") -- Removing /root/$FILE temporary file" >> $LOGFILE 2>&1
#logger "$(date +"%Y-%m-%d %H:%M:%S") -- Backing up data to $FILE file"
rm /tmp/$FILE
fi
};
# Test Case: files_backup <method> <source(s)> <destination>
#######################################################################
#mysql_backup(){
# # TODO: merge this section to script -- mysql dbs
## ADDRESS="172.20.5.212"
## FILE="mysql_${ADDRESS}_${NOW}.tar.bz2"
##
## echo "---------------- $NOW ---------------" >> /root/backup.log
##
## echo "Backing up data to $FILE file" >> /root/backup.log
## mysqldump -u root -pxaminmysql92 portal > portal_$NOW.sql
## mysqldump -u root -pxaminmysql92 wiki > wiki_$NOW.sql
## mysqldump -u root -pxaminmysql92 ask > ask_$NOW.sql
##
## tar cvjf /tmp/$FILE portal_$NOW.sql ask_$NOW.sql wiki_$NOW.sql
#
# echo "Moving to $FILE file to 172.20.5.211/home/backup" >> /root/backup.log
# scp -i .ssh/id_rsa /tmp/$FILE saver@172.20.5.211:/home/backup/
#
# echo "Removing /root/$FILE temporary file" >> /root/backup.log
# rm /tmp/$FILE portal_$NOW.sql ask_$NOW.sql wiki_$NOW.sql
#};
# Test case: mysql_backup
######################################################################
#-----------------------------------------------------------------------------------
if [ -z ${1} ];then
usage
fi
ssh sdg@31.184.132.files_backup "/etc/" "scp"
#for i in $@ ; do
# case "${1}" in
# "--mysql"|"-m")
# if [ -z "${2}" ] ; then
# echo "${red}No parameter defining mysql attributes${reset}"
# logger "No parameter defining mysql attributes"
# usage
# fi
#
# shift
# shift
# ;;
# "--file"|"-f")
# echo $NOW
# if [ -z "${2}" ] ; then
# echo "${red}No parameter defining file or directory${reset} "
# logger "No parameter defining file or directory"
# usage
# shift
# fi
#
# ADDRESS=${2} #like this format 127.0.0.1:/var/www/
# if [ \( "${3}" = "-i" \) -o \( "${3}" = "--increamental" \) ];then
# METHOD="rysnc"
# else
# if [ \( "${3}" = "-s" \) -o \( "${3}" = "--snapshot" \) ];then
# METHOD="scp"
# else
# usage
# fi
#
# fi
# #backup --files|-f </etc/> --increamental|-i --snapshot|-s --address 127.0.0.1
# echo "${ADDRESS} ${METHOD}"
# files_backup ${ADDRESS} ${METHOD}
# exit 0
# ;;
# "--git"|"-g")
# if [ -z "${2}" ] ; then
# echo "${red}No parameter defining git repo.${reset}"
# logger "No parameter defining git repo."
# usage
# fi
# ;;
# "--help"|"-h")
# echo $NOW
# usage
# shift
# ;;
# *)
# ;;
# esac
#done
#!/bin/bash
#NOW=$(date +"%Y-%m-%d %H:%M:%S")
DATE=$(date +"%Y-%m-%d")
HOSTNAME="C2"
TOBACKUP="/etc/"
BACKUP_DEST="/tmp/${HOSTNAME}-${DATE}.tar.bz2"
HOST_IP="172.20.90.11"
BACKUP_SERVER_IP="172.20.90.16"
LOGFILE="/var/log/backup.log"
tar cvjf ${BACKUP_DEST} ${TOBACKUP}
echo "$(date +'%Y-%m-%d %H:%M:%S') -- $TOBACKUP file created in ${BACKUP_DEST}" >> $LOGFILE 2>&1
scp -i /root/.ssh/id_rsa ${BACKUP_DEST} root@${BACKUP_SERVER_IP}:/home/backup/
echo "$(date +'%Y-%m-%d %H:%M:%S') -- $TOBACKUP file copied to backup server ${BACKUP_SERVER_IP}" >> $LOGFILE 2>&1
rm ${BACKUP_DEST}
echo "$(date +'%Y-%m-%d %H:%M:%S') -- ${BACKUP_DEST} removed from local machine ${HOST_IP}" >> $LOGFILE 2>&1
#0 1 * * 4 sh /root/backup > crontab -e
#!/bin/bash
#NOW=$(date +"%Y-%m-%d %H:%M:%S")
DATE=$(date +"%Y-%m-%d")
HOSTNAME="C4"
TOBACKUP="/etc/ /tmp/keystonedb_$DATE.sql /tmp/mysql_$DATE.sql /tmp/novadb_$DATE.sql /tmp/neutrondb_$DATE.sql /tmp/glancedb_$DATE.sql"
BACKUP_DEST="/tmp/${HOSTNAME}-${DATE}.tar.bz2"
HOST_IP="172.20.90.13"
BACKUP_SERVER_IP="172.20.90.16"
LOGFILE="/var/log/backup.log"
#--------------------------------
DB_NAME=""
DB_HOST="localhost"
DB_USER="root"
DB_PASS="c4!@#$%"
#--------------------------------
mysqldump -u ${DB_USER} -p${DB_PASS} novadb > /tmp/novadb_$DATE.sql
mysqldump -u ${DB_USER} -p${DB_PASS} neutrondb > /tmp/neutrondb_$DATE.sql
mysqldump -u ${DB_USER} -p${DB_PASS} glancedb > /tmp/glancedb_$DATE.sql
mysqldump -u ${DB_USER} -p${DB_PASS} keystonedb > /tmp/keystonedb_$DATE.sql
mysqldump -u ${DB_USER} -p${DB_PASS} mysql > /tmp/mysql_$DATE.sql
#${TOBACKUP}="/etc/ /tmp/keystonedb_$DATE.sql /tmp/mysql_$DATE.sql /tmp/novadb_$DATE.sql /tmp/neutrondb_$DATE.sql /tmp/glancedb_$DATE.sql"
echo "$(date +'%Y-%m-%d %H:%M:%S') -- SQL backup from novadb,neutrondb,glancedb,keystonedb,mysql has been created" >> $LOGFILE 2>&1
tar cvjf ${BACKUP_DEST} ${TOBACKUP}
echo "$(date +'%Y-%m-%d %H:%M:%S') -- $TOBACKUP file created in ${BACKUP_DEST}" >> $LOGFILE 2>&1
scp -i /root/.ssh/id_rsa ${BACKUP_DEST} root@${BACKUP_SERVER_IP}:/home/backup/
echo "$(date +'%Y-%m-%d %H:%M:%S') -- $TOBACKUP file copied to backup server ${BACKUP_SERVER_IP}" >> $LOGFILE 2>&1
rm -f ${BACKUP_DEST}
echo "$(date +'%Y-%m-%d %H:%M:%S') -- ${BACKUP_DEST} removed from local machine ${HOST_IP}" >> $LOGFILE 2>&1
rm -f /tmp/keystonedb_$DATE.sql /tmp/mysql_$DATE.sql /tmp/novadb_$DATE.sql /tmp/neutrondb_$DATE.sql /tmp/glancedb_$DATE.sql
echo "$(date +'%Y-%m-%d %H:%M:%S') -- temporary sql files removed from /tmp/" >> $LOGFILE 2>&1