Skip to content

Commit

Permalink
Merge branch 'release/1.2.0'
Browse files Browse the repository at this point in the history
  • Loading branch information
Enrico M. Crisostomo committed Oct 25, 2016
2 parents a536c11 + 4825834 commit c92ed8a
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 22 deletions.
36 changes: 22 additions & 14 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,30 @@ tm-cleanup.sh
-------------

`tm-cleanup.sh` is a ZSH script that lists the completed Time Machine snapshots
and deletes those that are oldest that a specified number of days. The default
threshold is 30 days.
and deletes those that satisfy the specified criteria. Two types of deletion
criteria exist:

* By date: snapshots that are older than a specified number of days are
deleted. The default threshold is 30 days.

* By number: a maximum number of snapshots is retained and oldest snapshots
are deleted.

Only one deletion criteria can be specified.

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

```
$ tm-cleanup.sh [-d days] [-f] [-x]
$ tm-cleanup.sh (-d days | -n number) [-f] [-x]
$ tm-cleanup.sh [-h]
```

where

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

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

Installation
------------

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

To install the symbolic links:

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

-----

Copyright (C) 2015 Enrico M. Crisostomo
Copyright (C) 2016 Enrico M. Crisostomo

This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
Expand Down
33 changes: 25 additions & 8 deletions tm-cleanup.sh
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#!/bin/zsh
# -*- 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
#
# Copyright (C) 2015, Enrico M. Crisostomo
# Copyright (C) 2016, Enrico M. Crisostomo
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
Expand All @@ -24,7 +24,7 @@ set -o errexit
set -o nounset

PROGNAME=$0
VERSION=1.1.0
VERSION=1.2.0

command -v tmutil > /dev/null 2>&1 || {
>&2 print -- Cannot find tmutil.
Expand All @@ -36,7 +36,7 @@ print_usage()
print -- "${PROGNAME} ${VERSION}"
print
print -- "Usage:"
print -- "${PROGNAME} [-d days] [-n number] [-f] [-x]"
print -- "${PROGNAME} (-d days | -n number) [-f] [-x]"
print -- "${PROGNAME} [-h]"
print
print -- "Options:"
Expand All @@ -59,9 +59,10 @@ FORCE_EXECUTION=0
# Execution modes
# - 0: number of days
# - 1: number of backups
MODE_DAYS=0
MODE_BACKUPS=1
typeset -i EXECUTION_MODE=-1
MODE_DAYS=1
MODE_BACKUPS=2
MODE_UNKNOWN=0
typeset -i EXECUTION_MODE=${MODE_UNKNOWN}
typeset -i ARGS_PROCESSED=0

parse_opts()
Expand All @@ -75,14 +76,14 @@ parse_opts()
;;
d)
DAYS_TO_KEEP=${OPTARG}
EXECUTION_MODE=${MODE_DAYS}
EXECUTION_MODE=$((MODE_DAYS | EXECUTION_MODE))
;;
f)
FORCE_EXECUTION=1
;;
n)
NUMBER_TO_KEEP=${OPTARG}
EXECUTION_MODE=${MODE_BACKUPS}
EXECUTION_MODE=$((MODE_BACKUPS | EXECUTION_MODE))
;;
x)
DRY_RUN=1
Expand Down Expand Up @@ -197,6 +198,18 @@ then
exit 4
fi

if (( ${EXECUTION_MODE} == ${MODE_UNKNOWN} ))
then
>&2 print -- No mode specified. Exiting.
exit 2
fi

if (( EXECUTION_MODE & (EXECUTION_MODE - 1) ))
then
>&2 print -- Only one mode can be specified. Exiting.
exit 2
fi

# Get the full list of backups from tmutil
TM_BACKUPS=( "${(ps:\n:)$(tmutil listbackups)}" )

Expand All @@ -211,4 +224,8 @@ case ${EXECUTION_MODE} in
${MODE_BACKUPS})
process_by_backups
;;
*)
>&2 print -- Unexpected mode. Exiting.
exit 4
;;
esac

0 comments on commit c92ed8a

Please sign in to comment.