-
Notifications
You must be signed in to change notification settings - Fork 0
/
backup.sh
executable file
·64 lines (49 loc) · 2.06 KB
/
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
57
58
59
60
61
62
63
64
#!/usr/bin/env bash
if [ "$#" -lt 2 ]; then
echo "Usage: $0 source target [rsync options - e.g. --exclude]"
exit 1
fi
SOURCE=$1
TARGET=$2
echo "Starting backup from $SOURCE to $TARGET using rsync."
rsync --archive "${@:3}" --human-readable --progress --link-dest "../current" "$SOURCE/" "$TARGET/incomplete"
rsync_exit_code=$?
if [ "$rsync_exit_code" -ne "0" ] && [ "$rsync_exit_code" -ne "23" ] && [ "$rsync_exit_code" -ne "24" ]; then
echo "Backup incomplete - rsync exited with code $rsync_exit_code"
exit $rsync_exit_code
fi
echo "Transfer completed. Now moving 'incomplete' to 'current'."
rm -r "$TARGET/current"
mv "$TARGET/incomplete" "$TARGET/current"
# Create hourly, daily and monthly backup copies as required.
HOURLY_NAME="$(date +%Y-%m-%d_%Hh)" # 2022-10-11_16h
DAILY_NAME="$(date +%Y-%m-%d)" # 2022-10-11
MONTHLY_NAME="$(date +%Y-%m)" # 2022-10
if [ ! -e "$TARGET/$HOURLY_NAME" ]; then
echo "Creating hourly backup copy '$HOURLY_NAME'."
cp -al "$TARGET/current" "$TARGET/$HOURLY_NAME"
fi
if [ ! -e "$TARGET/$DAILY_NAME" ]; then
echo "Creating daily backup copy '$DAILY_NAME'."
cp -al "$TARGET/current" "$TARGET/$DAILY_NAME"
fi
if [ ! -e "$TARGET/$MONTHLY_NAME" ]; then
echo "Creating monthly backup copy '$MONTHLY_NAME'."
cp -al "$TARGET/current" "$TARGET/$MONTHLY_NAME"
fi
# Delete old hourly and daily backups beyond defined limit.
MAX_HOURLY_BACKUPS=12
MAX_DAILY_BACKUPS=30
HOURLY_BACKUPS=("$TARGET"/????-??-??_??h)
DAILY_BACKUPS=("$TARGET"/????-??-??)
if [ ${#HOURLY_BACKUPS[@]} -gt $MAX_HOURLY_BACKUPS ]; then
old_HOURLY_BACKUPS=("${HOURLY_BACKUPS[@]::${#HOURLY_BACKUPS[@]}-$MAX_HOURLY_BACKUPS}")
echo "Removing ${#old_HOURLY_BACKUPS[@]} old hourly backups: " "${old_HOURLY_BACKUPS[@]}"
rm -r "${old_HOURLY_BACKUPS[@]}"
fi
if [ ${#DAILY_BACKUPS[@]} -gt $MAX_DAILY_BACKUPS ]; then
old_DAILY_BACKUPS=("${DAILY_BACKUPS[@]::${#DAILY_BACKUPS[@]}-$MAX_DAILY_BACKUPS}")
echo "Removing ${#old_DAILY_BACKUPS[@]} old daily backups: " "${old_DAILY_BACKUPS[@]}"
rm -r "${old_DAILY_BACKUPS[@]}"
fi
echo "Done."