Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Plugin/OSX: cleanup #2008

Merged
merged 6 commits into from
Jan 7, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions clean_files.txt
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,8 @@ plugins/available/jump.plugin.bash
plugins/available/less-pretty-cat.plugin.bash
plugins/available/node.plugin.bash
plugins/available/nodenv.plugin.bash
plugins/available/osx-timemachine.plugin.bash
plugins/available/osx.plugin.bash
plugins/available/percol.plugin.bash
plugins/available/plenv.plugin.bash
plugins/available/pyenv.plugin.bash
Expand Down
121 changes: 66 additions & 55 deletions plugins/available/osx-timemachine.plugin.bash
Original file line number Diff line number Diff line change
@@ -1,84 +1,95 @@
cite about-plugin
# shellcheck shell=bash
about-plugin 'OS X Time Machine functions'

if [[ "${OSTYPE}" != 'darwin'* ]]; then
_log_warning "This plugin only works with Mac OS X"
return 1
fi

function time-machine-destination() {
group "osx-timemachine"
about "Shows the OS X Time Machine destination/mount point"
group "osx-timemachine"
about "Shows the OS X Time Machine destination/mount point"

echo $(tmutil destinationinfo | grep "Mount Point" | sed -e 's/Mount Point : \(.*\)/\1/g')
tmutil destinationinfo | grep "Mount Point" | sed -e 's/Mount Point : \(.*\)/\1/g'
}

function time-machine-list-machines() {
group "osx-timemachine"
about "Lists the OS X Time Machine machines on the backup volume"
group "osx-timemachine"
about "Lists the OS X Time Machine machines on the backup volume"

local tmdest="$(time-machine-destination)/Backups.backupdb"
local tmdest
tmdest="$(time-machine-destination)/Backups.backupdb"

find "$tmdest" -maxdepth 1 -mindepth 1 -type d | grep -v "/\." | while read line ; do
echo "${line##*/}"
done
find "$tmdest" -maxdepth 1 -mindepth 1 -type d | grep -v "/\." | while read -r line; do
echo "${line##*/}"
done
}

function time-machine-list-all-backups() {
group "osx-timemachine"
about "Shows all of the backups for the specified machine"
param "1: Machine name (optional)"
example "time-machine-list-all-backups my-laptop"

# Use the local hostname if none provided
local COMPUTERNAME=${1:-$(scutil --get ComputerName)}
local BACKUP_LOCATION="$(time-machine-destination)/Backups.backupdb/$COMPUTERNAME"

find "$BACKUP_LOCATION" -maxdepth 1 -mindepth 1 -type d | while read line ; do
echo "$line"
done
group "osx-timemachine"
about "Shows all of the backups for the specified machine"
param "1: Machine name (optional)"
example "time-machine-list-all-backups my-laptop"

# Use the local hostname if none provided
local COMPUTERNAME BACKUP_LOCATION
COMPUTERNAME=${1:-$(scutil --get ComputerName)}
BACKUP_LOCATION="$(time-machine-destination)/Backups.backupdb/$COMPUTERNAME"

find "$BACKUP_LOCATION" -maxdepth 1 -mindepth 1 -type d | while read -r line; do
echo "$line"
done
}

function time-machine-list-old-backups() {
group "osx-timemachine"
about "Shows all of the backups for the specified machine, except for the most recent backup"
param "1: Machine name (optional)"
example "time-machine-list-old-backups my-laptop"

# Use the local hostname if none provided
local COMPUTERNAME=${1:-$(scutil --get ComputerName)}
local BACKUP_LOCATION="$(time-machine-destination)/Backups.backupdb/$COMPUTERNAME"

# List all but the most recent one
find "$BACKUP_LOCATION" -maxdepth 1 -mindepth 1 -type d -name 2\* | sed \$d | while read line ; do
echo "$line"
done
group "osx-timemachine"
about "Shows all of the backups for the specified machine, except for the most recent backup"
param "1: Machine name (optional)"
example "time-machine-list-old-backups my-laptop"

# Use the local hostname if none provided
local COMPUTERNAME BACKUP_LOCATION
COMPUTERNAME=${1:-$(scutil --get ComputerName)}
BACKUP_LOCATION="$(time-machine-destination)/Backups.backupdb/$COMPUTERNAME"

# List all but the most recent one
find "$BACKUP_LOCATION" -maxdepth 1 -mindepth 1 -type d -name 2\* | sed \$d | while read -r line; do
echo "$line"
done
}

# Taken from here: http://stackoverflow.com/a/30547074/1228454
function _tm_startsudo() {
sudo -v
( while true; do sudo -v; sleep 50; done; ) &
SUDO_PID="$!"
trap _tm_stopsudo SIGINT SIGTERM
local -x SUDO_COMMAND="plugin/osx-timemachine: keep 'sudo' token alive during long-run 'tmutil' commands"
sudo "-${SUDO_ASKPASS:+A}v" # validate without running a command, using `ssh-askpass` if available.
(while sudo "-${SUDO_ASKPASS:+A}v"; do
sleep 50
done) &
SUDO_PID="$!"
trap _tm_stopsudo SIGINT SIGTERM
}
function _tm_stopsudo() {
kill "$SUDO_PID"
trap - SIGINT SIGTERM
sudo -k
kill "$SUDO_PID"
trap - SIGINT SIGTERM
sudo -k
}

function time-machine-delete-old-backups() {
group "osx-timemachine"
about "Deletes all of the backups for the specified machine, with the exception of the most recent one"
param "1: Machine name (optional)"
example "time-machine-delete-old-backups my-laptop"
group "osx-timemachine"
about "Deletes all of the backups for the specified machine, with the exception of the most recent one"
param "1: Machine name (optional)"
example "time-machine-delete-old-backups my-laptop"

# Use the local hostname if none provided
local COMPUTERNAME=${1:-$(scutil --get ComputerName)}
# Use the local hostname if none provided
local COMPUTERNAME=${1:-$(scutil --get ComputerName)} _old_backup

# Ask for sudo credentials only once
_tm_startsudo
# Ask for sudo credentials only once
_tm_startsudo

echo "$(time-machine-list-old-backups "$COMPUTERNAME")" | while read i ; do
# Delete the backup
sudo tmutil delete "$i"
done
while read -r _old_backup; do
# Delete the backup
sudo tmutil delete "$_old_backup"
done <<< "$(time-machine-list-old-backups "$COMPUTERNAME")"

_tm_stopsudo
_tm_stopsudo
}
Loading