Skip to content

Commit c92ed8a

Browse files
author
Enrico M. Crisostomo
committed
Merge branch 'release/1.2.0'
2 parents a536c11 + 4825834 commit c92ed8a

File tree

2 files changed

+47
-22
lines changed

2 files changed

+47
-22
lines changed

README.md

Lines changed: 22 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -7,21 +7,30 @@ tm-cleanup.sh
77
-------------
88

99
`tm-cleanup.sh` is a ZSH script that lists the completed Time Machine snapshots
10-
and deletes those that are oldest that a specified number of days. The default
11-
threshold is 30 days.
10+
and deletes those that satisfy the specified criteria. Two types of deletion
11+
criteria exist:
12+
13+
* By date: snapshots that are older than a specified number of days are
14+
deleted. The default threshold is 30 days.
15+
16+
* By number: a maximum number of snapshots is retained and oldest snapshots
17+
are deleted.
18+
19+
Only one deletion criteria can be specified.
1220

1321
The syntax of `tm-cleanup.sh` is the following:
1422

1523
```
16-
$ tm-cleanup.sh [-d days] [-f] [-x]
24+
$ tm-cleanup.sh (-d days | -n number) [-f] [-x]
1725
$ tm-cleanup.sh [-h]
1826
```
1927

2028
where
2129

22-
* By default backups older than 30 days will be deleted. If `-d` is
23-
speficied, backups older than the specified number of days will be deleted.
24-
`days` is an unsigned positive integer number.
30+
* If `-d` is specified, backups older than the specified number of days will
31+
be deleted. `days` is a positive integer number.
32+
* `-n` specifies the number of backups to retain. `number` is a positive
33+
integer number.
2534
* By default, `tm-cleanup.sh` exits and prints an error message if a Time
2635
Machine backup is currently in progress. `-f` forces the backup deletion
2736
concurrently.
@@ -30,18 +39,17 @@ where
3039
be performed without actually performing any.
3140

3241
This script *never* deletes the latest snapshot, no matter the value of the
33-
`days` option.
42+
`-d` or `-n` options.
3443

3544
Installation
3645
------------
3746

3847
The scripts require no installation: they can be downloaded and run from any
39-
location.
40-
However, this repository provides an installation script that creates
41-
symbolic links to `/usr/local/bin`, a directory which is included by default
42-
in the `${PATH}` of any OS X user.
43-
Installing the symbolic links has the advantage of always providing the current
44-
version of the scripts on the `${PATH}` when the local repository is updated.
48+
location. However, this repository provides an installation script that creates
49+
symbolic links to `/usr/local/bin`, a directory which is included by default in
50+
the `${PATH}` of any OS X user. Installing the symbolic links has the advantage
51+
of always providing the current version of the scripts on the `${PATH}` when the
52+
local repository is updated.
4553

4654
To install the symbolic links:
4755

@@ -74,7 +82,7 @@ Bug reports can be sent directly to the authors.
7482

7583
-----
7684

77-
Copyright (C) 2015 Enrico M. Crisostomo
85+
Copyright (C) 2016 Enrico M. Crisostomo
7886

7987
This program is free software; you can redistribute it and/or modify
8088
it under the terms of the GNU General Public License as published by

tm-cleanup.sh

Lines changed: 25 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#!/bin/zsh
22
# -*- coding: utf-8; tab-width: 2; indent-tabs-mode: nil; sh-basic-offset: 2; sh-indentation: 2; -*- vim:fenc=utf-8:et:sw=2:ts=2:sts=2
33
#
4-
# Copyright (C) 2015, Enrico M. Crisostomo
4+
# Copyright (C) 2016, Enrico M. Crisostomo
55
#
66
# This program is free software; you can redistribute it and/or modify
77
# it under the terms of the GNU General Public License as published by
@@ -24,7 +24,7 @@ set -o errexit
2424
set -o nounset
2525

2626
PROGNAME=$0
27-
VERSION=1.1.0
27+
VERSION=1.2.0
2828

2929
command -v tmutil > /dev/null 2>&1 || {
3030
>&2 print -- Cannot find tmutil.
@@ -36,7 +36,7 @@ print_usage()
3636
print -- "${PROGNAME} ${VERSION}"
3737
print
3838
print -- "Usage:"
39-
print -- "${PROGNAME} [-d days] [-n number] [-f] [-x]"
39+
print -- "${PROGNAME} (-d days | -n number) [-f] [-x]"
4040
print -- "${PROGNAME} [-h]"
4141
print
4242
print -- "Options:"
@@ -59,9 +59,10 @@ FORCE_EXECUTION=0
5959
# Execution modes
6060
# - 0: number of days
6161
# - 1: number of backups
62-
MODE_DAYS=0
63-
MODE_BACKUPS=1
64-
typeset -i EXECUTION_MODE=-1
62+
MODE_DAYS=1
63+
MODE_BACKUPS=2
64+
MODE_UNKNOWN=0
65+
typeset -i EXECUTION_MODE=${MODE_UNKNOWN}
6566
typeset -i ARGS_PROCESSED=0
6667

6768
parse_opts()
@@ -75,14 +76,14 @@ parse_opts()
7576
;;
7677
d)
7778
DAYS_TO_KEEP=${OPTARG}
78-
EXECUTION_MODE=${MODE_DAYS}
79+
EXECUTION_MODE=$((MODE_DAYS | EXECUTION_MODE))
7980
;;
8081
f)
8182
FORCE_EXECUTION=1
8283
;;
8384
n)
8485
NUMBER_TO_KEEP=${OPTARG}
85-
EXECUTION_MODE=${MODE_BACKUPS}
86+
EXECUTION_MODE=$((MODE_BACKUPS | EXECUTION_MODE))
8687
;;
8788
x)
8889
DRY_RUN=1
@@ -197,6 +198,18 @@ then
197198
exit 4
198199
fi
199200

201+
if (( ${EXECUTION_MODE} == ${MODE_UNKNOWN} ))
202+
then
203+
>&2 print -- No mode specified. Exiting.
204+
exit 2
205+
fi
206+
207+
if (( EXECUTION_MODE & (EXECUTION_MODE - 1) ))
208+
then
209+
>&2 print -- Only one mode can be specified. Exiting.
210+
exit 2
211+
fi
212+
200213
# Get the full list of backups from tmutil
201214
TM_BACKUPS=( "${(ps:\n:)$(tmutil listbackups)}" )
202215

@@ -211,4 +224,8 @@ case ${EXECUTION_MODE} in
211224
${MODE_BACKUPS})
212225
process_by_backups
213226
;;
227+
*)
228+
>&2 print -- Unexpected mode. Exiting.
229+
exit 4
230+
;;
214231
esac

0 commit comments

Comments
 (0)