No Linux No Life – Backup Shellscript Sample

Backup and rsync.

Linux’s open-source nature and its possibility don’t know its limit. The playground is open and welcomes everyone with tech creativity. Have fun and enjoy your opensource journey with Linux!

Hi international friends,

It’s been a little while since I last updated my blog. So, I have recently created a simple backup script as I will need to possess some shell-scripting skills for my upcoming project. Due to my new project, which requires me to be present in the office on a daily basis (at least for the entire of April), I have been struggling to find time for myself.

In any case, it has been a rejuvenating experience to devote myself to shell-scripting once again!

The script:

#!/bin/bash

#set backup params
backup_dir="/path/to/backup_dir"
backup_file="backup_$(date +%Y%m%d).tar.gz"
remote_server="192.168.0.xxx"
remote_dir="/path/to/remote_dir"

# Create backup archive
tar -czvf "$backup_dir/$backup_file" /path/to/important/file

# Transfer backup to remote server
rsync -avz "$backup_dir/$backup_file" "$remote_server:$remote_dir"

The script itself is fairly straightforward. The first four variables serve as parameters that set the backup directory, file name, remote server IP address, and the directory on the server.

tar command:

For those who are not familiar with tar command, here’s the explanation.

In the example tar -czvf "$backup_dir/$backup_file" /path/to/important/files /path/to/other/directories, the two arguments after the archive filename ("$backup_dir/$backup_file") are actually the files and directories that will be included in the backup archive.

The tar command takes the archive filename as the last argument, and any arguments before that are the files and directories to be included in the archive. In this case, /path/to/important/files and /path/to/other/directories are the two directories that will be included in the backup archive.

The tar command will create a compressed archive of all the specified files and directories, and save it to the $backup_dir directory with the filename specified in $backup_file variable. The resulting archive will contain all the files and subdirectories of /path/to/important/files and /path/to/other/directories, along with their permissions, ownership, and other metadata.

Note that you can specify as many files and directories as you want after the archive filename, separated by spaces, and they will all be included in the archive.

rsync command:

For those who are not familiar with rsync command, here’s the explanation.

The rsync command is a powerful utility that is commonly used for file synchronization and backup purposes. It is particularly useful for efficiently transferring large amounts of data over a network, as it only transfers the differences between files, rather than copying entire files.

Here are some of the main features and capabilities of the rsync command:

  • It can copy and synchronize files and directories locally or remotely over a network.
  • It can preserve file permissions, ownership, timestamps, and other metadata during the transfer.
  • It can perform delta encoding, which means it only transfers the parts of a file that have changed since the last transfer, resulting in faster and more efficient transfers.
  • It can compress data during transfer to reduce bandwidth usage.
  • It can resume interrupted transfers and efficiently handle network disruptions.
  • It can provide a range of options for filtering, excluding, and including specific files and directories.
  • It can run in daemon mode, which allows it to serve as a backup server that other systems can connect to and transfer data.

Overall, the rsync command is a versatile and powerful tool for managing data backups, synchronization, and remote transfers.

Afterthoughts:

So, the command worked without any trouble, thankfully. Since backup shell-scripting is quite used a lot in real work, I thought it would be a great practice to get used to the Linux scripting language once more. Additionally, due to its open-source nature as well as its ever-increasing popularity and the global demands in various use cases, it’s never too late to learn Linux if you’re not familiar with it.

In my several years of engineering experience, Linux was almost always used as a backend server system. One aspect that I particularly appreciate about open-source operating systems is the vast amount of playgrounds it provides for me to explore and experiment with various technological creations.

As in one of my Linux series – Linux Programming, you can learn to program actual Linux commands with C language. Isn’t it exciting?

Building your own home server, creating our original commands with C/C++, and automating our daily tasks with shell-scripting/Python. Linux’s open nature and its possibility to offer us a vast amount of tech creativity and discovery don’t know its limit.

I can proudly say Linux changed my life for the better and always will.

Leave a Reply