From 16a492039f9b164f60714ad5c297c0a9e4ff7ac5 Mon Sep 17 00:00:00 2001 From: "Enrico M. Crisostomo" Date: Sun, 23 Oct 2016 00:59:49 +0200 Subject: [PATCH 1/5] Check execution mode --- tm-cleanup.sh | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/tm-cleanup.sh b/tm-cleanup.sh index ba731ca..16d4133 100755 --- a/tm-cleanup.sh +++ b/tm-cleanup.sh @@ -61,7 +61,8 @@ FORCE_EXECUTION=0 # - 1: number of backups MODE_DAYS=0 MODE_BACKUPS=1 -typeset -i EXECUTION_MODE=-1 +MODE_UNKNOWN=-1 +typeset -i EXECUTION_MODE=${MODE_UNKNOWN} typeset -i ARGS_PROCESSED=0 parse_opts() @@ -197,6 +198,12 @@ then exit 4 fi +if (( ${EXECUTION_MODE} == ${MODE_UNKNOWN} )) +then + >&2 print -- No mode specified. Exiting. + exit 2 +fi + # Get the full list of backups from tmutil TM_BACKUPS=( "${(ps:\n:)$(tmutil listbackups)}" ) @@ -211,4 +218,8 @@ case ${EXECUTION_MODE} in ${MODE_BACKUPS}) process_by_backups ;; + *) + >&2 print -- Unexpected mode. Exiting. + exit 4 + ;; esac From d307855dac984f36c892b204e58d2b44a5388841 Mon Sep 17 00:00:00 2001 From: "Enrico M. Crisostomo" Date: Sun, 23 Oct 2016 01:21:30 +0200 Subject: [PATCH 2/5] Update README.md --- README.md | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 6e71493..809ff93 100644 --- a/README.md +++ b/README.md @@ -13,15 +13,16 @@ threshold is 30 days. 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. + specified, backups older than the specified number of days will be deleted. `days` is an unsigned positive integer number. + * `-n` specifies the number of backups to retain. * 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. @@ -30,7 +31,7 @@ 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 ------------ @@ -74,7 +75,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 From 9af19c964df4ff69af133023b19e511b28643aa7 Mon Sep 17 00:00:00 2001 From: "Enrico M. Crisostomo" Date: Tue, 25 Oct 2016 11:50:35 +0200 Subject: [PATCH 3/5] Update README.md --- README.md | 33 ++++++++++++++++++++------------- 1 file changed, 20 insertions(+), 13 deletions(-) diff --git a/README.md b/README.md index 809ff93..8b819e0 100644 --- a/README.md +++ b/README.md @@ -7,22 +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] [-n number] [-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 - specified, backups older than the specified number of days will be deleted. - `days` is an unsigned positive integer number. - * `-n` specifies the number of backups to retain. + * 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. @@ -37,12 +45,11 @@ 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: From 61b8b37415fc65e1a67663c171f72d7d38a90aed Mon Sep 17 00:00:00 2001 From: "Enrico M. Crisostomo" Date: Tue, 25 Oct 2016 11:51:07 +0200 Subject: [PATCH 4/5] Check that only one execution mode can be specified --- tm-cleanup.sh | 20 +++++++++++++------- 1 file changed, 13 insertions(+), 7 deletions(-) diff --git a/tm-cleanup.sh b/tm-cleanup.sh index 16d4133..7f6148c 100755 --- a/tm-cleanup.sh +++ b/tm-cleanup.sh @@ -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 @@ -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:" @@ -59,9 +59,9 @@ FORCE_EXECUTION=0 # Execution modes # - 0: number of days # - 1: number of backups -MODE_DAYS=0 -MODE_BACKUPS=1 -MODE_UNKNOWN=-1 +MODE_DAYS=1 +MODE_BACKUPS=2 +MODE_UNKNOWN=0 typeset -i EXECUTION_MODE=${MODE_UNKNOWN} typeset -i ARGS_PROCESSED=0 @@ -76,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 @@ -204,6 +204,12 @@ then 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)}" ) From 482583417191d25966af6222d7e7d4fdee204558 Mon Sep 17 00:00:00 2001 From: "Enrico M. Crisostomo" Date: Tue, 25 Oct 2016 11:52:40 +0200 Subject: [PATCH 5/5] Bump 1.2.0 --- tm-cleanup.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tm-cleanup.sh b/tm-cleanup.sh index 7f6148c..4605d02 100755 --- a/tm-cleanup.sh +++ b/tm-cleanup.sh @@ -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.