-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvault_sync.sh
More file actions
216 lines (176 loc) · 4.38 KB
/
vault_sync.sh
File metadata and controls
216 lines (176 loc) · 4.38 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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
#!/bin/bash
# Git Sync Commands for Vault
# This script provides common git sync operations for the vault
VAULT_DIR="./vault"
# Colors for output
GREEN='\033[0;32m'
RED='\033[0;31m'
YELLOW='\033[1;33m'
NC='\033[0m' # No Color
echo "=========================================="
echo "Vault Git Sync Utility"
echo "=========================================="
# Function to check if we're in vault directory
check_vault() {
if [ ! -d "$VAULT_DIR/.git" ]; then
echo -e "${RED}Error: Vault is not a git repository${NC}"
echo "Run: cd $VAULT_DIR && git init"
exit 1
fi
}
# Function to setup remote
setup_remote() {
echo -e "${YELLOW}Setting up git remote...${NC}"
read -p "Enter remote repository URL: " REMOTE_URL
cd $VAULT_DIR
# Check if remote exists
if git remote | grep -q "origin"; then
git remote set-url origin "$REMOTE_URL"
echo -e "${GREEN}✓ Updated remote URL${NC}"
else
git remote add origin "$REMOTE_URL"
echo -e "${GREEN}✓ Added remote${NC}"
fi
cd ..
}
# Function to pull changes
pull_changes() {
echo -e "${YELLOW}Pulling changes from remote...${NC}"
cd $VAULT_DIR
# Fetch first
git fetch origin
# Check if there are changes
LOCAL=$(git rev-parse @)
REMOTE=$(git rev-parse @{u} 2>/dev/null)
if [ "$LOCAL" = "$REMOTE" ]; then
echo -e "${GREEN}✓ Already up to date${NC}"
else
# Pull with rebase
if git pull --rebase origin main; then
echo -e "${GREEN}✓ Pulled changes successfully${NC}"
else
echo -e "${RED}✗ Pull failed - check for conflicts${NC}"
git rebase --abort 2>/dev/null
exit 1
fi
fi
cd ..
}
# Function to push changes
push_changes() {
echo -e "${YELLOW}Pushing changes to remote...${NC}"
cd $VAULT_DIR
# Check if there are changes
if [ -z "$(git status --porcelain)" ]; then
echo -e "${GREEN}✓ No changes to push${NC}"
cd ..
return
fi
# Add all changes
git add .
# Commit
read -p "Enter commit message (or press Enter for default): " COMMIT_MSG
if [ -z "$COMMIT_MSG" ]; then
COMMIT_MSG="Update $(date '+%Y-%m-%d %H:%M:%S')"
fi
git commit -m "$COMMIT_MSG"
# Push
if git push origin main; then
echo -e "${GREEN}✓ Pushed changes successfully${NC}"
else
echo -e "${RED}✗ Push failed${NC}"
exit 1
fi
cd ..
}
# Function to sync (pull + push)
sync_vault() {
echo -e "${YELLOW}Syncing vault...${NC}"
pull_changes
push_changes
echo -e "${GREEN}✓ Sync complete${NC}"
}
# Function to show status
show_status() {
echo -e "${YELLOW}Vault status:${NC}"
cd $VAULT_DIR
# Show branch
BRANCH=$(git branch --show-current)
echo "Branch: $BRANCH"
# Show status
git status --short
# Show last commit
echo ""
echo "Last commit:"
git log -1 --oneline
cd ..
}
# Function to create initial commit
init_vault() {
echo -e "${YELLOW}Initializing vault git repository...${NC}"
cd $VAULT_DIR
# Initialize git
git init
# Configure
git config user.name "Vault Agent"
git config user.email "vault@agent.local"
# Create .gitignore
cat > .gitignore << 'EOF'
# Ignore sensitive files
.env
*.key
*.pem
credentials/
sessions/
whatsapp_session/
banking/
__pycache__/
*.pyc
*.log
EOF
# Initial commit
git add .gitignore
git commit -m "Initial vault setup"
# Create main branch
git branch -M main
echo -e "${GREEN}✓ Vault initialized${NC}"
cd ..
}
# Main menu
case "$1" in
init)
init_vault
;;
setup)
check_vault
setup_remote
;;
pull)
check_vault
pull_changes
;;
push)
check_vault
push_changes
;;
sync)
check_vault
sync_vault
;;
status)
check_vault
show_status
;;
*)
echo "Usage: $0 {init|setup|pull|push|sync|status}"
echo ""
echo "Commands:"
echo " init - Initialize vault as git repository"
echo " setup - Setup git remote URL"
echo " pull - Pull changes from remote"
echo " push - Push changes to remote"
echo " sync - Pull and push (full sync)"
echo " status - Show vault git status"
exit 1
;;
esac