- [Instructor] Now we move on to the 8th video
of this section, version control-based backup.
In the previous video,
we looked at taking backup snapshots with rsync.
In this video, we'll see how to use git
for the backup of regular files
in a non-programming context.
People use different strategies for backing up data.
Out of these, differential backups are more efficient
than making copies of the entire source directory
to target the backup the directory.
But the version number using data or time of day
as it causes wastage of space.
We only need to copy the changes that occur
to files from the second time that the backups occur.
This is also called incremental backup.
We can manually create incremental backups
using tools like rsync,
but restoring this kind of backup can be difficult.
The best way to maintain and restore changes
is to use version control systems.
They are very much used in software development
and maintenance of code,
since coding frequently undergoes changes.
Git is the most famous
and most efficient version control system available.
Let's use git for the backup of regular files
in a non-programming context.
Now let's get ready for our next task.
So, we have a directory that contains several files
and subdirectories.
We need to keep track of changes
occurring to the directory contents and back them up.
If data becomes corrupted or goes missing,
we must be able to restore a previous copy of that data.
We'll either need to take the backup at different locations
in the local machine,
or to a remote machine.
To install git, just use your distro's package manager,
and let's get started.
So in the directory which is to be backed up,
use this command line.
Let it be the directory source to be tracked.
Now, set up and initiate the remote backup directory.
In this remote machine,
create the backup destination directory in this way.
These steps are to be performed
in the sourced host machine.
Also, add user detail to git in the second host machine.
Now, initiate the source directory
to backup from the host machine,
in the source directory in the host machine
whose files are to be backed up and execute this command.
Next, in the source directory,
execute this command to add the remote git directory
and synchronize backup.
Let's now look into adding or removing files
for git tracking.
This command adds all the files and folders
in the current directory to the backup list.
You can also conditionally add certain files
only to the backup list as we do here.
You can even remove the files and folders
not required to be tracked by using this command.
It can be a folder or even a wildcard as this one.
Let's now see about checkpointing
or marking backup points.
We can mark checkpoints for the backup
with a message using this command.
We need to update the backup at the remote location
at regular intervals, hence, set up a Cron job,
for example, backing up every five hours.
Let's create a file Cron job entry with lines.
Now let's create a script/home/data/backup.sh in this way.
Now, we've set up the backup system.
Next, to view all backup versions, use this syntax.
To revert back to any previous state or version,
look into the commit id, which is a 32 character hex string.
Use the commit id with git checkout.
For this commit id, it will be this alpha-numeric value.
To make this revert permanent, we run this command.
Now, in order to view the details
of our versions again, use,
if the working directory is broken due to some issues,
we need to fix the directory with backup
at the remote location.
We can create the contents from the backup
at the remote location as follows.
It will create a directory backup with all contents.
You should note that while git is pretty good
for keeping versioned copies of text files
which include documents, source code, and so on,
it's not a good idea to use git
for large amount of binary-only data.
For example, it's not sensible to use git
to backup/version control your photo collection.
The reason for this is git keeps whole files
instead of differences when it comes to binary files,
and this will make it occupy a huge amount of space.
Wonderful.
In this video, we've used git for backup of regular files
in a non-programming context using version control.
In the next video, we'll learn to create
entire disk images with the fsarchiver command.