Backing up a Server Remotely Using Minimal Bandwidth

The server that runs this website (and a lot of others) also runs a lot of other services such as an IRC server, a Jabber server, NGINX (of course), and various other things. I like to take a lot of backups, especially since I’m not the best person in the area of security. With that, my old way of backing my my server was relatively painful. I had a script that tarred and compressed each service directory individually and move it to a secure location on my web server for download. After download, the script would remove the backup, and continue to the next.

The problem with this method is that it consumes a lot of bandwidth and time. By the time I have downloaded everything, I have used up several gigabytes of bandwidth. I don’t mind so much about the bandwidth though. What’s important is the time and interraction it takes.

Enter the Light Bulb…​

I’ve been using rsync for some time now to mirror my laptop to my server at home. For some reason, it never occurred to me to use rsync with a private key to log in to my server and download the deltas to my local machine. If I want a single compressed tar file for a backup, all I have to do is backup my local server’s copy of everything rather than doing it on my web server and downloading that. Ending this already too long blog post on this simple topic, here’s the rsync command I’m using…​

sync -avP --delete --chmod=g+rx --rsh="ssh -p1234 -i ~/.ssh/id_rsa.pdeb.user" user@server.net:/dir1 /home/server/dir1

A Quick Explanation

  • rsync -avP uses default rsync settings (-a), specifies verbose mode (-v) and sets rsync to display its progress on each individual file as it goes (-P).

  • --delete option, rsync will delete files on the destination if they deleted on the source (this isn’t default).

  • --chmod=g+rx sets the group settings on the destination to group with read and write. This is handy if you want to access the backups from another account that doesn’t have access on the server too. This switch is not necessary.

  • --rsh="ssh -p1234 -i ~/.ssh/id_rsa.user" specifies a custom port to connect on (port 1234 in this case) and specifies a private key to use when attempting to log in.

  • user@server.net:dir1 /home/server/dir1 is the host (server) to connect to along with the user to try (user), the source directory (:/dir) and the destination directory (/home/server/dir1).

Category:Linux Category:Backups Category:SSH