Skip to content
Open
Changes from 1 commit
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
101 changes: 100 additions & 1 deletion bin/computecanada/oops
Original file line number Diff line number Diff line change
@@ -1,5 +1,104 @@
#!/usr/bin/env zsh
#!/bin/zsh -f
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

mmm, why assume that the OS has /bin/zsh ?

If anything, since this is on CVMFS, it would make more sense to use

#!/cvmfs/soft.computecanada.ca/gentoo/2023/x86-64-v3/usr/bin/zsh -f

Copy link
Author

@jaimepinto jaimepinto Nov 14, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Absolutely, you are right.
Should I recommit or could you do it from your end and already make it effective?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well, just did it

# modified from the original nibi-only version 2025-08-22 FMcC
# adapted by Jaime Pinto on Nov/13/2025 to Extended functionality, to
# browse and restore previous versions, deleted or not
#

# --- Argument handling for help, versions, and restore -----------------------

if [[ "$1" == "--help" || "$1" == "-h" ]]; then
cat <<EOF
Usage: oops [path]
oops --versions <file>
oops --restore <file> <timestamp>
oops --help | -h
Default (no options):
Search snapshots for files that have been deleted and can be restored.
Options:
--versions <file>
List all snapshot versions of a specific file.
Shows timestamps and snapshot paths.
--restore <file> <timestamp>
Restore a file from a specific snapshot timestamp.
Timestamp format must match the snapshot directory suffix, e.g.:
2025-01-13_04_00_00_daily
--help, -h
Show this usage information.
Examples:
oops /home/alice/project
oops --versions ~/.bash_history
oops --restore ~/.bash_history 2025-01-13_04_00_00_daily
EOF
exit 0
fi


# --- Versions listing --------------------------------------------------------

if [[ "$1" == "--versions" ]]; then
file=$2
if [[ -z "$file" ]]; then
echo "Usage: oops --versions <file>"
exit 1
fi

dir=$(dirname "$file")
base=$(basename "$file")

if [[ ! -d "$dir/.snapshot" ]]; then
echo "No snapshots available for $dir"
exit 1
fi

echo "Versions of $file found in snapshots:"
echo

for snap in "$dir"/.snapshot/backup_*; do
snapfile="$snap/$base"
if [[ -f "$snapfile" ]]; then
timestamp="${snap##*backup_}"
size=$(stat -c %s "$snapfile" 2>/dev/null)
echo "$timestamp size=${size}B $snapfile"
fi
done

exit 0
fi


# --- Restore mode ------------------------------------------------------------

if [[ "$1" == "--restore" ]]; then
file=$2
timestamp=$3

if [[ -z "$file" || -z "$timestamp" ]]; then
echo "Usage: oops --restore <file> <timestamp>"
exit 1
fi

dir=$(dirname "$file")
base=$(basename "$file")
snap="$dir/.snapshot/backup_${timestamp}"

if [[ ! -f "$snap/$base" ]]; then
echo "Snapshot version does not exist: $snap/$base"
exit 1
fi

echo "Restoring $file from snapshot $timestamp"
cp -i "$snap/$base" "$file"
exit 0
fi

#
# --- End of the custom block; normal behavior continues below ---------------
#

target_path=${1-.}
if [[ ! -d $target_path ]]; then
Expand Down