-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbackubash
More file actions
executable file
·201 lines (175 loc) · 4.48 KB
/
backubash
File metadata and controls
executable file
·201 lines (175 loc) · 4.48 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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
#!/bin/bash
version="1.0.1"
### init ###
# error detection
set -e
error() {
echo "ERROR: something went wrong. abandon update."
exit 1
}
trap "error" ERR
# parse args
help() {
echo "Usage backupbash [OPTIONS]"
echo "Releases the current packwiz pack."
echo
echo "Single digit flags have to be passed seperately"
echo " -h, --help print this text and exit."
echo " -v, --version print version text and exit."
echo " -V, --verbose verbose logging."
echo " -c, --conf specify a conf file."
echo " --backup create backup according to conf file"
echo " --cleanup cleanup backupDestination according to conf file"
exit 0
}
confFile="${HOME}/.config/backupbash/config.conf"
doBackup=0
doCleanup=0
verbose=0
while [[ $# -gt 0 ]]; do
case $1 in
-c|--conf)
confFile=$2
shift
shift
;;
--backup)
doBackup=1
shift
;;
--cleanup)
doCleanup=1
shift
;;
-V|--verbose)
verbose=1
shift
;;
-v|--version)
echo "v${version}"
exit 0
shift
;;
-h|--help)
help
shift
;;
*)
echo "Unknown option '$1'"
echo "Try '--help' for help"
exit 1
;;
esac
done
### functions ###
getFile() {
local dir="$1"
local ending=""
if [[ $2 == "new" ]]; then
ending="tail"
elif [[ $2 == "old" ]]; then
ending="head"
else
echo "ERROR: getFile: wring endings arg"
exit 1
fi
latest_file=$(ls "$dir" | grep -E $grepPattern | sort -t '_' -k3,3n | $ending -1)
if [ -n "$latest_file" ]; then
echo "$dir/$latest_file"
else
echo "false"
fi
}
getFileTimestamp() {
file=${1##*/}
#echo $file
if [[ $file =~ ^([0-9]+)_([^_]+)$ ]]; then
echo ${BASH_REMATCH[1]}
else
echo "false"
fi
}
### runtime vars ###
echo "Init"
if [[ -e "$confFile" ]]; then
. "$confFile"
else
echo "ERROR: Conf file not found: $confFile"
exit 1
fi
mkdir -p $backupDestination
grepPattern="^[0-9]+_[^_]+$"
currentTimestamp="$(date +'%s')"
timeString=${currentTimestamp}_"$(date +'%Y-%m-%d-%H-%M-%S%z')"
processBackupPath="${backupDestination}/processing_${timeString}"
finalBackupPath="${backupDestination}/${timeString}"
newestBackup="$(getFile $backupDestination 'new')"
linkDest=""
### prog start ###
if [[ $doBackup == 1 ]]; then
echo "Start creating backups into: $backupDestination"
if [ -d $processBackupPath ]; then
echo "Backup with same name is procesing atm. Please wait a second"
exit 1
elif [ -d $finalBackupPath ]; then
echo "Backup with same name already exists. Please wait a second"
exit 1
fi
createBackup() {
local backupSource=$1
local excludeString=""
local pureBackupSource="${backupSource%%$'\t'*}"
local pureBackupDestination="$(dirname $pureBackupSource)"
echo "Creating backup: $backupSource >> $backupPath"
orgIFS=$IFS
IFS=$'\t' read -r -a array <<< "$backupSource"
echo "Parsed values:"
for value in "${array[@]}"; do
if [[ $value == exclude=* ]]; then
excludeString="${excludeString} --exclude ${value#*=}"
fi
done
IFS=$orgIFS
if [[ $newestBackup != "false" ]] && [[ -d "${newestBackup}/${pureBackupDestination}" ]]; then
linkDest="--link-dest=${newestBackup}/${pureBackupDestination}"
fi
if [[ $verbose == 1 ]]; then
echo "Calling rsync: " "$rsyncArgs" $excludeString "$linkDest" "${pureBackupSource}" "${processBackupPath}/${pureBackupDestination}"
fi
mkdir -p "${processBackupPath}/${pureBackupDestination}"
rsync "$rsyncArgs" $excludeString $linkDest "${pureBackupSource}" "${processBackupPath}/${pureBackupDestination}"
}
for backupSource in "${backupSources[@]}"; do
createBackup "$backupSource"
done
mv $processBackupPath $finalBackupPath
fi
if [[ $doCleanup == 1 ]]; then
echo "Doing cleanup"
if [[ $maxBackups != -1 ]]; then
backupOverflow=$(($(ls $backupDestination | grep -E $grepPattern | wc -l) - $maxBackups))
for ((counter=$backupOverflow; counter>0; counter--)); do
file=$(getFile $backupDestination "old")
if (($(getFileTimestamp $file) < $currentTimestamp - $minBackupAge)); then
echo "Removing excess file: $file"
rm -r $file
else
echo "Skipping removal of exess files because minBackupAge is not reached"
break
fi
done
fi
if [[ $maxBackupAge != -1 ]]; then
while true; do
file=$(getFile $backupDestination "old")
fileTimestamp=$(getFileTimestamp $file)
if [[ $file != "false" ]] && (( $fileTimestamp < $currentTimestamp - $maxBackupAge )); then
echo "Removing old file: $file"
rm -r $file
else
break
fi
done
fi
fi
echo "Done"