-
Notifications
You must be signed in to change notification settings - Fork 0
/
arcman.sh
executable file
·138 lines (127 loc) · 4.14 KB
/
arcman.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
#!/bin/bash
########## sqldump.sh ##########################################################
#
# Author: Daniel Huckson Filename: sqldump.sh
#
# Version 0.0.1 Date: 2012/03/28
#
# Purpose:
# Export and archive MySQL database.
#
################################################################################
#
purge() {
retval=0
saved_pwd=`pwd`
[ "$1" == "" ] && retval=1 || {
[ "$2" == "" ] && retval=2 || {
[ ! `expr $2 + 1 2> /dev/null` ] && retval=3 || {
[ ! -d "$1" ] && retval=4 || {
i=0;
cd $1
for f in `ls -r *`; do
[ -f "$f" ] && {
i=$(( i + 1 ))
[[ $i -gt `expr $2` ]] && {
rm -f $f
[ $? != '0' ] && {
retval=5
break
}
}
}
done
}
}
}
}
[[ $retval -ne 0 ]] && {
case "$retval" in
1) echo "Error:$retval, Directory name must not be null." ;;
2) echo "Error:$retval, You need to indicate the number of files to keep." ;;
3) echo "Error:$retval, You must supply an interger value." ;;
4) echo "Error:$retval, Could not locate directory \"$1\"." ;;
5) echo "Error:$retval, Could not remove file \"$f\"." ;;
*) echo "Error:$retval, Unknown error type." ;;
esac
}
cd $saved_pwd
return $retval
}
start_time=$(date +%s)
index=0
options=$@
arguments=($options)
for argument in $options; do
index=`expr $index + 1`
case $argument in
-u) user="${arguments[index]}" ;;
-p) password="${arguments[index]}" ;;
-d) database="${arguments[index]}" ;;
-f) filename="${arguments[index]}" ;;
-h) host="${arguments[index]}" ;;
-r) root="${arguments[index]}" ;;
-m) max_archive_files="${arguments[index]}" ;;
esac
done
retval=0
pwd=`pwd`
filename=$filename"_`date +%s`"
[ ! -d "$root/mysql-backup/$database" ] && {
mkdir -p "$root/mysql-backup/$database/"{current,archive}
[[ $? -ne 0 ]] && retval=1
}
[[ $retval -eq 0 ]] && {
echo;date
echo "Starting backup process for database ($database)."
cd "$root/mysql-backup/$database/"
echo -n "Exporting database \"$database\"."
mysqldump -q -h $host -u $user --password=$password $database > $filename.sql
[[ $? -ne 0 ]] && retval=2 || {
echo "..(Success!) File size: `stat --printf='%s' $filename.sql | sed -r ':L;s=\b([0-9]+)([0-9]{3})\b=\1,\2=g;t L'` Bytes"
[ "$(ls -A current)" ] && current_backup=`ls -r current/*_sql.tar.bz2 | tail -n 1`
echo -n "Compressing exported database file."
tar -cpjf current/$filename\_sql.tar.bz2 $filename.sql
[[ $? -ne 0 ]] && retval=3 || {
echo "..(Success!), File size: `stat --printf='%s' current/$filename\_sql.tar.bz2 | sed -r ':L;s=\b([0-9]+)([0-9]{3})\b=\1,\2=g;t L'` Bytes"
chmod 600 current/$filename\_sql.tar.bz2
echo -n "Removing temporary uncompressed export file."
rm -f $filename.sql
[[ $? -ne 0 ]] && retval=4 || {
echo "..(Success!)"
[[ `ls current | wc -l` -gt 1 ]] && {
[ "$current_backup" != "" ] && {
echo -n "Archiving previously exported file."
mv $current_backup archive/
[[ $? -ne 0 ]] && retval=5 || {
echo "..(Success!)"
echo -n "Cleaning up, removing old archive files."
purge archive $max_archive_files
[[ $? -ne 0 ]] && retval=6 || {
echo "..(Success!)"
}
}
}
}
}
}
}
}
case "$retval" in
0)
echo;echo "Backup finished successfully.";
s=$[$(date +%s) - $start_time]; h=$[$s / 3600]; s=$[$s - $[$h * 3600]]; m=$[$s / 60]; s=$[$s - $[m * 60]]
[ "$h" != '0' ] && hours=" $h hours" || hours=""
[ "$m" != '0' ] && minutes=" $m minutes and" || minutes=""
echo "Backup time$hours$minutes $s seconds."
echo "----------------------------------------------------------------------"
;;
1) echo "..(Failed!)";echo "Error:$retval, Could not create directory \"$root/mysql-backup\"." ;;
2) echo "..(Failed!)";echo "Error:$retval, Could not export database \"$database\"." ;;
3) echo "..(Failed!)";echo "Error:$retval, Could not compress database export file." ;;
4) echo "..(Failed!)";echo "Error:$retval, Could not remove file \"$filename\"." ;;
5) echo "..(Failed!)";echo "Error:$retval, Could not move old archive file, `pwd`$current_backup to directory `pwd`/archive" ;;
esac
cd "$pwd"
echo $retval > .results.log
#