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