Return an object's position and rotation to what it was a certain amount of time ago

I have an object that is moved by following the player’s mouse position. After a certain event I need the object to return to it’s prior position/rotation from a certain amount of time ago. I know to do this I probably need to constantly keep track of the object’s transform somehow. So far I have this:

public Transform earlierTransform;
public float waitTime = 2f;

void Update () {
		StartCoroutine (SaveEarlierTransform ());
		if (Input.GetKeyDown (KeyCode.Space)) {
				//set position and rotation equal to what it was earlier
				transform.position = earlierTransform.position;
				transform.rotation = earlierTransform.rotation;
		}
}

IEnumerator SaveEarlierTransform () {
		yield return new WaitForSeconds (waitTime);
		earlierTransform = transform;
}

// reference the object in question
Public GameObject myObject;

// reference object's X, Y, Z values
public float initialPositionX;
public float initialPositionY;
public float initialPositionZ;

void Start()
{
    // set the initial position of myObject for X, Y and Z
    initialPositionX = myObject.tranform.position.x;
    initialPositionY = myObject.tranform.position.y;
    initialPositionZ = myObject.tranform.position.z;
}

// call this method when you want to return the object to its original position
public void MoveObjectBack()
{
    // move it back to its initial position
    myObject.transform.position = new Vector3(initialPositionX, initialPositionY, initialPositionZ);
}

Depending on how accurate you want the position or rotation to be, you can choose to store the object’s rotation and transform every few seconds or less.

public float delayBetweenRecords; // Delay between storing positions and rotation
public float timeBeforePresent; // Amount of time to jump back
public bool recordCheck; // If false, we stop recording positions

private Vector3 pastPosition;
private Vector3 pastRotation;
private IEnumerator saveRecord; // Reference to our coroutine

void Start() {
    Invoke("SaveRecordHandler", timeBeforePresent);
}

void Update() {
    if (Input.GetKeyDown (KeyCode.Space)) {
        transform.position = pastPosition;
        transform.rotation = pastRotation;
    }
}

private void SaveRecordHandler() {
    // If coroutine is already running, we stop it
    if (saveRecord != null) {
        StopCoroutine(saveRecord);
        saveRecord = null;
    }
    
    if (recordCheck) {
        saveRecord = SaveRecord();
        StartCoroutine(saveRecord);
    }
{

private IEnumerator SaveRecord() {
    while (recordCheck) {
        pastPosition = transform.position;
        pastRotation = transform.rotation;
        yield return new WaitForRealtimeSeconds(delayBetweenRecords);
    }

    // Reset variable
    saveRecord = null;
}