Restoring Synapse from backup: python[6558]: ModuleNotFoundError: No module named 'attrs'

Ok, I fixed my problem and was able to restore my synapse successfully. Likely for someone with a similar problem to mine, they may have a slightly different set of broken libraries, but the process is the same. Any exceptions due to failure to load libraries due to import issues, can be fixed by deleting the offending library from the tar file, installing it locally, then copying the locally installed library into the tar package. May have to do this a few times until there are no more library exceptions during the restore. Below commands summarize it:
After logging in through ssh as the admin user:

# locally install libraries that had errors (goes to /home/admin/.local/lib/python3/site-packages)
pip install -U attr attrs cryptography pyopenssl
# create a temp working directory
mkdir ~/tmp
cd ~/tmp
# extract the restore file
tar xvf /home/yunohost.backup/archives/synapse-pre-upgrade2.tar
# become root for the next few steps since files in the extracted tar are not all accessible by `admin` user
sudo su
# remove offending libraries from extracted tar
cd apps/synapse/backup/opt/yunohost/matrix-synapse/lib/python3.7/site-packages
rm -rf attr attr.py attr-* attrs* cryptography* OpenSSL pyOpenSSL*
# copy over the local libraries, replacing the ones that were removed
cp -r /home/admin/.local/lib/python3.7/site-packages/attr .
cp -r /home/admin/.local/lib/python3.7/site-packages/attr.py .
cp -r /home/admin/.local/lib/python3.7/site-packages/attr-* .
cp -r /home/admin/.local/lib/python3.7/site-packages/attrs* .
cp -r /home/admin/.local/lib/python3.7/site-packages/cryptography* .
cp -r /home/admin/.local/lib/python3.7/site-packages/OpenSSL .
cp -r /home/admin/.local/lib/python3.7/site-packages/pyOpenSSL* .
# make sure ownership of freshly copied files matches ownership of other files in the directory
ls -altr
chown -R admin:1007 attr attr.py attr-* attrs* cryptography* OpenSSL pyOpenSSL*
# leave `root` user and return to `admin` user. Should be back in the /home/admin/tmp folder
exit
# save off the original restore file as a backup
mv /home/yunohost.backup/archive/synapse-pre-upgrade2.tar /home/yunohost.backup/archive/synapse-pre-upgrade2.tar-orig
# tar the files from the `tmp` folder into a new restore tar file with the same name and path as the original. The `--transform` argument makes sure the paths inside the tar file are like what was in the original
tar --transform 's|^\./||' -cvf /home/yunohost.backup/archive/synapse-pre-upgrade2.tar .
# finally, re-attempt the restore operation
sudo yunohost backup restore synapse-pre-upgrade2 --apps
2 Likes