Ubuntu – How to remote backup a Linux/Ubuntu server from a Windows client

backupUbuntu

I have an Ubuntu 18.04 VPS on a hosting company.

I manage it via web panel and SSH from my Windows 10 client.

I would like to full remote backup my VPS for possible future restore.

How can I do from a Win client ?

Thanks

Best Answer

Basics

In most cases, you have 3 things to backup:

  1. Files uploaded by your VPS clients (i.e. Media files in Wordpress)
  2. Your database(s)
  3. The Linux setup (a.k.a. files under /etc)

Anything else is static data and yous should not have to back it up. (i.e. you can recover it by reinstalling the system and your tools/software)

Local Backup with All Properties

Assuming you have enough space on your VPS hard drive, you can first create tarballs of your data, as you see fit. For example, I create one per database so I can restore that specific database and have one large file with everything in it.

# for files
tar cf - /path/to/folder/to/backup | xz -9 > /home/backups/my-files.tar.xz

# for databases
mysqldump db_name | xz -9 > /home/backups/my-database.sql.xz

Place those in a folder (/home/backups in my example here; Ubuntu has /var/backups, for example) and then with SSH (or SFTP) do a transfer.

scp vps:/home/backups/my-files.tar.xz C:\Backups
scp vps:/home/backups/my-database.sql.xz C:\Backups

Multiple Backup Files

Further, you can (probably want to) automate everything. In that case I would strongly suggest that you look into using a date in the filename:

DAY_OF_WEEK=`date + %u`
tar cf - /path/to/folder/to/backup \
             | xz -9 > /home/backups/my-files-${DAY_OF_WEEK}.tar.xz

This way you get 7 different files and when you do the transfer you can choose the correct file each time. Also it gives you a few days to detect a problem. You can of course use %d, but remember that uses a lot of space on your VPS. Make sure that if you increase the number of backups you will not overflow your available disk space.

Note that the tar utility and mysqldump will save all the necessary information to restore the files as required.

Transfer

Finally, to do the transfer. You can use SSH or, to make sure you get the latest version, you could use cwRsync. This will get you the files that have changed without you having to do much.

rsync <options> C:\Backups vps:/home/backups

Check the rsync docs for the proper . There are many of them. Test well to make sure you get them right.

Note: I use rsync between my Linux boxes, with compression, it works well. However, it would probably not work well between Linux and MS-Windows (Users Names, Permissions, etc.). This is why I suggest you first create tarballs and database dumps as mentioned above. It's a good idea to have tarballs anyway because they can be well compressed. Note that for databases, a dump is the easiest way to make sure you get a correct snapshot.

Security

When automating, you need a key without a password. Otherwise you'll have to be at your computer whenever the rsync or SSH transfer starts... not practical.

In order to allow for such, I create a special user that will have his own account (a.k.a. /home/backups, user is named backups... hint hint!) I create a key without a password and that user has minimal rights on the Linux box. Mainly, it can only read data... For sure, no sudo or other such advanced permissions.

The backup files can even be created under /home/backups with a user such as root and thus can't be tempered by backups, only read for the transfer. That way you know that those files are safe.

Final Step

Now it looks like you have a fully automated backup...

Well! Please make sure that the files look correct. Even, if you have time, attempt a restore. It's going to be manual, but hopefully you'll never need those backups. (I rarely need mine and I like it that way!)

Replication for Backing Up Systems

You may also be interested in a more advance topic: replication. This is where you create a VPS second instance and get all the data duplicated between both computers. I know of Hadoop, for example. However, I'm not so sure that it would work well between Linux & MS-Windows, hence the second VPS as a backup... Some database systems come with such replication, like Cassandra. PostgreSQL and MySQL also have replication mechanism that can be used for backing up your data. Those would work between a Linux VPS and MS-Windows. However, it probably requires your MS-Windows computer to be up and running at all time.

So, with Hadoop replicating your folders and setting your database with replication, you could also achieve your replication. It's just much more work to make it all work as expected... (and you have other potential security issues arising.)

Related Topic