-
Notifications
You must be signed in to change notification settings - Fork 0
/
host_backup.sh
executable file
·145 lines (125 loc) · 4.1 KB
/
host_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
#!/bin/bash
# For each nova node, make backup of
# /etc/ (is in /root/git-repos)
# /var/lib/? - mysql DB, LDAP
# modified nova files - symlinks in /root/git-repos/
# set -x
SRCDIR=`readlink -f $0`
SRCDIR=`dirname $SRCDIR`
source $SRCDIR/settings.sh
source $SRCDIR/local_settings.sh
DESTD=$HOST_BACKUP_DEST/`hostname`
RSYNC="/usr/bin/rsync -rav --delete"
TAR="/bin/tar"
MYSQLDUMP="/usr/bin/mysqldump"
MYSQL="/usr/bin/mysql"
SLAPCAT="/usr/sbin/slapcat"
MYSQL_EXCLUDE_DB="Database information_schema performance_schema"
function backup_git_repos() {
echo "-------------------------------------------------------------"
/bin/ls -la /root/git-repos/ > $DESTD/log/git-repos-list.$DATE.txt
all_repos=`find /root/git-repos/ -type l -or -type d`
all_repos=`echo $all_repos`
for ff in $all_repos
do
if [ "$ff" == "/root/git-repos/" ]
then
continue
fi
gitdir=`readlink -f $ff` # ff=/root/git-repos/ttrt -> gitdir=/home/user/ttrt-dir
gitdir_dirname=`dirname $gitdir` # ttrt-dir
ff_basename=`basename $ff` # ttrt
echo "Backing up gitdir/ $gitdir -> $DESTD/day/git-repos/$ff_basename/"
CMD="$RSYNC $gitdir/ $DESTD/day/git-repos/$ff_basename/"
echo EXEC $CMD
$CMD
if [ $DAY_OF_WEEK == 7 ]
then
echo "Weekly backup $gitdir -> $DESTD/week/git-repos/$ff_basename.$DATE.tar.gz"
# get file ttrt.DATE.tar.gz, with structure like ./ttrt-dir/file1 etc
CMD="$TAR -czf $DESTD/week/git-repos/$ff_basename.$DATE.tar.gz -C $gitdir_dirname $gitdir"
echo EXEC $CMD
$CMD
fi
done
}
function backup_mysql() {
echo "-------------------------------------------------------------"
if [ -z "`which mysql`" ]
then
echo "INFO: mysql binary not found, so likely there is no DB to backup."
return
fi
db_all=`$MYSQL -uroot -p$MYSQL_ROOT_PASS -e 'show databases;' | sed 's/asdfasdfasdf//'` # multiline
#echo "DEBUG: db_all AA $db_all"
for db_exc in $MYSQL_EXCLUDE_DB
do
db_all=`echo "$db_all" | sed "/^$db_exc$/d"`
done
db_all=`echo $db_all` # to single line
echo "DEBUG: db_all = $db_all"
for db in $db_all
do
echo "Backing up mysql DB $db - > $DESTD/day/mysql/$db.$DATE.sql.gz"
CMD="$MYSQLDUMP -uroot -p$MYSQL_ROOT_PASS $db | gzip -9 > $DESTD/day/mysql/$db.$DATE.sql.gz"
echo EXEC $CMD
/bin/bash -c "$CMD"
if [ $DAY_OF_WEEK == 7 ]
then
echo "Weekly backup $DESTD/week/mysql/$db.$DATE.sql.gz"
CMD="/bin/cp $DESTD/day/mysql/$db.$DATE.sql.gz $DESTD/week/mysql/$db.$DATE.sql.gz"
echo EXEC $CMD
$CMD
fi
done
}
function backup_ldap() {
echo "-------------------------------------------------------------"
if [ ! -d /etc/ldap/slapd.d/ ]
then
echo "INFO: no /etc/ldap/slapd.d dir, so no slapd here."
return
fi
OUTDIR_DAY=$DESTD/day/ldap
OUTDIR_WEEK=$DESTD/week/ldap
[ ! -d $OUTDIR_DAY ] && mkdir $OUTDIR_DAY
[ ! -d $OUTDIR_WEEK ] && mkdir $OUTDIR_WEEK
# LDAP_BASE='dc=example,dc=com'
ldap_postfix=`echo $LDAP_BASE | sed -e 's/dc=//g' -e 's/,/-/g'`
# backup DIT data, DIT config and filesystem config (/etc/ldap/)
DIT_DATA="$OUTDIR_DAY/`hostname`-$DATE-${ldap_postfix}.dit-data.ldif"
DIT_CONFIG="$OUTDIR_DAY/`hostname`-$DATE-${ldap_postfix}.dit-config.ldif"
FS_CONFIG="$OUTDIR_DAY/`hostname`-$DATE-${ldap_postfix}.fs-config.tar.gz"
echo " DIT_DATA=$DIT_DATA"
echo " DIT_CONFIG=$DIT_CONFIG"
echo " FS_CONFIG=$FS_CONFIG"
CMD="$SLAPCAT -b $LDAP_BASE -l $DIT_DATA"
echo EXEC $CMD
$CMD
CMD="$SLAPCAT -b cn=config -l $DIT_CONFIG"
echo EXEC $CMD
$CMD
CMD="$TAR -czf $FS_CONFIG /etc/ldap/ /etc/ldapscripts"
echo EXEC $CMD
$CMD
}
function main() {
echo "Backup start `date`"
#[ -d $DESTD ] || mkdir $DESTD
#[ -d $DESTD/log ] || mkdir $DESTD/log
for subd in day week day/git-repos week/git-repos day/mysql week/mysql
do
[ -d $DESTD/$subd ] || mkdir $DESTD/$subd
done
backup_git_repos
backup_mysql
backup_ldap
echo "Backup stop `date`"
}
DATE=`date +%Y%m%d-%H%M%S`
DAY_OF_WEEK=`date +%u` # 7==sunday, weekly backup
## DAY_OF_WEEK=7
[ -d $DESTD ] || mkdir $DESTD
[ -d $DESTD/log ] || mkdir $DESTD/log
main 1>>$DESTD/log/host_backup.$DATE.log 2>&1
##