-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbak-gen.sh
119 lines (104 loc) · 2.98 KB
/
bak-gen.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
#!/bin/sh
# Description: generates backup archives of a precompiled list of directories and files
# Author: Jørgen Bele Reinfjell
# Date: 19.09.2017 [dd.mm.yyyy]
# Modified: 04.11.2018 [dd.mm.yyyy]
# File: bak-gen.sh
[ -z "$HOSTNAME" ] && HOSTNAME="$(hostname)"
fextr=".tar.xz"
uextr="$(date +%Y_%m_%d_%s)"
fname="backup_${HOSTNAME}_${uextr}${fextr}"
#compr="tar -acJv -T - -f $fname --"
tar_cmd="tar -acv -T - --"
xz_cmd="xz -z -T 0 - "
# Newline separated list of dirs (the '/' postfix are needed)
DIRS="$HOME/src/
$HOME/bin/
$HOME/conf
$HOME/books
$HOME/pdfs
$HOME/usr/docs/
$HOME/usr/vpn/
$HOME/.ecryptfs/
$HOME/usr/.gpg
$HOME/.emacs.d/
$HOME/.zsh
$HOME/.vim/
$HOME/.xinitrc.d/
$HOME/.mpd/
$HOME/.config/
"
# Newline separated list of files
FILES="$HOME/.bashrc
$HOME/.bash_profile
$HOME/.zshrc
$HOME/.shrc
$HOME/.astylerc
$HOME/.profile
$HOME/.editrc
$HOME/.inputrc
$HOME/.emacs
$HOME/.spacemacs
$HOME/.vimrc
$HOME/.xinitrc
$HOME/.Xresources
$HOME/.Xdefaults
$HOME/.xbindkeys
$HOME/.tmux.conf
$HOME/note
"
if [ "$INTERACTIVE" ]; then
tfile="$(mktemp)"
echo "tfile=$tfile"
[ -z "$tfile" ] && echo "mktemp failed!" 1>&2 && exit 1
# Convert DIRS and FILES to nul-terminated strings, store in temp file (for use by du)
# Source: https://unix.stackexchange.com/questions/102891/posix-compliant-way-to-work-with-a-list-of-filenames-possibly-with-whitespace
set -f; IFS='
' # turn off variable value expansion except for splitting at newlines
for path in $FILES $DIRS; do
set +f; unset IFS # restore globbing and field splitting at all whitespace
printf "%s\0" "$path" >> "$tfile" # write null terminated paths to temp file
done
set +f; unset IFS # restore globbing again in case $INPUT was empty
# Is interactive
# Run sorted 'du' on the files, this makes it easier to determine if there is large unwanted files.
printf "List files: [y/n]: "
case "$inp" in
"N*"|"n*") ;;
"Y*"|"y*"|*)
# TODO: change out --files0-from with something else
du -hac --files0-from="$tfile" | sort -uh ;;
esac
rm "$tfile" # remove temp file
printf "Continue backup [yes/no]: "
read -r inp
case "$inp" in
"yes") ;;
"no"|*) printf "Exiting.."; exit 0 ;;
esac
fi
## Time
# tsec(): time in seconds since UNIX time epoch)
tsec() {
date +"%s"
}
# difftime(time): converts the time difference between time $1 and now
difftime() {
local time="$1"
local now="$(tsec)"
echo $(($now - $time))
}
# stohms(seconds): convert seconds to hh:mm:ss
stohms() {
local ts="$1"
local h="$(($ts/3600))"
local m="$((($ts%3600)/60))"
local s="$(($ts%60))"
printf "%02d:%02d:%02d" "$h" "$m" "$s"
}
# Do backup
echo "$(date +%H:%M:%S) Starting: '[DIRS] [FILES] | $compr'"
stime="$(tsec)"
echo "$FILES" "$DIRS" | $tar_cmd | $xz_cmd > "$fname"
difft="$(difftime $stime)"
echo "$(date +%H:%M:%S) Finished! Took $difft seconds ($(stohms $difft))."