-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrotate-backups
More file actions
executable file
·53 lines (46 loc) · 1.78 KB
/
Copy pathrotate-backups
File metadata and controls
executable file
·53 lines (46 loc) · 1.78 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
#!/bin/bash
BACKUP=${1:-'/media/1.5tb/arch-backup'}
COUNT=${2:-'3'}
LOG="/var/log/backup.log"
log () { echo -e "$(date "+%Y/%m/%d %T") $1" | tee --append "$LOG"; }
backup-name () { echo "$BACKUP.$1"; }
OLDEST_BACKUP=$(backup-name 1)
for i in $(seq 1 "$COUNT"); do
CURRENT="$(backup-name "$i")"
if [ ! -d "$CURRENT" ]; then
log "No rotated backup number $i found, creating it"
mkdir "$CURRENT"
# mark the new directory as "oldest" so that the current backup is rotated there
OLDEST_BACKUP=$CURRENT
break
else
if [ "$CURRENT" -ot "$OLDEST_BACKUP" ]; then
OLDEST_BACKUP=$CURRENT
fi
fi
done
log "########## Rotating current backup to: $OLDEST_BACKUP... ##########"
VERBOSE='-v'
ARCHIVE='-a' # -r (recursive), -l (preserve symlinks), -p (preserve permissions), -t (), -g (preserve group), -o (preserve owner), -D (presere devices and special files)
TIME='-t' # (preserve modification time)
# PREALLOC='--preallocate'
# DRYRUN='-n'
APPEND='--append'
# WHOLEFILE='-W'
DELETE='--delete --force' # delete extra files on dest and force delete non-empty dirs
COMPRESS='-z'
SPACES='-s' # no space splitting in paths
STATS='--stats -h' # stats which are human readable
PROGRESS='--progress'
LOGCMD="--log-file=$LOG"
# TMPDIR="--temp-dir=/tmp/rsync"
# EXCLUDECMD=$(join '⋄' "${EXCLUDE[@]}") # assuming ⋄ isn't part of any file name...
# EXCLUDECMD=$EX${EXCLUDECMD//⋄/ $EX}
# ICONV='--iconv=utf8,iso88591'
OPTIONS="$ARCHIVE $APPEND $TIME $DELETE $COMPRESS $SPACES $PREALLOC $TMPDIR $VERBOSE $STATS $PROGRESS $LOGCMD"
# shellcheck disable=SC2086
rsync $OPTIONS $BACKUP/ $OLDEST_BACKUP
touch "$OLDEST_BACKUP"
date > "$OLDEST_BACKUP/backup-date"
DURATION=$SECONDS
log "########## ... finished rotating backup! $((DURATION / 60)) minutes and $((DURATION % 60)) seconds elapsed. ##########"