-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbackup.sh
More file actions
executable file
·128 lines (105 loc) · 2.2 KB
/
backup.sh
File metadata and controls
executable file
·128 lines (105 loc) · 2.2 KB
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
#!/bin/bash
set -eu
FULL=0
PURGE=0
DUMMY=''
TARGET=''
NOW=`date +%s`
NAME="`uname -n`"
check_fs() {
local v=`grep -c " ${1} " /proc/mounts`
[ $v = "1" ] && return 0 || return 1
}
get_level()
{
local -r db='/var/lib/dumpdates'
grep "^${1} " "${db}" | while read disk level dy mn dt tm yr tz
do
echo -ne "$disk\t$level\t"
date -d "$dy $mn $dt $yr $tm $tz" +%s
done | sort -t $'\t' -k 3 -nr | head -n1 | cut -d $'\t' -f 2
}
get_dev()
{
local -r db='/etc/mtab'
grep "^/.* ${1} " "${db}" | awk '{print $1}'
}
next_level()
{
local d=`get_dev "${1}"`
local -i l=`get_level "${d}"`
echo $((${l} + 1))
}
backup_system()
{
local options=''
local -a sources=('/' '/home')
local d="${TARGET}/system/`date +%m`"
mkdir -p "$d" || return 1
for fs in ${sources[@]}
do
name=${fs##*/}
if [ $FULL -eq 1 ]; then
level=0
else
level=`next_level "${fs}"`
fi
if [ "x$name" = "x" ]; then
name='root'
fi
$DUMMY dump -${level} -uj -v \
-A "${d}/dump-${name}-archive-${level}.log" \
-f "${d}/dump-${name}-db-${level}.log" "${fs}"
done
}
backup_data()
{
local options=''
local d="${TARGET}/data"
local -a sources=('/mnt/video' '/mnt/music' '/mnt/photos')
mkdir -p "$d" || return 1
[ $PURGE -eq 1 ] && options='--delete-during'
for fs in ${sources[@]}
do
name=${fs##*/}
if [ "x$name" = "x" ]; then
name='root'
fi
$DUMMY rsync -amxh --stats "$options" \
"${fs}/" "${d}/${name}/"
done
}
while getopts 'dpfh' OPTION; do
case $OPTION in
d)
DUMMY=echo
;;
p)
PURGE=1
;;
f)
FULL=1
;;
h)
echo -e "Usage: $0 [options] <device>\nOptions:"
echo -e "\t-h\tHelp! Print this message then exit"
echo -e "\t-d\tDummy mode - print, don't execute"
echo -e "\t-p\tPurge (from the target) as we go"
echo -e "\t-f\tPerform a full system backup (level 0 dump)"
exit 0
;;
?)
exit 1
;;
esac
done
shift $(($OPTIND - 1))
if [ -z "$1" ]; then
echo "$0: Required device path not provided; try -h" >&2
exit 1
fi
TARGET="${1}/$NAME"
check_fs "${1}" || exit 1
mkdir -p "${TARGET}/logs" || exit 1
backup_data >> "${TARGET}/logs/${NOW}.log" 2>&1
backup_system >> "${TARGET}/logs/${NOW}.log" 2>&1