Backing up your VPS is an essential strategy providing the safety of your data regardless of circumstances. If you don’t have a large website with huge amounts of content and want to save some money on third-party backup services, you can consider backing up your VPS directly on your home laptop. However, doing everything by hand can end up being a tedious task, that you will tend to forget about once in a while, so you can eventually find yourself in a situation when restoring your data is emergently needed, but the last backup was done a month ago…
To exclude situations of this kind, it would be great to have a script that would allow you to perform all the necessary actions within a few clicks.
Also, it doesn’t make much sense to back up the whole amount of data. Instead, it would be much more ergonomic to organize a differential backing-up policy, that is to backup only the lately changed and added files.
To help you find the proper solution to the problems mentioned above, we’ve prepared this guide.
Backing up your VPS on your home laptop
To back up your Linux VPS on your home laptop with Windows 10 OS, you’ll, first of all, need Ubuntu Bash Linux shell. About its installation, you can learn more here.
Once you have installed Ubuntu Bash, make sure:
- You can access your VPS through SSH;
- You have enough storage space on your laptop to fit the whole amount of your website’s data;
- You have an SQL user with access to each of the databases on your VPS.
After it, you can move on to the configuration of your backups.
The first step is to take care of passwordless SSH access from your laptop to your VPS.
Your laptop will have to access the VPS several times, so you won’t be able to automize it without passwordless access.
First, open your command prompt and type bash to start Ubuntu bash shell. Then, install Ubuntu rsync package with apt-get install rsync command.
The next step is creating a key pair for passwordless login. Use the ssh-keygen -t rsa command. You’ll be asked for a passphrase. Just press enter, thus setting a blank passphrase.
Now you need to create an SSH directory under your home directory on the web server. Use the command:
ssh -p 22 [email protected] mkdir .ssh
If necessary, change the -p 22 according to your port.
If the directory already existed before, you’ll get the message: mkdir: cannot create directory ‘.ssh’: File exists
In this case, just go on.
Now, upload the public key created above and make them accessible to your user only:
cat ~/.ssh/myserver.pub | ssh -p 22 [email protected] ‘cat >> ~/.ssh/authorized_keys’
ssh -p 3516 [email protected] “chmod 640 ~/.ssh/authorized_keys”
Let’s create a config file to make things easier:
~/.ssh/config
Add the following configuration to it:
Host yourdomain.com
HostName yourdomain.com
Port 22
User Username
IdentityFile ~/.ssh/myserver
Finally, it only remains to issue these two commands:
chmod 600 ~/.ssh/config
chmod 600 ~/.ssh/myserver
If everything is alright, the ssh yourdomain.com should immediately log in to your server.
Now we can move on to creating a script that will copy the databases from the server and then incrementally update them with new files.
First, change the directory to the category you want to keep your backups in (here we’ve chosen the home directory):
/mnt/c#cd ~
~#
Create a sub-directory for the backup script, where, in turn, you should create a subfolder for the backups themselves:
~#mkdir my_server_backups
~#cd my_server_backups
~/my_server_backups#mkdir my_server_files
~/my_server_backups#mkdir my_server_files/sql_backups
~/my_server_backups#mkdir my_server_files/website_backups
(The names in bold can be changed for the names of your choice).
Create a text file called backup_my_server.sh and fill it with the following script:
#!/bin/bash
###############################################################
# User Variables #
###############################################################
local_website_backup_path=”./website_backups”
local_database_backup_path=”./sql_backups”
local_database_dump_file=”all-databases”
server_domain_name=”yourdomain.com”
server_ssh_username=”Username”
server_ssh_port=22
server_html_directories_path=”/var/www/html”
server_sql_admin_name=”sql_admin_user”
server_sql_admin_pass=”sql_admin_password”
###############################################################
###############################################################
# Remote Commands #
###############################################################
server_sql_dump_command=”mysqldump -u’$server_sql_admin_name’ -p’$server_sql_admin_pass’ –all-databases”
###############################################################
###############################################################
# Generic #
###############################################################
timestamp=`date +”_%d-%m-%Y_%H%M”`
sql_ext=”.sql”
database_dump_file=$local_database_dump_file$timestamp$sql_ext
###############################################################
clear
printf “Backup Start time : $(date)\n” > last_backup_log.txt
printf “SQL Dump stored in : $database_dump_file\n” | tee -a last_backup_log.txt
printf “Starting Database backup…\n” | tee -a last_backup_log.txt
ssh -p$server_ssh_port $server_ssh_username@$server_domain_name $server_sql_dump_command > $local_database_backup_path/$database_dump_file | tee -a last_backup_log.txt
printf “Database Backup and transfer finished $(date)”
printf “\n”
printf “Starting differential backup of the HTML Directories $(date)\n” | tee -a last_backup_log.txt
rsync -avz -e “ssh -p$server_ssh_port” $server_ssh_username@$server_domain_name:$server_html_directories_path $local_website_backup_path | tee -a last_backup_log.txt
echo “Server backup Complete”
printf “\nBackup End time:$(date)\n” | tee -a last_backup_log.txt
Save the file and make it executable with the command:
~/my_server_backups#chmod 770 backup_my_server.sh
Now you can run it with the command:
~/my_server_backups#./backup_my_server.sh
If everything went well, the in a few minutes you’ll see the message “Database Backup and transfer finished”. Congratulations, you’ve just set up the backing-up VPS on your home laptop. Later on, the backups will be performed within just a few seconds, as only newly created files will be added.
Conclusion
You’ve learned how to make backing up your VPS on your home laptop as convenient as possible, so we hope that from now on you’ll have no problems losing your precious data. If you, however, are in search of a VPS for your website, you can read more here. Thank you for your attention and have a nice day!