-
Notifications
You must be signed in to change notification settings - Fork 0
/
backup
executable file
·91 lines (70 loc) · 1.72 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
#!/bin/bash
source $HOME/.backups
mktimestamp() {
date "+%Y-%m-%d-%H-%M"
}
mktar() {
local full_path="$1"
local extra_name="$2"
local top_dir=$(basename $full_path)
local file="/tmp/${top_dir}${extra_name}.tar.xz"
tar caf $file $full_path 2>/dev/null
echo $file
}
rsync_backup() {
local local_host=$(hostname)
local backup_location=$RSYNC_BACKUP_DEST
local host=${backup_location%:*}
local dir=${backup_location#*:}
local dirs="$RSYNC_BACKUP_DIRS"
local new="$local_host/$(date +%Y-%m)/$(date +%Y.%m.%d.%H.%M.%S)"
echo "Backing up $dirs to ${backup_location}${new}..."
ssh $host -- mkdir -p ${new}
rsync --archive --one-file-system --human-readable --progress \
--numeric-ids --delete --delete-excluded \
--hard-links --links --safe-links \
--exclude-from=$RSYNC_EXCLUDE \
--include-from=$RSYNC_INCLUDE \
--link-dest=../../last \
${dirs} ${backup_location}${new} || exit
ssh $host ln -sfn ${new#*/} ${dir}${local_host}/last
}
scp_backup() {
for dir in "${TAR_BACKUP_DIRS[@]}"; do
echo "Backing up $dir to ${TAR_BACKUP_DEST}..."
file=$(mktar $dir)
scp $file $TAR_BACKUP_DEST
rm -f $file
done
}
email_backup() {
local files=""
local dirs="${EMAIL_BACKUP_DIRS[@]}"
for dir in $dirs; do
file=$(mktar $dir -$(mktimestamp))
files="${files} $file"
done
echo "Backing up $dirs to ${EMAIL_BACKUP_DEST}..."
if which mutt >/dev/null; then
echo "Backup of $dirs" | mutt -s "Backup for $(hostname)" $EMAIL_BACKUP_DEST -a $files
else
echo "Mutt is not installed, backup failed."
fi
rm -f $files
}
for backup in $*; do
case $backup in
rsync)
rsync_backup
;;
scp)
scp_backup
;;
email)
email_backup
;;
*)
echo "Backup wat?"
;;
esac
done