Apple OSX Time Machine

From HaFrWiki
Jump to: navigation, search

Time Machine is a great tool for saving the Data on your Mac and/or MacBook. Here are 2 helpful extra's

Backup Frequency

The standard Backup Frequency is set to 1 hour (3600 seconds).

TimeMachineEdit App

The most easy way is the app that creates a wrapper for the Backup mechanism.
TimeMachineEditor starts backups in Time Machine when it is most appropriate, the whole backup process is still handled by Time Machine.
TimeMachineEditor does not make changes to your system, it simply runs as an alternative scheduler to start your backups.
Therefore it is safe and easy to use. It does not use the app settings at all.

Manual Edit

Manually Edit the Time Machine Preference File
The one-hour default is actually a «magic number» stored in a preference file; the default value of 3600 represents the number of seconds between backups.
The file we need to edit is located under /System/Library/LaunchDaemons.

OSX Monterey

The required file-data is:

  • Location: /System/Library/LaunchDaemons
  • File: com.apple.backupd-helper.plist
<key>com.apple.backupd-auto</key>
  <dict>
    <key>Interval</key>
    <integer>3600</integer>
    <key>Delay</key>
    <integer>3600</integer>
    <key>GracePeriod</key>
    <integer>1800</integer>
    <key>Repeating</key>
    <true/>
    <key>AllowBattery</key>
    <true/>
    <key>PowerNap</key>
    <true/>
    <key>Priority</key>
    <string>Utility</string>
  </dict>

Change the key Interval value 3600 into the requested value (3600 = 1 hour).

OSX before Monterey


Open com.apple.backupd-auto.plist with a text editor on your machine (e.g., TextEdit) and find the following in the file:

<key>StartInterval</key>
<integer>3600</integer>
<key>RunAtLoad</key>

Deprecated


There are several methods, but I have chosen for the terminal way:

  • Open a Terminal session
  • Enter the command after the dollar sign, without the dollar sign:
# Note: Change the value 3600 to the desired backup interval.
$ sudo defaults write /System/Library/LaunchDaemons/com.apple.backup-auto StartInterval -int 3600
  • Press <Enter>

Delete old backups

Time Machine is based on the (Terminal) application tmutil. Deleting old backups on Time Machine Article on DZone [1].

Old backups can be deleted in many ways, the simplest one being the following:

  • Open the Time Machine application when the Finder application is in the foreground.
  • Navigate to the backup to be deleted double clicking on the corresponding position of the ruler on the right side of the screen.
  • Right click with the mouse on the empty space in the Finder.
  • Select Delete Backup.
  • Time Machine will ask you to introduce the super user password and then it will delete the selected backup.

Unfortunately this method is very clumsy if you need to delete many backups.

OSX Ventura

Another way is using the tmutil command to perform the deletion.
Not only it is simpler, but it will allow you to do it programmatically if you need to.
To delete a backup using tmutil you must perform the following operations:

  • Make sure your time machine backup disk is mounted (the simplest way to do it is opening the Time Machine application).
  • The format of the backup folders created by Time Machine is YYYY-MM-DD-HHmmss.
    If you want to delete a specific backup, you can use the following command:
$ sudo tmutil delete -d <machine-directory> -t <timestamp>

# To get the machine-directory do:
$ tmutil machinedirectory

# To view all available backups do:
$ tmutil listbackups
  • machinedirectory : Use the given CLI to get the correct directory.
  • timestamp : A valid timestamp taken from the CLI to get all backups.

An example:

# Obtaining the list of backups.
$ tmutil listbackups
...
/Volumes/.timemachine/6504D95A-E415-47BB-A561-A5A221BF5D5A/2022-03-18-070119.backup/2022-03-18-070119.backup
/Volumes/.timemachine/6504D95A-E415-47BB-A561-A5A221BF5D5A/2022-04-01-110146.backup/2022-04-01-110146.backup
...

# Obtaining your TimeMachine machinedirectory
$ tmutil machinedirectory
/Volumes/XXXX/YYYY

# Delete the first Backup
sudo tmutil -d /Volumes/XXXX/YYYY -t 2022-03-18-070119
Password:


OSX before Ventura

Another way is using the tmutil command to perform the deletion.
Not only it is simpler, but it will allow you to do it programmatically if you need to.
To delete a backup using tmutil you must perform the following operations:

  • Make sure your time machine backup disk is mounted (the simplest way to do it is opening the Time Machine application).
  • The format of the backup folders created by Time Machine is YYYY-MM-DD-HHmmss.
    If you want to delete a specific backup, you can use the following command:
$ sudo tmutil delete /full/path/to/backup/Backups.backupdb/machine/backup-name

Time Machine currently stores its backups in a folder named after the backed up machine, into the Backups.backupdb folder in the backup disk.
This means that, if your machine is called iMac and your backup disk is mounted on /Volumes/Time Machine Backups, then backups will be located in the following folder:

  • /Volumes/Time Machine Backups/Backups.backupdb/iMac

If you want to delete the 2014-02-02-123411 backup, you must run the following command:

$ sudo tmutil delete /Volumes/Time Machine Backups/Backups.backupdb/iMac/2014-02-02-123411

You can easily make a script, for example, to delete all the backups of a specific month.
The following commands will delete all the backups created in January 2014 (lines were split with \):

$ sudo bash
Password:
$ for i in /Volumes/Time\ Machine\ Backups/Backups.backupdb/iMac/2014-01* ; \
  do tmutil delete "$i" ; \
  done

For Harm only

Note for the Wiki Webpage owner Harm:
A 'Shell Script' TimeMachine.sh has been been created and resides in the directory Users/Shared/Data/USR_iMac.

Content of Time Machine

Often you wanna know what Time Machine has backing up. This Perl script may help you find out.

Usage

To use the tool timedog (installed on /Sources/Perl) do: <syntaxhighlight lang="bash"> $ cd /Volumes/MPRO-TB/Backups.backupdb/Harm’s\ MacBook\ Pro $ /Sources/Perl/timedog -d 5 -l </syntaxhighlight>

Background

By default, timedog will examine the most recent backup, compare it to the one prior, and report all changed files. The -d flag controls the directory depth of reporting, -l disables reporting for symbolic links (for which Time Machine seems to create a new copy of the link each backup). You can also specify a backup of your choice as an argument, though it must also have one prior backup with which to compare.

Source

#!/usr/bin/perl
#
# timedog
#
#   J.D. Smith (jdtsmith A@T gmail _dot_ com)
#
# Display the files which time machine backed up in its most recent
# backup (or any of your choosing).  Uses the fact that Time Machine
# creates hard links for unchanged files and directories.  Reports old
# and new sizes of the changed files, along with the total file count
# and backup size.
#
# Usage:  timedog [-d depth] [-l] [-n] [latest]
#
#   N.B. You must first mount the time machine volume, and change to
#   the directory containing your time machine backup directories, the
#   ones named like 2008-07-14-112245/.  This can be found in the
#   directory /Volumes/TM/Backups.backupdb/hostname or similar.
#
#     latest: The backup directory for which you'd like to see the
#             changed contents.  Defaults to the most recent (the one
#             linked to by Latest)
#
#         -l: Omit symbolic links from the summary.  For whatever
#             reason, Time Machine creates a new version of a symbolic
#             link each and every time it backs up.
#
#         -n  Use simple fixed width formatting, and omit summaries.
#
#   -d depth: By default, all files are printed, which can get
#             lengthy.  With this option, summarize changes in
#             directories only down to the given depth.  The number of
#             files and subdirectories which changed will be reported
#             as [n].
#
# Example:
#   % cd /Volumes/TM/Backups.backupdb/myhost
#   % timedog -d 5 -l
#
###############################################################################
# 
# LICENSE
#
#  Copyright (C) 2008 J.D. Smith
#
#  This file is free software; you can redistribute it and/or modify
#  it under the terms of the GNU General Public License as published
#  by the Free Software Foundation; either version 2, or (at your
#  option) any later version.
#
#  This File is distributed in the hope that it will be useful, but
#  WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
#  General Public License for more details.
#
#  You should have received a copy of the GNU General Public License
#  along with this file; see the file COPYING.  If not, write to the
#  Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
#  Boston, MA 02110-1301, USA.
#
###############################################################################


use File::Find;
use Fcntl ':mode';
use Getopt::Std;

getopt('d');

sub bytes {
  my $bytes=shift;
  $format=shift || ".1";
  @suff=("B","KB","MB","GB","TB");
  for ($suff=shift @suff; $#suff>=0 and $bytes>=1000.; $suff=shift @suff) {
    $bytes/=1024.;
  }
  return int($bytes) . $suff if int($bytes)==$bytes;
  return sprintf("%${format}f",$bytes) . $suff;
}

sub summarize {
  ($size,$size_old,$old_exists,$name,$cnt)=@_;
  if ($opt_n) {
    printf "%12d %12d %s\n",$old_exists?$size_old:0,$size,$name;
  } else {
    if ($opt_d) { 
      printf "%9s->%9s %6s %s\n",$old_exists?bytes($size_old):"....  ",
	bytes($size),$cnt?"[$cnt]":"",$name;
    } else {
      printf "%9s->%9s %s\n",$old_exists?bytes($size_old):"....  ",
	bytes($size),$name;
    }
  }
}


opendir DIR,"." or die "Can't open directory.";
@files=sort grep {m|[0-9]{4}-[0-9]{2}-[0-9]{2}-[0-9]+$|} readdir(DIR);
die "None or only one Time Machine backups found." if @files == 1;

if (@ARGV) {
  $latest=$ARGV[0];
  $latest=~s|/$||;
  foreach (@files) {
    last if $_ eq $latest;
    $last=$_;
  }
  die "Invalid backup directory" if !defined($last) || $last eq $latest;
} else {
  ($last,$latest)=@files[$#files-1..$#files];
}

print "==> Comparing TM backup $latest to $last\n" unless $opt_n;

my ($old_exists,$rold_exists,$rsize,$rsize_old);
$total_size=0;
$total_cnt=0;

find({wanted =>
      sub{
	($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size) =
	  lstat($_);
	($old=$_)=~s/^$latest/$last/;
	if (-e $old) {
	  ($dev, $ino_old,$mode_old,
	   $nlink,$uid,$gid,$rdev,$size_old) = lstat($old);
	  if ($ino == $ino_old) { # Prune matching
	    $File::Find::prune=1 if -d;
	    return
	  }
	  $old_exists=1;
	} else {$old_exists=0;}


	$total_size+=$size;

	$link=S_ISLNK($mode);
	return if $opt_l && $link;

	# Don't include links in the count
	$total_cnt++;
	($name=$_)=~s/^$latest//;

	if ($opt_d) {
	  $depth=$name=~tr|/||;
	  $rsize+=$size;
	  $rsize_old+=$size_old if $old_exists;
	  $rcnt++;
	  return if S_ISDIR($mode) || $depth > $opt_d; # Post will handle
	}
	$name.="/" if S_ISDIR($mode);
	$name.="@" if $link;
	summarize($size,$size_old,$old_exists,$name);
      },
      preprocess =>
      (!$opt_d)?0:
      sub{
	$depth=$File::Find::dir=~tr|/||;
	if ($depth<=$opt_d) {
	  # Starting a new printable directory; zero out sizes
	  $rsize=$rsize_old=$rcnt=0;
	  $rold_exists=-e $File::Find::dir;
	}
	@_;
      },
      postprocess =>
      (!$opt_d)?0:
      sub{
	$depth=$File::Find::dir=~tr|/||;
	return if $depth > $opt_d;
	# This directory is at or below the depth, summarize it
	($name=$File::Find::dir)=~s/^$latest//;
	summarize($rsize,$rsize_old,$rold_exists,$name.'/',$rcnt)
	  if $rsize || $rsize_old;
	$rsize=$rsize_old=$rcnt=0;
      },
      no_chdir => 1}, $latest);

print "==> Total Backup: $total_cnt changed files/directories, ",
  bytes($total_size,".2"),"\n" unless $opt_n;

See also

top

Reference

top

  1. DZone, Shrink your Time Machine Backups, by Enrico Maria Crisostomo