-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbackup
executable file
·173 lines (146 loc) · 2.67 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
#!/bin/bash
##############################
#
# Backup script
#
##############################
#
# Usage and parse options
#
usage() {
echo "Usage: $0 [-n] [-f configfile]" 1>&2
exit 1
}
while getopts f:nh OPT
do
case $OPT in
f) cfgfile=$OPTARG
;;
n) DRY_RUN=1
echo "Dry run!"
export DRY_RUN
;;
h) usage
;;
\?) usage
;;
esac
done
shift $((OPTIND - 1))
#
# Move basedir
#
BASEDIR=${0%/*}
BASEDIR=$(cd $BASEDIR && pwd)
#
# Load config file and setup env
#
if [ "$cfgfile" == "" ]; then
cfgfile=$BASEDIR/backup.conf
fi
if [ ! -f $cfgfile ]; then
echo "Config file not found!"
exit 1
fi
. $cfgfile
#
# Buckets
#
if [ "$Buckets" == "" ]; then
echo "Buckets not defined!"
exit 1
fi
#
# Setup backup temp dir
#
if [ -z "${BackupTmpDir+x}" ]; then
echo "Backup temp dir not defined!"
exit 1
fi
bkupdir=$BackupTmpDir
if [[ ! "$BackupTmpDir" =~ te?mp$ ]]; then
echo "Backup temp dirname must end with 'temp' or 'tmp'!"
exit 1
fi
if [ -d $bkupdir ]; then
# Remove temporary archives
rm -rf $bkupdir/*
else
# Make temporary directory
mkdir $bkupdir
fi
if [ $? -ne 0 ]; then
exit 1
fi
backupdir="$DestDir"
buckets=(`echo $Buckets | tr -s ',' ' '`)
epochsec=`date +"%s"`
epochday=$((epochsec / 86400))
dayidx=$((epochday % MaxAge))
bknum=${#buckets[@]}
bkidx=$((epochday % bknum))
bucket=${buckets[$bkidx]}
#
# Logging
#
if [ -z "${LogDir+x}" ]; then
logfile=/dev/null
else
# Delete old logfiles
find $LogDir -mtime +$MaxLogAge -name "*.log.*" | xargs rm -f
logfile=$LogDir/backup.log.`date +\%Y\%m\%d\%H\%M`
fi
#
# Start
#
echo "Day Index: $dayidx" | tee $logfile
echo "Bucket: $bucket" | tee -a $logfile
echo "Host: `hostname`" | tee -a $logfile
echo "Backup start at `date`." | tee -a $logfile
#
# Backup scripts
#
BKUP_PREFIX=$bkupdir/$dayidx-
cd $BASEDIR
tar zcf ${BKUP_PREFIX}backup-scripts.tgz *
BKUP_DIR=$bkupdir
export BKUP_DIR
export BKUP_PREFIX
if [ "$BackupCmdDir" == "" ]; then
BackupCmdDir=$BASEDIR/bkcmd.d
fi
cd $BackupCmdDir
for cmd in `ls S*`
do
./$cmd >>$logfile 2>&1
if [ $? -eq 0 ]; then
echo -e "[OK] $cmd" | tee -a $logfile
else
echo -e "[NG] $cmd" | tee -a $logfile
fi
done
#
# Syncing to the cloud
#
echo "Start to synchronize files to cloud." | tee -a $logfile
if [ "$Sync" == "" ]; then
Sync=$BASEDIR/sync
fi
$Sync $bkupdir $bucket $backupdir
if [ $? -eq 0 ]; then
result=0
echo -e "[OK] Sync" | tee -a $logfile
else
result=1
echo -e "[NG] Sync" | tee -a $logfile
fi
#
# Exit
#
if [ $result -eq 0 ]; then
echo "Backup done at `date`." | tee -a $logfile
exit 0
else
echo "Backup failed at `date`." | tee -a $logfile
exit 1
fi