From 351f9b0436306281916820c3ec7f46ce5f14fe27 Mon Sep 17 00:00:00 2001
From: Christian Heller <c.heller@plomlompom.de>
Date: Wed, 12 Feb 2020 23:39:44 +0100
Subject: [PATCH] Add PeerTube migration scripts.

---
 buster/setup_scripts/backup_peertube.sh  | 13 +++++++++++++
 buster/setup_scripts/backup_pleroma.sh   |  1 +
 buster/setup_scripts/migrate_peertube.sh | 23 +++++++++++++++++++++++
 buster/setup_scripts/restore_peertube.sh | 15 +++++++++++++++
 buster/setup_scripts/setup_peertube.sh   |  2 +-
 5 files changed, 53 insertions(+), 1 deletion(-)
 create mode 100644 buster/setup_scripts/backup_peertube.sh
 create mode 100644 buster/setup_scripts/migrate_peertube.sh
 create mode 100644 buster/setup_scripts/restore_peertube.sh

diff --git a/buster/setup_scripts/backup_peertube.sh b/buster/setup_scripts/backup_peertube.sh
new file mode 100644
index 0000000..c787c75
--- /dev/null
+++ b/buster/setup_scripts/backup_peertube.sh
@@ -0,0 +1,13 @@
+#!/bin/sh
+set -e
+
+service peertube stop
+cd /tmp
+mkdir -p peertube_backup
+su peertube -c 'pg_dump -Fc peertube_prod' > peertube_backup/peertube_prod-dump.db
+cp -a /var/www/peertube/storage peertube_backup/storage
+cp -a /var/www/peertube/config peertube_backup/config
+tar cf peertube_backup.tar peertube_backup
+chown plom:plom peertube_backup.tar
+mv peertube_backup.tar /home/plom/
+service peertube start
diff --git a/buster/setup_scripts/backup_pleroma.sh b/buster/setup_scripts/backup_pleroma.sh
index fadbb5f..31b2718 100755
--- a/buster/setup_scripts/backup_pleroma.sh
+++ b/buster/setup_scripts/backup_pleroma.sh
@@ -11,6 +11,7 @@ mv pleroma.pgdump pleroma_backup
 cp -a /var/lib/pleroma/uploads pleroma_backup/uploads
 cp -a /etc/pleroma pleroma_backup/etc_files
 tar cf pleroma_backup.tar pleroma_backup
+rm -rf pleroma_backup
 chown plom:plom pleroma_backup.tar
 mv pleroma_backup.tar /home/plom/
 service pleroma start
diff --git a/buster/setup_scripts/migrate_peertube.sh b/buster/setup_scripts/migrate_peertube.sh
new file mode 100644
index 0000000..604920a
--- /dev/null
+++ b/buster/setup_scripts/migrate_peertube.sh
@@ -0,0 +1,23 @@
+#!/bin/sh
+set -e
+
+# Heavily inspired by <https://docs.pleroma.social/backup.html>
+
+if [ "$#" -ne 1 ]; then
+    echo 'Need server IP as argument.'
+    false
+fi
+server_ip="$1"
+
+config_tree_prefix="${HOME}/config/buster"
+setup_scripts_dir="${config_tree_prefix}/setup_scripts"
+
+cd "${setup_scripts_dir}"
+./prepare_to_meet_server.sh "${server_ip}"
+read -p'Hit Enter when you are done.' ignore
+eval $(ssh-agent) && ssh-add
+echo 'Enter password for root on target server next.'
+ssh plom@"${server_ip}" 'su -lc "cd config/buster/setup_scripts && git pull && ./backup_peertube.sh"'
+scp plom@"${server_ip}":~/peertube_backup.tar /home/plom/peertube_backup.tar
+./restore_peertube.sh
+
diff --git a/buster/setup_scripts/restore_peertube.sh b/buster/setup_scripts/restore_peertube.sh
new file mode 100644
index 0000000..dad8649
--- /dev/null
+++ b/buster/setup_scripts/restore_peertube.sh
@@ -0,0 +1,15 @@
+#!/bin/sh
+set -e
+
+service peertube stop
+mv /home/plom/peertube_backup.tar /tmp/
+cd /tmp
+tar xf peertube_backup.tar
+cp -a peertube_backup/storage/* /var/www/peertube/storage/
+cp -a peertube_backup/config/* /var/www/peertube/config/
+su postgres -c 'pg_restore -c -C -d postgres peertube_backup/peertube_prod-dump.db'
+
+db_pw=$(cat /var/www/peertube/config/production.yaml | grep password | head -1 | sed "s/[ ]*password\: *//g" | sed "s/'//g")
+su postgres -lc "psql -c \"ALTER USER peertube WITH PASSWORD '${db_pw}';\""
+
+service peertube start
diff --git a/buster/setup_scripts/setup_peertube.sh b/buster/setup_scripts/setup_peertube.sh
index f57ec40..2465cfe 100755
--- a/buster/setup_scripts/setup_peertube.sh
+++ b/buster/setup_scripts/setup_peertube.sh
@@ -1,6 +1,6 @@
 #!/bin/sh
 set -e
-set -x
+
 # Heavily inspired by
 # <https://github.com/Chocobozzz/PeerTube/blob/develop/support/doc/production.md>
 # and
-- 
2.30.2