-
Notifications
You must be signed in to change notification settings - Fork 23
/
backup.sh
executable file
·233 lines (220 loc) · 5.86 KB
/
backup.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
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
#!/bin/bash
# --------------------------------------------------------
# Centos-Server-Backup-Script.
#
#
# by RaveMaker & ET
# http://ravemaker.net
# http://etcs.me
#
# this script backup important files from your system:
# system files, db files, user files, services, etc.,
# and create .tgz in a backup folder for each backup session.
# root access required.
# --------------------------------------------------------
function remoteBackup {
if $BACKUP_REMOTELY ; then
printf "Remote backup host configured... "
mountRemote
else
echo "Skipping remote backup."
fi;
}
function mountRemote {
if ! [[ $(mount -v | grep -i -e 'type smb' -e 'type cifs') ]] ; then
printf "Mounting remote backup host..."
mount -t cifs //$REMOTE_HOST/$REMOTE_SHARE $backupdir -o username=$REMOTE_USER,password=$REMOTE_PASS,nounix
RESULT=$?
if [ $RESULT == 0 ] ; then
printf "Ok.\n"
else
printf "Failed!\n"
exit $RESULT;
fi;
fi;
}
function checkLists {
if ! [ -f $backuplistfile ] ; then
echo "Missing backup Listfile. create $backuplistfile"
exit
fi;
}
function checkBackupStatus {
if $BACKUP_DAILY_ONLY_ONCE ; then
if [ -f $backupdir/0/$filename ] ; then
echo "Backup already exist - try again tomorrow."
exit;
fi;
fi;
if [ -d $tempdir ] ; then
echo ""
echo "Backup is already running. remove temp folder to reset."
exit;
fi;
}
function createTemporaryFolder {
printf "Creating temporary directory.. "
if $WRITE_CHANGES ; then
mkdir $tempdir
printf "Ok\n"
else printf "Skipping\n"
fi;
}
function deleteOldestBackup {
if $WRITE_CHANGES ; then
if [ -d $backupdir/$1/ ] ;
then
echo "Deleteing Number $1";
rm -r -f $backupdir/$1/ ;
fi;
fi;
}
function deleteOldBackups {
count=$(ls -1 $backupdir | wc -l)
for (( c=$count; c>=$1; c-- ))
do
deleteOldestBackup $c
done
}
function shiftBackup {
if [ -d $backupdir/$1/ ] ; then
printf "Moving Number $1 to Number $2.. ";
if $WRITE_CHANGES ; then
mv $backupdir/$1 $backupdir/$2/ ;
printf "Ok\n"
else printf "Skipping\n"
fi;
fi;
}
function shiftBackups {
for (( c=$BACKUP_COPIES; c>0; c-- ))
do
b=$c
let "b -= 1"
shiftBackup $b $c
done
}
function dumpSQL {
printf "Regenerating DB list file.. ";
if $WRITE_CHANGES && $BACKUP_MYSQL ; then
mysql -u $SQL_USER -p$SQL_PASSWD -Bse 'show databases' > $listfile
printf "Dumping SQL Databases.. ";
cat $listfile | while read line
do
dbname=$line
if [ $line != "information_schema" ] ;
then
mysqldump --events --ignore-table=mysql.events -u $SQL_USER -p$SQL_PASSWD $dbname > $tempdir/$dbname.sql
fi
done
printf "Ok\n"
else printf "Skipping\n"
fi;
}
function createBackup {
echo "Creating TGZ Backup file for..";
echo "directories:"
cat $backuplistfile | while read line
do
for d in $line; do
echo $d
# take target directory to backup and replace / with _ for backup filename
target_backup_file=$tempdir/${d//[\/]/_}$filename
if $WRITE_CHANGES && $BACKUP_USERFILES ; then
tar zcfP $target_backup_file $d > $logdir/$filename.log
fi;
done
break
done
echo "databases"
if $WRITE_CHANGES && $BACKUP_MYSQL ; then
tar zcfP $tempdir/db.$filename $tempdir/*.sql > $logdir/db.$filename.log
fi;
}
function moveBackup {
printf "Move from temp to Backup Number 0.. ";
if $WRITE_CHANGES ; then
mkdir $backupdir/0/
#mv $tempdir/$filename $backupdir/0/ ;
mv $tempdir/*$filename $backupdir/0/ ;
printf "Ok\n"
else printf "Skipping\n"
fi;
}
function cleanBackup {
printf "Cleaning.. ";
if $WRITE_CHANGES ; then
rm -r -f $tempdir/
printf "Ok\n"
else printf "Skipping\n"
fi;
}
function remoteUnmount {
if $BACKUP_REMOTELY ; then
if $unmountremote ; then
printf "Unmounting remote host"
umount $backupdir
RESULT=$?
if [ $RESULT == 0 ] ; then
printf "Ok.\n"
else
printf "Failed!\n"
exit $RESULT;
fi;
fi;
fi;
}
function startBackup {
if $DISABLED ; then
echo "Skipping backup - script disabled"
exit
else
checkLists
checkBackupStatus
if $WRITE_CHANGES ; then
echo "Starting Backup..."
else
echo "Running in test mode..."
fi;
# step 1: check if and then mount remote backup host or skip
remoteBackup
createTemporaryFolder
# step 1: delete old backups
deleteOldBackups $BACKUP_COPIES
# step 2: shift the middle snapshots(s) back by one, if they exist
shiftBackups
# step 4: dump sql dbs
dumpSQL
# step 5: create new backup
createBackup
# step 6: move to location 0
moveBackup
# step 7: clear temp for the next run
cleanBackup
# step 8: if unmountremote then unmount backupdir
remoteUnmount
fi;
}
# Intro
echo "Copyright(c) 2013 Backup script. - by Ravemaker & ET"
# Load settings
SCRIPTDIRECTORY=$(cd `dirname $0` && pwd)
cd $SCRIPTDIRECTORY
if [ -f /etc/backup.cfg ] ; then
echo "Loading settings..."
source /etc/backup.cfg
elif [ -f settings.cfg ] ; then
echo "Loading settings..."
source settings.cfg
else
echo "ERROR: Create settings.cfg (from settings.cfg.example)"
exit
fi;
# Start backup
startBackup
# Final
echo
if $showfsz ; then
df -h
fi;
echo "All done"