How to take backup of your files regularly to external drive or to a new mount point ?

Hello Everyone,
As I have mentioned in other posts, I am using Arch based Endeavour distro in my ThinkPad.
No matter how stable the OS is, its always recommended to backup your important data.
So by doing very small tasks I achieve something like regular backup and OnDemand backup in my Arch Linux.

Step 1: Identify What folder you like to backup


So just to avoid any crazy permission related issues I got, like mentioned here , I set myself to use my home folder only for learnings.
So I have created a folder called `my_learning` and I am going to keep all my notes, code at that location. So if you want to create a folder you can simply do

mkdir ~/my_learning

Step 2: Install rsync ( If not installed )

If you dont know what rsync from Wikipedia

rsync is a utility for efficiently transferring and synchronizing files between a computer and an external hard drive and across networked computers by comparing the modification times and sizes of files


so what ever the distro you are using the binary name stays same to install rsync.
RedHat/CentOS/Fedora

sudo yum install rsync


Debian/Ubuntu

sudo apt-get install rsync


Arch

sudo pacman -S rsync


Step 3: Lets identify what Rsync options needed

So We need to backup to be happen in recusrive order and while its happening I need compress to happen just to save sometime. And verbose and human readable output format and pretty much needed anyway. So overall I need below options

-r, --recursive             recurse into directories
-z, --compress              compress file data during the transfer
-v, --verbose               increase verbosity
-h, --human-readable        output numbers in a human-readable format


But Rsync isnt limited to jus these 4 options, if you want to know more about rsync options, please check out its man page.

Step 4: source and target locations


So In one of my mount point I have created a folder with same name as source( make sure its mounted before creating the folder)
mkdir /run/media/username/ContinousImprovement/my_learning
.
and source is anyway my home folder
my_learning
location.
And the syntax of rsync is similar to cp command in linux i.e
cp [OPTIONS] source destination

rsync -zrvh /home/username/my_learning /run/media/username/ContinousImprovement/my_learning

Step 4: lets make the command handy

So I am using ZSH as my default shell, so I have opened my
.zshrc
file. If you are using bash, you can use
.bashrc
and I have added below function using shell scripting at very bottom

# backup home directory
function backup_home
  rsync -zrvh /home/username/my_learning /run/media/username/ContinousImprovement/my_learning


and execute
exec $SHELL
or
source ~/.zshrc
.
That's it, now if you call
backup_home
from terminal, your source directory will be backup to remote directory. if you want to automate it using a scheduler job, you can achieve same using
cron
, but make sure target is available during execution.
my_learning backup_home 
sending incremental file list
my_learning/ansible/ansible.cfg
my_learning/ansible/inventory
my_learning/ansible/test.yaml
...
...
...

sent 51.93M bytes  received 141 bytes  103.86M bytes/sec
total size is 55.07M  speedup is 1.06
my_learning 


Hope it helps.
Thank you.

Comments

Popular posts from this blog

grep: unknown device method

Uploading files to FTP/SFTP using CURL

How to find outgoing IP in Linux ?