File size: 1,936 Bytes
392073e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
eee4731
392073e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
#!/bin/bash

echo "=== Manual Backup Script ==="

# Check if HF_TOKEN and HF_DATASET are set
if [ -z "$HF_TOKEN" ] || [ -z "$HF_DATASET" ]; then
    echo "Error: HF_TOKEN or HF_DATASET not set."
    echo "Please set these environment variables to use backup."
    exit 1
fi

BACKUP_DIR="/tmp/hf_backup"
mkdir -p "$BACKUP_DIR"

echo "Creating backup archive..."
cd /home/coder

# Create backup archive
tar -czf "$BACKUP_DIR/backup.tar.gz" \
    --exclude='*.log' \
    --exclude='*.sock' \
    --exclude='CachedData' \
    --exclude='Cache' \
    --exclude='GPUCache' \
    .config/code-server \
    .local/share/code-server \
    .continue \
    workspace 2>/dev/null

if [ $? -eq 0 ]; then
    echo "Archive created successfully!"
    
    # Get archive size
    SIZE=$(du -h "$BACKUP_DIR/backup.tar.gz" | cut -f1)
    echo "Archive size: $SIZE"
    
    # Upload to HF dataset using Python
    echo "Uploading to Hugging Face dataset: $HF_DATASET"
    
    python3 << 'PYTHON_SCRIPT'
import os
from huggingface_hub import HfApi
from datetime import datetime

try:
    api = HfApi()
    backup_file = "/tmp/hf_backup/backup.tar.gz"
    
    # Create repo if it doesn't exist
    api.create_repo(
        repo_id=os.environ["HF_DATASET"],
        repo_type="dataset",
        private=True,
        exist_ok=True
    )
    
    # Upload backup file
    api.upload_file(
        path_or_fileobj=backup_file,
        path_in_repo="backup.tar.gz",
        repo_id=os.environ["HF_DATASET"],
        repo_type="dataset"
    )
    
    timestamp = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
    print(f"✓ Backup uploaded successfully at {timestamp}!")
    
except Exception as e:
    print(f"✗ Upload failed: {e}")
    exit(1)
PYTHON_SCRIPT

    if [ $? -eq 0 ]; then
        echo "=== Backup Complete ==="
    else
        echo "=== Backup Failed ==="
        exit 1
    fi
else
    echo "Failed to create backup archive"
    exit 1
fi