-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstore_backup
executable file
·57 lines (45 loc) · 1.47 KB
/
store_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
#!/bin/bash
set -e
function info() { echo "$@" >&2; }
function error() { echo "ERROR: $@" >&2; exit 1; }
# read global config file
conffile=$(cd $(dirname $0)/.. && pwd -P)/etc/backup.conf
[[ ! -r $conffile ]] && error "Unable to read configuration file $conffile"
. $conffile
# check mandatory options
[[ -z "$BACKUP_HOSTS_DIR" ]] && error "Invalid backup dir for hosts"
# parse arguments
host=$1
[[ -z "$host" ]] && error "Invalid hostname"
bakname=$2
[[ -z "$bakname" ]] && error "Invalid backup name"
# load host-specific config if any
[[ -f $BACKUP_HOSTS_DIR/$host/backup.conf ]] && . $BACKUP_HOSTS_DIR/$host/backup.conf
# move to backup dir
mkdir -p $BACKUP_HOSTS_DIR
cd $BACKUP_HOSTS_DIR
# create per-host directory
mkdir -p $host || error "Unable to create backup dir for $host"
# save transmitted file
outfile=$host/$(date +%Y%m%d_%H%M%S)_${bakname}
info "Saving backup on $(hostname -f):$(pwd -P)/$outfile"
if [[ -n $(which pv) ]]; then
$(which pv) -trafpb >$outfile
elif [[ -n $(which buffer) ]]; then
time $(which buffer) -z 256K -o $outfile
else
cat >$outfile
fi
info "Done"
# remove old backups
if [[ "$BACKUP_MAX_AGE" -gt 0 ]]; then
for x in $(find $host -type f -mtime +$BACKUP_MAX_AGE); do
info "Removing old backup $x"
rm $x
done
fi
# push last backup to secondary if defined
if [[ -n "$BACKUP_SECONDARY_SERVER" ]]; then
info "Sending backup to seconday server $BACKUP_SECONDARY_SERVER"
cat $outfile | ssh $BACKUP_SECONDARY_SERVER store_backup $host $bakname
fi