Most solutions here either print only environment variables, or have the drawback that env or (set -o posix; posixset) do not print values in easily parseable form (try to print variable A=$'a\r\nb', it has multiple lines...).
Here is a function that will print all variables, one variable per line, in the POSIX escaped form (works properly for simple text variables, does not work for arrays):
function dump_vars {
local VARNAME
compgen -v | while read -r VARNAME; do
printf "$VARNAME=%q\n" "${!VARNAME}"
done
}
Thanks to @tmgoblin for pointing out the use of compgen -v.