How To Back Up Your Files In Remote Server

In my days as an ALX SE student, we were given servers to help us understand the concepts of MYSQL, FIREWALL, LOAD BALANCER, and the appreciation of servers like nginx, Haproxy and Apache.

For these projects and tasks, we had to install these frameworks on these servers for learning purposes.

THE EMAIL

After the end of the foundation phase, we received an email that our servers will be destroyed and that we need to back up the necessary files we will need. These files/folders need to be backed up so that we do not have to go through the long process of installing these services all over again on our new server.

THE PANIC

when I got the email I thought of how I would back up these files; should I push all of them to my GitHub account or should I copy them one after another to my local machine? if I wanted to copy it one after the other It would take a lot of time. So I made some research on how I could back up folders in chunks from my remote server.

The Solutions: Tar command

My research on how to back up my files yielded a positive result and I came across the tar command. The tar command is used for archiving files and directories into a single file. It is short for Tape Archive. In this article, I won't explain extensively on how to use the tar command but you can read more about the tar command.

I noticed that most of the folders I needed to back up were in the /etc/ directory. Knowing about the tar command, I finally decided to archive the folders and files that I needed from my server into a single file and back it up on my local machine. then I logged into my remote server and archived the files I needed. The syntax for using the tar command is tar [options][archive-file] [file or dir to be archived] in my case the tar options I used were:

  1. -c which creates a new archive

  2. -f specifies the file name of the archive

  3. -z enables compression using gzip. and also reduces the file size

I backed up my nginx/ and ufw/ folders in the /etc/ directory I used the command sudo tar -cfz server_backup /etc/nginx /etc/ufw/I used sudo in this case because I wasn't in my root directory.

Transferring The Archived File to My Local Machine

This part was not particularly hard as I had previously done something similar to this in one of the tasks I was given (0x0C-web_server) in my ALX Software Engineering program. The task required me to write a bash script that transfers a file from my local machine to my server. This time I had to reverse the transfer which was transferring files from my remote server to my local machine.

Since I had three different servers I was trying to back up, I wrote a bash script to automate this process.

#!/bin/bash

if [ $# -lt 2 ]; then
  echo "Usage: $0 IP SOURCE_FILE [DESTINATION_FILE]"
  exit 1
fi

IP=$1
SOURCE_FILE=$2

if [ $# -eq 3 ]; then
  DESTINATION_FILE=$3
else
  DESTINATION_FILE=$(basename "$SOURCE_FILE")
fi

scp -o StrictHostKeyChecking=no "ubuntu@$IP:$SOURCE_FILE" "$DESTINATION_FILE"

The script accepts two or three parameters:

  • IP: The IP address of the server you want to transfer the file from.

  • SOURCE_FILE: The path to the file on the remote server.

  • [DESTINATION_FILE]: (Optional) The path where the file will be saved locally. If not provided, it will be saved in the current directory with the same name as the source file.

The script uses the scp command with the provided IP and source file to copy the file from the remote server to the local machine. The -o StrictHostKeyChecking=no option disables strict host key checking to avoid prompts for confirming the server's authenticity.

Executing The Script

To execute the script, I saved it in a file transfer_file.sh made it executable usingchmod u+x transfer_file.sh and run it with the appropriate parameters. For example:

./transfer_file.sh 100.25.151.94 ~/server_backup.tar.gz ~/server_backup.tar.gz

And voila! That was how I was able to back up my files on my remote server to my local machine.

Conclusion

The importance of backing up files cannot be overemphasized, whether it is on your remote server or your local machine. As a software Engineer, you should not only know how to back up your files but also organise them well so that they can be easily retrieved in the future if need be.

If you like this Article don't forget to react or drop a comment. I am also open to corrections, as most Software Engineers and writers, I learn every day. you can also follow me on GitHub.