Using diff (or rsync) to compare folders over SSH on two different servers
So I wanted to find a quick code that will allow me to a diff over two folders as I want to be sure all files get transfered as we are using Plesk migrator to move domains from one server to another. Found a Serverfault post titled How do diff over ssh? but most of the suggestions are for files and not folders and none that work recursively. Well using this answer from Server Fault and some man pages I have the following
diff <(ssh server1 'sudo ls -1aR /var/www/vhosts/domain.com') <(ssh server2 'sudo ls -1aR /var/www/vhosts/domain.com')
Update:
So a lot of people in the ServerFault page I linked to mentioned rync with the –dry-run flag. I did not look in to this much but just as I posted this blog the first @reply i got on twitter was from @EvanHoffman asking if I had looked at using rsync. Ok so I thought I should revisited the problem and using rsync and I got the follow:
rsync --dry-run -rvce "ssh -p port#" user@server1:/var/www/vhosts/ /var/www/vhosts/domain.com/
(I have not tested the rsync solution with a live box but from testing on my local system I assume this would work)
One of the limitation of this you need to be on one of the two servers vs the solutions using diff is something you do on your computer that can SSH to both systems.





Stainless 9:38 am on January 12, 2012 Permalink
I think the first command works, the second I tried and it works.But there is no example on the net that I can find that diffs a local directory and a remote directory .So I wrote a script and it just compare the file count, you can change it to do the ` du ` and get the file size.:
#!/bin/bash
LOCALFOLDER=/var/www/tee
REMOTEFOLDER=$(ssh root@111.111.111.1 ‘ls -lA /hfs/tee’| grep -E ‘^total’ | cut -d ” ” -f 2 > remotessh.txt)
COMMAND=$(ls -lA $LOCALFOLDER | grep -E ‘^total’ | cut -d ” ” -f 2 > localssh.txt)
REM=$(cat remotessh.txt)
LOCAL=$(cat localssh.txt)
echo $LOCAL
echo $REM
if [ $REM -eq $LOCAL ]
then
echo Directories are the same
else
echo Directories are differnt
fi