如何迁移ownCloud到nextCloud?

Bash

1 前言

笔者的ownCloud生产环境由于功能的关系需要迁移至nextCloud,于是笔者搭建好新的nextCloud环境后进行用户数据的迁移。

2 最佳实践

2.1 环境信息

2.1.1 ownCloud环境

ownCloud按照以下章节部署,

如何部署ownCloud 10.x?

2.1.2 nextCloud环境

nextCloud按照以下章节部署,

如何部署CentOS 8.x nextCloud?

2.1.3 环境信息汇总

ownCloud Server,
Host Name = owncloud.cmdschool.org
IP Address = 10.168.0.156
Application = owncloud 10.0.10
www Root= /var/www/owncloud
data directory = /data/owncloudData

nextCloud Server,
Host Name = nextcloud.cmdschool.org
IP Address = 10.168.0.80
Application = nextcloud 20.0.1
www Root = /var/www/nextcloud
data directory = /data/netxcloud-data

2.2 分析数据目录结构

In ownCloud Server,

2.2.1 ownCloud的目录结构

In ownCloud Server,

ls -l /data/owncloudData

可见如下输出,

total 693709
drwxrwx---   9 apache apache      4096 Nov  4  2019 00323401-c56f11e5-a663eb63-06a7ea03
drwxrwx---   4 apache apache      4096 Mar 13  2018 00f2b701-4b2111e7-b718eb63-06a7ea03
drwxrwx---   5 apache apache      4096 Jun 14  2019 01963c01-5c2011e5-a663eb63-06a7ea03
#...

2.2.2 nextCloud的目录结构

In nextCloud Server,

ls -l /data/netxcloud-data/
total 3308
drwxr-xr-x.  6 apache apache      72 Nov 30 03:18 admin
drwxr-xr-x. 12 apache apache     159 Nov 20 00:32 appdata_oc19alpcvdbu
-rw-r-----.  1 apache apache   27098 Dec  3 21:42 audit.log
drwxr-xr-x.  2 apache apache       6 Nov 20 01:31 will
drwxr-xr-x.  2 apache apache       6 Nov 19 22:46 cindy
drwxr-xr-x.  2 apache apache       6 Nov 20 01:31 alan

注:以上可见由于设置的关系,数据目录存在差异,需要修正数据目录名称。

2.3 手动测试用户数据同步

In ownCloud Server,

2.3.1 取得用户ID

sudo -u apache php /var/www/owncloud/occ user:list | grep -i will

可见如下信息,

  - 00323401-c56f11e5-a663eb63-06a7ea03: will (Tan Zhen Chao)

“00323401-c56f11e5-a663eb63-06a7ea03”为用户ID号,另外,如果通过数据库匹配,可参阅如下语句,

select directory_uuid from owncloud.oc_ldap_user_mapping where ldap_dn like '%will%'

2.3.2 查看用户设置

sudo -u apache php /var/www/owncloud/occ user:setting 00323401-c56f11e5-a663eb63-06a7ea03

可见如下显示,

  - core:
    - lang: zh_CN
    - timezone: Asia/Shanghai
  - files:
    - file_sorting: name
    - file_sorting_direction: desc
    - quota: 5 GB
  - files_external:
    - config_version: 0.5.0
  - firstrunwizard:
    - show: 0
  - login:
    - lastLogin: 1545019099
  - settings:
    - email: will@cmdschool.org
  - user_ldap:
    - displayName: will (Tan Zhen Chao)
    - firstLoginAccomplished: 1
    - homePath:
    - lastFeatureRefresh: 1545019692
    - uid: will

注:请注意“quota”和“uid”,后面需要使用
另外,特殊情况下,也可以通过查询如下表获取Quota的大小,

select quota from owncloud.oc_accounts where user_id = "00323401-c56f11e5-a663eb63-06a7ea03"

2.3.3 同步用户数据目录

rsync -avP /var/www/owncloud/00323401-c56f11e5-a663eb63-06a7ea03/files/ nextcloud.cmdschool.org:/var/www/nextcloud/will/files/

2.3.3 配置相同的用户存储容量

ssh nextcloud.cmdschool.org sudo -u apache php /var/www/nextcloud/occ user:setting will files quota 5GB

2.3.4 扫描文件并更正数据库信息

ssh nextcloud.cmdschool.org sudo -u apache php /var/www/nextcloud/occ files:scan will

注:只扫描用户“will”的目录

2.4 批量同步用户数据

2.4.1 设置MySQL免密码登录

In ownCloud Server,

vim /etc/my.cnf

加入如下配置,

[client]
user=root
password=mysqlpwd

In ownCloud Server,

2.4.2 配置公钥认证

In ownCloud Server,

ssh-keygen

向导如下,

Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa):
Created directory '/root/.ssh'.
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
[…]

然后,使用如下命令部署公钥,

ssh-copy-id -i ~/.ssh/id_rsa.pub nextcloud.cmdschool.org

部署完毕后,使用如下命令测试,

ssh nextcloud.cmdschool.org

2.4.3 创建迁移脚本

mkdir -p ~/scripts
vim ~/scripts/rsync-data-ownCloud2netxCloud.sh

加入如下脚本,

#!/bin/bash

ownCloudRoot="/var/www/owncloud"
ownCloudData="/data/owncloudData"
nextCloudRoot="/var/www/nextcloud"
nextCloudData="/data/netxcloud-data"
nextCloudHost="nextcloud.cmdschool.org"
rsyncDataLog="/var/log/rsyncData.log"

for i in $(sudo -u apache php $ownCloudRoot"/occ" user:list | sed 's/  -//g' | cut -d":" -f1); do
        userID=$i
        loginID=`mysql -e "select ldap_dn from owncloud.oc_ldap_user_mapping where directory_uuid = '"$userID"'\G;" | grep 'ldap_dn:' | sed -e 's/ldap_dn: //g' -e 's/uid=//g' -e 's/cn=//g' | cut -d "," -f1 | tr 'a-z' 'A-Z'`
        quota=`mysql -e "select quota from owncloud.oc_accounts where user_id = '"$userID"' \G;" | grep "quota:" | sed -e 's/ //g' | cut -d":" -f2`
        if [ -z "$loginID" ]; then
                echo `date "+%Y-%m-%d %H:%M:%S"`" "$userID" cannot match username!" | tee -a $rsyncDataLog
                continue
        fi
        ssh $nextCloudHost ls -ld $nextCloudData"/"$loginID"/files/" 2> /dev/null
        if [ $? != "0" ]; then
                ssh $nextCloudHost mkdir -p $nextCloudData"/"$loginID"/files/"
                ssh $nextCloudHost chown apache:apache -R $nextCloudData"/"$loginID"/"
        fi
        rsync -avP $ownCloudData"/"$userID"/files/" $nextCloudHost:$nextCloudData"/"$loginID"/files/"
        ssh $nextCloudHost sudo -u apache php $nextCloudRoot"/occ files:scan "$loginID
        if [ ! `echo $quota | egrep -i "gb|none" | wc -l` = 0 ]; then
                ssh $nextCloudHost sudo -u apache php $nextCloudRoot"/occ user:setting "$loginID" files quota "$quota
                if [ $? = "0" ]; then
                        echo `date "+%Y-%m-%d %H:%M:%S"`" ""$loginID quota has been changed to $quota" | tee -a $rsyncDataLog
                fi
        fi
        #exit
done

需要特别说明的是,
– 以上脚本只适用于笔者的生产环境,如果你需要使用请慎重(可能需测试和修改)
– 与手动操作不同的是,使用用户ID查询出登录ID脚本使用的是数据库查询的方式
另外,对于不活跃用户,根据用户目录同步资料可能更加靠谱,

#!/bin/bash

ownCloudRoot="/var/www/owncloud"
ownCloudData="/data/owncloudData"
nextCloudRoot="/var/www/nextcloud"
nextCloudData="/data/netxcloud-data"
nextCloudHost="nextcloud.cmdschool.org"
rsyncDataLog="/var/log/rsyncData.log"

cd $ownCloudData
for i in $(ls -d *-*-*-*); do
        userID=$i
        loginID=`mysql -e "select ldap_dn from owncloud.oc_ldap_user_mapping where directory_uuid = '"$userID"'\G;" | grep 'ldap_dn:' | sed -e 's/ldap_dn: //g' -e 's/uid=//g' -e 's/cn=//g' | cut -d "," -f1 | tr 'a-z' 'A-Z'`
        quota=`mysql -e "select quota from owncloud.oc_accounts where user_id = '"$userID"' \G;" | grep "quota:" | sed -e 's/ //g' | cut -d":" -f2`
        if [ -z "$loginID" ]; then
                echo `date "+%Y-%m-%d %H:%M:%S"`" "$userID" cannot match username!" | tee -a $rsyncDataLog
                continue
        fi
        ssh $nextCloudHost ls -ld $nextCloudData"/"$loginID"/files/" 2> /dev/null
        if [ $? != "0" ]; then
                ssh $nextCloudHost mkdir -p $nextCloudData"/"$loginID"/files/"
                ssh $nextCloudHost chown apache:apache -R $nextCloudData"/"$loginID"/"
        fi
        rsync -avP $ownCloudData"/"$userID"/files/" $nextCloudHost:$nextCloudData"/"$loginID"/files/"
        ssh $nextCloudHost sudo -u apache php $nextCloudRoot"/occ files:scan "$loginID
        if [ ! `echo $quota | egrep -i "gb|none" | wc -l` = 0 ]; then
                ssh $nextCloudHost sudo -u apache php $nextCloudRoot"/occ user:setting "$loginID" files quota "$quota
                if [ $? = "0" ]; then
                        echo `date "+%Y-%m-%d %H:%M:%S"`" ""$loginID quota has been changed to $quota" | tee -a $rsyncDataLog
                fi
        fi
        #exit
done

2.4.4 执行数据同步

sh ~/scripts/rsync-data-ownCloud2netxCloud.sh
没有评论

发表评论

Bash
如何快速统计网站应用?

1 前言 一个问题,一篇文章,一出故事。 笔者生产环境需要统计站点应用,由于应用数量巨大,于是通过如 …

Cloud storage
如何解决NextCloud文件锁定问题?

1 前言 一个问题,一篇文章,一出故事。 笔者生产环境nextCloud遇到用户上传文件锁定,于是整 …

Bash
如何熟悉Linux经典功能shell?

1 前言 一个问题,一篇文章,一出故事。 笔者想用一篇文章收集记录经典的脚本命令集合,于是产生此文( …