]> Untitled Git - lemmy.git/commitdiff
wip: Add migration script from pictshare to pictrs
authorFelix Ableitner <me@nutomic.com>
Mon, 15 Jun 2020 17:46:57 +0000 (19:46 +0200)
committerFelix Ableitner <me@nutomic.com>
Mon, 15 Jun 2020 17:46:57 +0000 (19:46 +0200)
ansible/lemmy.yml
ansible/lemmy_dev.yml
docker/prod/migrate-pictshare-to-pictrs.bash [new file with mode: 0644]

index 0b49856aca8eb50c09963caea3f09340688d1470..7b78ab8d35fbd6ae4be115d99d6ac66d93a6401c 100644 (file)
@@ -60,6 +60,7 @@
       project_src: /lemmy/
       state: present
       pull: yes
+      remove_orphans: yes
 
   - name: reload nginx with new config
     shell: nginx -s reload
index 05eb1ffed19fe5216d47c573f721974e9f479073..7a3683610ec04a3098cb045b3ef52aabcf7be694 100644 (file)
@@ -89,6 +89,7 @@
       project_src: /lemmy/
       state: present
       recreate: always
+      remove_orphans: yes
     ignore_errors: yes
 
   - name: reload nginx with new config
diff --git a/docker/prod/migrate-pictshare-to-pictrs.bash b/docker/prod/migrate-pictshare-to-pictrs.bash
new file mode 100644 (file)
index 0000000..14ec07f
--- /dev/null
@@ -0,0 +1,70 @@
+#!/bin/bash
+set -e
+
+if [[ $(id -u) != 0 ]]; then 
+    echo "This migration needs to be run as root"
+    exit
+fi
+
+if [[ ! -f docker-compose.yml ]]; then 
+    echo "No docker-compose.yml found in current directory. Is this the right folder?"
+    exit
+fi
+
+echo "Restarting docker-compose, making sure that pictrs is started and pictshare is removed"
+docker-compose up -d --remove-orphans
+
+if [[ -z $(docker-compose ps | grep pictrs) ]]; then
+    echo "Pict-rs is not running, make sure you update Lemmy first"
+    exit
+fi
+
+echo "Installing imagemagick to convert .webp images to .jpg"
+apt install imagemagick -y
+
+echo "Stopping Lemmy so that users dont upload new images during the migration"
+docker-compose stop lemmy
+
+echo "Importing pictshare images to pict-rs"
+pushd volumes/pictshare/
+IMAGE_NAMES=*
+for image in $IMAGE_NAMES; do
+    IMAGE_PATH="$(pwd)/$image/$image"
+    if [[ ! -f $IMAGE_PATH ]]; then
+        continue
+    fi
+    if [ ${IMAGE_PATH: -5} == ".webp" ]; then
+        NEW_IMAGE_PATH=$(echo "$IMAGE_PATH" | sed "s/\.webp$/\.jpg/g")
+        convert "$IMAGE_PATH" "$NEW_IMAGE_PATH"
+        IMAGE_PATH="$NEW_IMAGE_PATH"
+        continue
+    fi
+    echo -e "\nImporting $IMAGE_PATH"
+    ret=0
+    curl --fail -F "images[]=@$IMAGE_PATH" http://127.0.0.1:8537/import || ret=$?
+    if [[ $ret != 0 ]]; then
+        read -p "Failed to import $IMAGE_PATH, continue? " yn
+        case $yn in
+            [Yy]* ) ;;
+            [Nn]* ) exit;;
+            * ) exit;;
+        esac
+    fi
+done
+
+echo "Fixing permissions on pictshare folder"
+find . -type d -exec chmod 755 {} \;
+find . -type f -exec chmod 644 {} \;
+
+popd
+
+echo "Rewrite image links in Lemmy database"
+docker-compose exec -u  postgres postgres psql -U lemmy -c "UPDATE user_ SET avatar = REPLACE(avatar, 'pictshare', 'pictrs/image') WHERE avatar is not null;"
+docker-compose exec -u  postgres postgres psql -U lemmy -c "UPDATE post SET url = REPLACE(url, 'pictshare', 'pictrs/image') WHERE url is not null;"
+
+echo "Moving pictshare data folder to pictshare_backup"
+mv volumes/pictshare volumes/pictshare_backup
+
+echo "Migration done, starting Lemmy again"
+echo "If everything went well, you can delete ./volumes/pictshare_backup/ and uninstall imagemagick"
+docker-compose start lemmy