From: Christian Heller Date: Fri, 14 Feb 2020 22:13:52 +0000 (+0100) Subject: Experimental refactor of migration scripts. X-Git-Url: https://plomlompom.com/repos/%7B%7Bprefix%7D%7D/static/%7B%7B%20web_path%20%7D%7D/%7B%7Bdb.prefix%7D%7D/edit?a=commitdiff_plain;h=19bd8ce75275c6d6d737dbdf78ab3d969369b19f;p=config Experimental refactor of migration scripts. --- diff --git a/buster/setup_scripts/backup_app.sh b/buster/setup_scripts/backup_app.sh new file mode 100755 index 0000000..ada3eb1 --- /dev/null +++ b/buster/setup_scripts/backup_app.sh @@ -0,0 +1,35 @@ +#!/bin/sh +set -e +set -x + +if [ "$#" -lt 3 ]; then + echo 'Need at least three arguments: service name, DB name, and backup directory names.' + #echo 'Need at least two arguments: service name and backup directory or (prefixed with "db:") DB names.' + false +fi +app="$1" +db_name="$2" +shift 2 + +cd /tmp +rm -rf "/tmp/${app}_backup" + +app "${app}" stop + +su postgres -lc "pg_dump -d ${db_name} --format=custom -f ${app}_backup/pgdump" +for target in "$@"; do + cp -a "${target}" "${app}_backup${target}" +done +#for target in "$@"; do +# prefix=$(echo "${target}" | cut -c-3) +# if [ "${prefix}" = "DB:" ]; then +# db_name=$(echo "${ŧarget}" | cut -c4-) +# else +# cp -a "${target}" "backup${target}" +# fi +#done + +tar cf "${app}_backup.tar" "${app}_backup" +rm -rf "${app}_backup" +chown plom:plom "${app}_backup.tar" +mv "${app}_backup.tar" /home/plom diff --git a/buster/setup_scripts/migrate_app.sh b/buster/setup_scripts/migrate_app.sh new file mode 100755 index 0000000..c0e1637 --- /dev/null +++ b/buster/setup_scripts/migrate_app.sh @@ -0,0 +1,28 @@ +#!/bin/sh +set -e +set -x + +if [ "$#" -lt 4 ]; then + echo 'Need at least four arguments: old server IP, service name, DB name, and backup directory names.' + false +fi +if [ ! "$1" = "pleroma" ] && [ ! "$1" = "peertube" ]; then + echo "Need legal service name (pleroma or peertube)." + false +fi +server_ip="$1" +app="$2" +db_name="$3" +shift 3 + +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_${app}.sh ${app} ${db_name} $@"' +scp plom@"${server_ip}":~/${app}_backup.tar /home/plom/${app}_backup.tar +./restore_${app}.sh diff --git a/buster/setup_scripts/restore_app.sh b/buster/setup_scripts/restore_app.sh new file mode 100755 index 0000000..cb30788 --- /dev/null +++ b/buster/setup_scripts/restore_app.sh @@ -0,0 +1,42 @@ +#!/bin/sh +set -e +set -x + +if [ "$#" -lt 2 ]; then + echo 'Need two arguments: service name and DB name.' + false +fi +if [ ! "$1" = "pleroma" ] && [ ! "$1" = "peertube" ]; then + echo "Need legal service name (pleroma or peertube)." + false +fi +app="$1" +db_name="$2" + +service "${app}" stop + +mv "/home/plom/${app}_backup.tar" /tmp/ +cd /tmp +tar xf "${app}_backup.tar" + +su postgres -c "pg_restore -c -C -d ${db_name} -1 ${app}_backup/${db_name}.pgdump" +rm "${app}_backup/${db_name}.pgdump" + +cd "${app}_backup" +for path in $/find . -type f); do + target_path=$(echo "${path}" | cut -c2-) + source_path=$(realpath "${path}") + dir=$(dirname "${target_path}") + mkdir -p "${dir}" + cp -a "${source_path}" "${target_path}" +done + +# TODO: Horrible hack, improve. +if [ "${app}" = "pleroma" ]; then + db_pw=$(cat /etc/pleroma/config.exs | grep password | sed 's/[ ]*password\: *//g' | sed 's/,//g' | sed 's/"//g') +elif [ "${app}" = "peertube" ]; then + db_pw=$(cat /var/www/peertube/config/production.yaml | grep password | head -1 | sed "s/[ ]*password\: *//g" | sed "s/'//g") +fi +su postgres -lc "psql -c \"ALTER USER ${app} WITH PASSWORD '${db_pw}';\"" + +service "${app}" stop