]> Untitled Git - lemmy.git/blob - docker/prod/migrate-pictshare-to-pictrs.bash
ea3007a6edac71068d0881c9b08b23f471dbfb61
[lemmy.git] / docker / prod / migrate-pictshare-to-pictrs.bash
1 #!/bin/bash
2 set -e
3
4 if [[ $(id -u) != 0 ]]; then 
5     echo "This migration needs to be run as root"
6     exit
7 fi
8
9 if [[ ! -f docker-compose.yml ]]; then 
10     echo "No docker-compose.yml found in current directory. Is this the right folder?"
11     exit
12 fi
13
14 echo "Restarting docker-compose, making sure that pictrs is started and pictshare is removed"
15 docker-compose up -d --remove-orphans
16
17 if [[ -z $(docker-compose ps | grep pictrs) ]]; then
18     echo "Pict-rs is not running, make sure you update Lemmy first"
19     exit
20 fi
21
22 if [[ -z $(type -P convert) ]]; then
23   echo "Installing imagemagick to convert .webp images to .jpg"
24   apt install imagemagick -y
25 else 
26   echo "Imagemagick already installed."
27 fi
28
29 echo "Stopping Lemmy so that users dont upload new images during the migration"
30 docker-compose stop lemmy
31
32 echo "Importing pictshare images to pict-rs"
33 pushd volumes/pictshare/
34 IMAGE_NAMES=*
35 for image in $IMAGE_NAMES; do
36     IMAGE_PATH="$(pwd)/$image/$image"
37     if [[ ! -f $IMAGE_PATH ]]; then
38         continue
39     fi
40     if [ ${IMAGE_PATH: -5} == ".webp" ]; then
41         NEW_IMAGE_PATH=$(echo "$IMAGE_PATH" | sed "s/\.webp$/\.jpg/g")
42         convert "$IMAGE_PATH" "$NEW_IMAGE_PATH"
43         IMAGE_PATH="$NEW_IMAGE_PATH"
44         continue
45     fi
46     echo -e "\nImporting $IMAGE_PATH"
47     ret=0
48     curl --fail -F "images[]=@$IMAGE_PATH" http://127.0.0.1:8537/import || ret=$?
49     if [[ $ret != 0 ]]; then
50         read -p "Failed to import $IMAGE_PATH, continue? " yn
51         case $yn in
52             [Yy]* ) ;;
53             [Nn]* ) exit;;
54             * ) exit;;
55         esac
56     fi
57 done
58
59 echo "Fixing permissions on pictshare folder"
60 find . -type d -exec chmod 755 {} \;
61 find . -type f -exec chmod 644 {} \;
62
63 popd
64
65 echo "Rewrite image links in Lemmy database"
66 docker-compose exec -u  postgres postgres psql -U lemmy -c "UPDATE user_ SET avatar = REPLACE(avatar, 'pictshare', 'pictrs/image') WHERE avatar is not null;"
67 docker-compose exec -u  postgres postgres psql -U lemmy -c "UPDATE post SET url = REPLACE(url, 'pictshare', 'pictrs/image') WHERE url is not null;"
68
69 echo "Moving pictshare data folder to pictshare_backup"
70 mv volumes/pictshare volumes/pictshare_backup
71
72 echo "Migration done, starting Lemmy again"
73 echo "If everything went well, you can delete ./volumes/pictshare_backup/ and uninstall imagemagick"
74 docker-compose start lemmy