This repository has been archived by the owner on Apr 28, 2018. It is now read-only.
/
includes.sh
executable file
·132 lines (116 loc) · 3.31 KB
/
includes.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
#!/bin/bash
function set_lock() {
(set -C; : > ${1} ) 2> /dev/null
if [ ${?} != "0" ]; then
echo "Lock File exists - exiting"
exit 1
fi
}
function cleanup() {
if [[ -f "${LOCK_FILE}" ]]; then
rm "${LOCK_FILE}"
fi
if [[ -f "${ERROR_LOG}" ]]; then
rm "${ERROR_LOG}"
fi
if [ "${ERROR}" -gt 0 ]; then
if [[ ${NOTIFY_FAILURE_EMAIL:-1} -eq 1 ]]; then
echo -e "${TYPE} failed:\n${ERROR_MSG}\n\n\n${MSG}" | mailx -s "[CRON]: ${TYPE} of ${HOSTNAME} failed!" "${EMAIL}"
fi
if [[ ${NOTIFY_FAILURE_POPUP:-1} -eq 1 ]]; then
notify_users_error
fi
else
if [[ ${NOTIFY_COMPLETE_EMAIL:-1} -eq 1 ]]; then
echo -e "${TYPE} completed:${MSG}" | mailx -s "[CRON]: ${TYPE} of ${HOSTNAME} completed" "${EMAIL}"
fi
if [[ ${NOTIFY_COMPLETE_POPUP:-1} -eq 1 ]]; then
notify_users_done
fi
fi
unset PASSPHRASE
unset GOOGLE_DRIVE_SETTINGS
unset ENCRYPT_KEY
unset COMMON_OPTS
unset BACKUP_OPTS
unset EXCLUDE_OPTS
unset DIR_EXCLUDE
unset REMOTE_DIR
unset BACKDIRS
unset EMAIL
unset TYPE
}
function err_report() {
local LASTLINE
local LASTERR
LASTLINE="${1}"
LASTERR="${2}"
ERROR_MSG=$(<"${ERROR_LOG}")
ERROR_MSG="${ERROR_MSG}\nLine ${LASTLINE}: exit status of last command: ${LASTERR}"
ERROR=1
exit "${LASTERR}"
}
# Setup handlers for errors and exit
# NOTE: error handling in bash is... interesting :)
function setup() {
set_lock "${LOCK_FILE}"
trap cleanup EXIT
trap 'err_report ${LINENO} ${?}' ERR
if [[ ! -d /var/backup ]]; then
mkdir /var/backup
fi
if [[ ! -d /var/log/backup ]]; then
mkdir /var/log/backup
fi
notify_users_start
}
function dump_disk_info() {
rm -f /var/backup/partition_info.txt
rm -f /var/backup/fs_info.txt
touch /var/backup/partition_info.txt
touch /var/backup/fs_info.txt
touch /var/backup/raid_info.txt
# TODO:
# Only match ext3 partitions
# Breaks if vfat, should also match /dev/md*
# for partition in $(blkid | grep -E '/dev/sd.*TYPE="ext4"' | cut -f1 -d":"); do
# if [[ ${partition} != 'swap' ]]; then
# dumpe2fs -h ${partition} >> /var/backup/fs_info.txt 2>/dev/null
# fi
# done
# Dump partition info, needs name of disks else it will fail. Append "|| true" to ignore
# Requires sfdisk
if command -v "sfdisk" &>/dev/null; then
for DISK in $(lsblk -ndro KNAME,TYPE | awk '$2 ~ /disk/ { print $1 }'); do
sfdisk --dump /dev/${DISK} >> /var/backup/partition_info.txt
done
else
echo "No sfdisk installed"
fi
# Dump raid meta data
if command -v "mdadm" &>/dev/null; then
# Ignore return value greater than 0
mdadm -Evvvvs >> /var/backup/raid_info.txt || true
fi
}
# Arch Linux package list
function dump_package_list() {
if [ -f /etc/arch-release ]; then
pacman -Q | gzip > /var/backup/packages.txt.gz
fi
}
function notify_users_start() {
if [ -e /usr/bin/notify-send ]; then
/usr/local/sbin/user-notification.sh "emblem-generic" "Duplicity backup" "... Backup is starting"
fi
}
function notify_users_done() {
if [ -e /usr/bin/notify-send ]; then
/usr/local/sbin/user-notification.sh "emblem-generic" "Duplicity backup" "... Backup completed"
fi
}
function notify_users_error() {
if [ -e /usr/bin/notify-send ]; then
/usr/local/sbin/user-notification.sh "emblem-urgent" "Duplicity backup" "... Backup failed!"
fi
}