# nextcloud local S3 migration

Script for migrating Nextcloud primary storage from local to S3 storage
:floppy_disk: to :cloud:

Fork from [mrAceT](https://github.com/mrAceT/nextcloud-S3-local-S3-migration) 
> Revision `main` - 4ab29ef

## local to S3

It will transfer files from **local** based primary storage to a **S3** primary storage.

Before you start, it is probably wise to set $DO_FILES_CLEAN (occ files:cleanup)
and $DO_FILES_SCAN (occ files:scan --all) to '1' once, let the 'Nextcloud' do some checking.. then you'll start out as clean as possible

0. make sure that the Nextcloud cron job is disabled and make sure that you local data is sufficiently large

1. go to the nextcloud root folder and install composer

```bash
wget https://getcomposer.org/installer
mv installer composer-setup.php
mkdir bin
php composer-setup.php --install-dir /var/www/html/nextcloud/bin
chown -R apache:apache bin
rm -f composer-setup.php
chmod 755 bin/*
```

2. install 'aws/aws-sdk-php'

```bash
runuser -u apache -- ./bin/composer.phar require aws/aws-sdk-php
```

3. place 'storage.config.php', 'localtos3.php' and 'tests3.php' in the nextcloud root folder (and set your S3 credentials!)
4. set & check all the config variables in the beginning of the scripts!
5. check your S3 config 

```bash
sudo -u apache php81 -d memory_limit=1024M tests3.php
```

6. start with the highest $TEST => 2 (complete dry run, just checks en dummy uploads etc. NO database changes what so ever!)

```bash
sudo -u apache php81 -d memory_limit=1024M localtos3.php
```

7. set $TEST to a 'small test user", upload the data to S3 for only that user (NO database changes what so ever!)
8. set $TEST to 1 and run the script yet again, upload (**and check**) all the data to S3 (NO database changes what so ever!)
9. set $TEST to 0 and run the script again (this is LIVE, nextcloud will be set into maintenance:mode --on while working ! **database changes!**)

> **DO NOT** skip ahead and go live ($TEST=0) as the first step.. then your downtime will be very long!

With performing 'the move' at step 8 you will decrease the downtime (with maintenance mode:on) immensely!
This because the script will first check if it already has uploaded the latest file, then it can skip to the next and does not need to (slowly) upload it to your S3 bucket!
With a litte luck the final run (with $TEST=0) can be done within a minute!

> **NOTE** step 8 will take a very long time when you have a lot of data to upload!

If everything worked you might want to delete the data in data folder.
Also you probably want to delete this script (and the 'storage.config.php') after running it.
If all went as it should the config data in 'storage.config.php' is included in the 'config/config.php'. Then the 'storage.config.php' can also be removed from your config folder (no sense in having a double config)

## S3 sanity check!

When you

1. have S3 as your primary storage
2. set $TEST to 0
3. **optionally** set $SET_MAINTENANCE to 0
4. (have set/checked all the other variables..)

Then the script 'localtos3.php' will:
- look for entries in S3 and not in the database and vice versa **and remove them**.
This can happen sometimes upon removing an account, preview files might not get removed.. stuff like that..

- check for canceled uploads.
Inspired upon [otherguy/nextcloud-cleanup](https://github.com/otherguy/nextcloud-cleanup/blob/main/clean.php). I have not had this problem, so can not test.. => check only!

- preview cleanup.
Removes previews of files that no longer exist.
There is some initial work for clearing previews.. that is a work in progress, use at your own risc!

The script will do the "sanity check" when migrating also (we want a good and clean migrition, won't we? ;)
