Backing up your vps or shared hosting server on your Home Laptop or PC with Windows 10 (Part 2)

In the first part of this tutorial, we have setup a private/public key pair, and enabled password-less ssh access to our vps or shared hosting server. In the second part, we will setup a bash script to copy (dump) the contents of our databases on the server, and copy (incrementally) the website files to a local folder on our windows machine.

Open the Ubuntu bash on your Windows 10 machine, and change directory to the home directory (or to whichever directory you want to create the backups into)

C:\>bash
root@DESKTOP:/mnt/c#cd ~
root@DESKTOP:~#

create a sub-directory where the backup script will reside, and a sub-folder in it, where the actual backups will reside.

root@DESKTOP:~#mkdir my_server_backups
root@DESKTOP:~#cd my_server_backups
root@DESKTOP:~/my_server_backups#mkdir my_server_files
root@DESKTOP:~/my_server_backups#mkdir my_server_files/sql_backups
root@DESKTOP:~/my_server_backups#mkdir my_server_files/website_backups

The sections in RED above may be changed to whatever you prefer, as long as they are correctly input in the script later on.

The directory structure on our local machine will look something like this :-

 

Let’s build the script to copy the files from our server.

Make sure your current working directory is “my_server_backups” or whatever you chose to name it. Create a text file using your favourite editor and call it “backup_my_server.sh“. Paste the contents below, and modify the variables inside the “User Variables”

As you might notice, I have added the server_ssh_username and server_ssh_port, technically these are not really necessary, since we have created a ~/.ssh/config file with the necessary info. I have added these, simply to make the script more portable.

#!/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 then make it executable but issuing the command chmod 770 backup_my_server.sh

root@DESKTOP:~/my_server_backups#chmod 770 backup_my_server.sh

We are now ready to run the script for the first time. Issue the command ./backup_my_server.sh

root@DESKTOP:~/my_server_backups#./backup_my_server.sh
Starting Database backup...
Database Backup and transfer finished Fri Feb 17 15:07:03 STD 2017
Starting differential backup of the HTML Directories Fri Feb 17 15:07:03 STD 2017
receiving incremental file list

[YOU WILL SEE A LOT OF FILES BEING COPIED HERE]

sent 1,212 bytes received 199,591 bytes 7,874.63 bytes/sec
total size is 334,665,373 speedup is 1,666.64
Server backup Complete

Backup End time:Fri Feb 17 15:07:28 STD 2017
root@DESKTOP:~/my_server_backups#

During the first part, the database will be dumped and copied directly to your local machine, (this avoids using up space on the server), depending on the sizes and number of databases, this might take a few minutes. When the dump finishes, you will see the message “Database Backup and transfer finished.” followed by Starting differential backup of the HTML Directories. You will see files scrolling by as they are being copied.

Once again, depending on the number of websites, and the size of each directory, this could take a few minutes, the first time the script is run. Subsequent executions, will take much less time, since they will only copy newly generated or modified files.

You may want to delete older sql dumps to save up space, since these are time stamped and thus will not overwrite the old content.

 

 

 

1 comments On Backing up your vps or shared hosting server on your Home Laptop or PC with Windows 10 (Part 2)

Leave a reply:

Your email address will not be published.

Site Footer