# Transfer files with rsync

#### You:
how do i transfer all my files in /root/backend to /ubuntu/backend from one ubuntu vps to another

#### ChatGPT:
If you're moving files from one Ubuntu VPS to another and the source path is:

- Source VPS: `/root/backend`
- Destination VPS: `/ubuntu/backend`

the simplest method is `rsync`, because it preserves permissions and only transfers changed files.

### Option 1: Transfer directly with `rsync`

Run this **on the source VPS**:

```bash
rsync -avz /root/backend/ ubuntu@DESTINATION_IP:/ubuntu/backend/
```

Replace `DESTINATION_IP` with the IP address of the new VPS.

Example:

```bash
rsync -avz /root/backend/ ubuntu@203.0.113.10:/ubuntu/backend/
```

- `-a` = archive mode (preserves permissions, timestamps, symlinks, etc.)
- `-v` = verbose
- `-z` = compress data during transfer

You will be prompted for the `ubuntu` user's password unless SSH keys are configured.

---

### Option 2: If SSH uses a private key

```bash
rsync -avz -e "ssh -i ~/.ssh/id_rsa" /root/backend/ ubuntu@DESTINATION_IP:/ubuntu/backend/
```

or if the key is elsewhere:

```bash
rsync -avz -e "ssh -i /path/to/key.pem" /root/backend/ ubuntu@DESTINATION_IP:/ubuntu/backend/
```

---

### Option 3: If you are logged in as `root` on the source VPS and the destination directory belongs to `ubuntu`

Create the destination directory first on the new VPS:

```bash
sudo mkdir -p /ubuntu/backend
sudo chown ubuntu:ubuntu /ubuntu/backend
```

Then from the old VPS:

```bash
rsync -avz /root/backend/ ubuntu@DESTINATION_IP:/ubuntu/backend/
```

---

### Option 4: Preserve ownership exactly (requires root access on both VPSs)

From the old VPS:

```bash
rsync -avz -e ssh /root/backend/ root@DESTINATION_IP:/ubuntu/backend/
```

or:

```bash
rsync -aHAX --numeric-ids /root/backend/ root@DESTINATION_IP:/ubuntu/backend/
```

---

### If `rsync` isn't installed

On both servers:

```bash
sudo apt update
sudo apt install rsync
```

---

If you have:

- the old server IP,
- the new server IP,
- and whether you connect as `root` or `ubuntu`,

I can give you the exact command to paste.


#### You:
only backend/data folder and overwrite the data folder in destination vps


#### ChatGPT:
To copy **only `backend/data`** from your old VPS and **overwrite the destination `data` folder**, use `rsync` with the `--delete` option.

---

## ✅ Recommended command (safe + standard)

Run this on the **source VPS**:

```bash
rsync -avz --delete /root/backend/data/ ubuntu@DESTINATION_IP:/ubuntu/backend/data/
```

### What this does:

- Copies only `data/` contents
- Overwrites changed files on destination
- Removes files in destination that don’t exist in source (`--delete`)
- Keeps permissions, timestamps, etc.

---

## ⚠️ Important details

### 1. Trailing slash matters

This is important:

- ✅ `/root/backend/data/` → copies contents of `data`
- ❌ `/root/backend/data` → copies the folder itself inside destination

You want the **trailing slash version**.

---

### 2. Ensure destination folder exists

On the destination VPS:

```bash
mkdir -p /ubuntu/backend/data
```

If permissions are needed:

```bash
chown -R ubuntu:ubuntu /ubuntu/backend
```

---

### 3. If using SSH key

```bash
rsync -avz -e "ssh -i ~/.ssh/id_rsa" --delete /root/backend/data/ ubuntu@DESTINATION_IP:/ubuntu/backend/data/
```

---

## 🚀 Safer alternative (dry run first)

Before actually syncing:

```bash
rsync -avz --delete --dry-run /root/backend/data/ ubuntu@DESTINATION_IP:/ubuntu/backend/data/
```

This shows what *would* change without doing it.

---

If you want, I can also give you a **zero-downtime sync method** (useful if this is a live database or production backend).
