Backing Up WordPress Site Over SSH

Paste the following into terminal while connected over SSH.

backup() {
    if [ -z "$1" ]; then echo "Please enter a folder: backup <folder>"; return 1; fi
	folder=$1
	folder=${folder%/}
	today=$(date +"%Y-%m-%d")
	random=$( cat /dev/urandom | tr -dc 'a-zA-Z0-9' | head -c 7 )
	backup_file="${today}_${random}.zip"
	cd $folder
	home_url=$( wp option get home --skip-plugins --skip-themes )
	name=$( wp option get blogname --skip-plugins --skip-themes )
	database_file=$( wp db export --add-drop-table --default-character-set=utf8mb4 --porcelain)
    if [ -f "../wp-config.php" ]; then zip -r "../$backup_file" ../wp-config.php; fi
	cd ..
	zip -r "$backup_file" $folder
	size=$(du -sh "$backup_file" | cut -f1)
	rm -f ${folder}/$database_file
	mv "$backup_file" $folder
	echo "Backup ready ($size): ${home_url}/${backup_file} ($name)"
	echo "When done remember to: rm -f ${folder}/${backup_file}"
}

Now run backup <folder-name>/. Make sure to target the folder where WordPress lives like backup public/ or backup htdocs/. This will generate a full backup with a link to download it.