-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclaude-backup
More file actions
executable file
·95 lines (77 loc) · 2.72 KB
/
claude-backup
File metadata and controls
executable file
·95 lines (77 loc) · 2.72 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
#!/bin/bash
# Claude Code Conversation Backup
# Creates a timestamped backup of ~/.claude data
set -e
# Save original directory to return to it at the end
ORIGINAL_DIR="$(pwd)"
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
# Load configuration
source "$SCRIPT_DIR/lib-claude-sync.sh"
# Handle --version flag
if [ "$1" = "--version" ] || [ "$1" = "-v" ]; then
show_version "$SCRIPT_DIR" "claude-backup"
exit 0
fi
load_config "$SCRIPT_DIR"
CLAUDE_DIR="$CLAUDE_DATA_DIR"
BACKUP_DIR="$SCRIPT_DIR/backups"
TIMESTAMP=$(date '+%Y%m%d-%H%M%S')
HOSTNAME=$(hostname)
BACKUP_NAME="${TIMESTAMP}-${HOSTNAME}"
BACKUP_PATH="$BACKUP_DIR/$BACKUP_NAME"
echo "=== Claude Code Backup ==="
echo ""
# Check if Claude directory exists
if [ ! -d "$CLAUDE_DIR" ]; then
echo "Error: $CLAUDE_DIR does not exist."
echo "Claude Code may not have been used yet on this machine."
cd "$ORIGINAL_DIR"
exit 1
fi
# Create backup directory
mkdir -p "$BACKUP_DIR"
echo "Creating backup: $BACKUP_NAME"
echo "Source: $CLAUDE_DIR"
echo "Destination: $BACKUP_PATH"
echo ""
# Create backup using tar for better compression and integrity
tar -czf "$BACKUP_PATH.tar.gz" \
-C "$(dirname "$CLAUDE_DIR")" \
--exclude='$(basename "$CLAUDE_DIR")/.credentials.json' \
--exclude='$(basename "$CLAUDE_DIR")/debug' \
--exclude='$(basename "$CLAUDE_DIR")/statsig' \
"$(basename "$CLAUDE_DIR")"
echo "Backup created successfully!"
echo ""
echo "Backup file: $BACKUP_PATH.tar.gz"
echo "Size: $(du -h "$BACKUP_PATH.tar.gz" | cut -f1)"
echo ""
# Show number of backups
BACKUP_COUNT=$(ls -1 "$BACKUP_DIR"/*.tar.gz 2>/dev/null | wc -l)
echo "Total backups: $BACKUP_COUNT"
# Check retention policy
RETENTION_DAYS="$CLAUDE_BACKUP_RETENTION_DAYS"
if [ "$BACKUP_COUNT" -gt 20 ] || [ -n "$RETENTION_DAYS" ]; then
echo ""
echo "Retention policy: Keep backups for $RETENTION_DAYS days"
# Auto-cleanup old backups based on retention
if [ -n "$RETENTION_DAYS" ] && [ "$RETENTION_DAYS" -gt 0 ]; then
OLD_BACKUPS=$(find "$BACKUP_DIR" -name "*.tar.gz" -type f -mtime +"$RETENTION_DAYS" 2>/dev/null)
if [ -n "$OLD_BACKUPS" ]; then
echo "Cleaning up backups older than $RETENTION_DAYS days..."
echo "$OLD_BACKUPS" | while read old_backup; do
echo " Removing: $(basename "$old_backup")"
rm "$old_backup"
done
fi
elif [ "$BACKUP_COUNT" -gt 20 ]; then
echo "Warning: You have $BACKUP_COUNT backups. Consider cleaning old ones:"
echo " ls -lth $BACKUP_DIR/"
echo " rm $BACKUP_DIR/<old-backup>.tar.gz"
fi
fi
echo ""
echo "To restore this backup later, run:"
echo " claude-restore $BACKUP_NAME"
# Return to original directory
cd "$ORIGINAL_DIR"