-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcopy-last-backup.sh
executable file
·56 lines (42 loc) · 1.17 KB
/
copy-last-backup.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
#!/bin/bash
#
# Copy a backup from one folder to another.
#
# Author: Francisco Troncoso
### Functions
# Lists the contents of a directory, ordered alphabetically
lsdir() {
find "$1" -mindepth 1 -maxdepth 1 -type d | sort
}
# Returns the number of directories in a given directory
lsdircount() {
local list=$(lsdir "$1")
if [[ ! -z "${list}" ]]; then
echo "${list}" | wc -l
else
echo "0"
fi
}
### Command line arguments
if [[ "$#" -ne 3 ]]; then
echo "Usage: $0 <source_folder> <dest_folder> <max_backups_dest>" >&2
exit 1
fi
# Source folder
readonly SRC_FOLDER="$(realpath "$1")"
# Destination backups folder
readonly DST_FOLDER="$(realpath "$2")"
# Maximum number of simultaneous backups
readonly MAX_BUS="$3"
### MAIN
# *** Copy backup to destination folder
src_bu="$(echo "$(lsdir "${SRC_FOLDER}")" | tail -n 1)"
mkdir -p "${DST_FOLDER}"
cp -al --remove-destination "${src_bu}" "${DST_FOLDER}"
# *** Remove old backups in destination folder
bu_count="$(lsdircount "${DST_FOLDER}")"
if [ "${bu_count}" -gt "${MAX_BUS}" ]; then
n="$((${bu_count} - ${MAX_BUS}))"
bus="$(echo "$(lsdir "${BUS_FOLDER}")" | head -n "${n}")"
echo "${bus}" | xargs rm -r
fi