45 lines
1.6 KiB
Markdown
45 lines
1.6 KiB
Markdown
# Live File Migration Pattern
|
|
|
|
When reorganizing, migrating, or archiving active infrastructure files (like scripts, documents, markdown notes, or configurations) on a live server, moving the file abruptly can break cron jobs, hardcoded paths in scripts, references in other notes, or user habits.
|
|
|
|
Instead of a simple `mv`, use the **Copy-Remove-Symlink** pattern to safely migrate files while keeping the old path functional.
|
|
|
|
## The Pattern
|
|
|
|
```bash
|
|
migrate_file() {
|
|
src="$1"
|
|
dest_dir="$2"
|
|
|
|
# 1. Ensure source exists
|
|
if [ -f "$src" ]; then
|
|
filename=$(basename "$src")
|
|
dest_path="${dest_dir}/${filename}"
|
|
|
|
# 2. Copy to new destination
|
|
cp "$src" "$dest_path"
|
|
|
|
# 3. Remove original
|
|
rm "$src"
|
|
|
|
# 4. Create a symlink at the original location pointing to the new destination
|
|
ln -s "$dest_path" "$src"
|
|
|
|
echo "Migrated $src -> $dest_path"
|
|
else
|
|
echo "File not found: $src"
|
|
fi
|
|
}
|
|
|
|
# Example usage:
|
|
mkdir -p /root/projects/itpp-infra/backups
|
|
migrate_file "/root/.hermes/references/backup-policy.md" "/root/projects/itpp-infra/backups"
|
|
```
|
|
|
|
### Why this works
|
|
1. Existing scripts/references pointing to the old path follow the symlink transparently.
|
|
2. The file is physically relocated to the new taxonomic structure.
|
|
3. Over time, references can be updated to the new path, and the symlinks eventually cleaned up when safe.
|
|
|
|
Use this whenever the user asks to "move existing docs", "reorganize folder X", or "migrate configurations" to prevent silent breakages in the background infrastructure.
|