# Replicate Nextcloud instance to another server

## Destination:
Prepare
```bash
export user="user"
export src="(source host)"
export dst="(destination host)"
export path="/home/${user}/delme/${src}--${dst}/"
mkdir -p ${path}
chown -R ${user}:${user} /home/${user}/delme/
ls -la ${path}
namei -mo ${path}
```

Clean before bringing new stuff here (carefully here)
```bash
echo ${path}
ls -la ${path}
read -p "Correct path to clean?"
rm -i ${path}/*
ls -la ${path}
```

## Source
Prepare
```bash
sudo su
export user="user"
export src="(source host)"
export dst="(destination host)"
export path="/home/${user}/delme/${src}--${dst}/"
export file="nextcloud.tgz.$(date +"%Y-%m-%d.%H%M")"
mkdir -p ${path}
ls -lahtr ${path}
```

Compress application directory (takes little time, coffee time)
```bash
tar -czvf ${path}/${file} /var/www/nextcloud/
ls -lahtr ${path}
```

Transfer file to the second instance
```bash
scp -v ${path}/${file} ${user}@${dst}:delme/${src}--${dst}/
```


## Destination (continues):
Observe, that has been transferred correctly
```bash
ls -lahtr ${path}
```

Move aside current Nextcloud application instance before bring new one.
```bash
export dir_current="nextcloud.$(date +"%Y-%m-%d.%H%M")"
echo ${dir_current}
mv /var/www/nextcloud /var/www/${dir_current}
ls -latr /var/www
```

Extract it
```bash
# that is number one, not L letter
export file=$(ls -1 ${path}/nextcloud*)
echo ${file}
tar -xzvf ${file} -C /
```

After successful testing, remove old version, as they might occupy disk space.
```bash
ls -dlr /var/www/nextcloud*
du -h --max-depth=1 /var/www
```