Backing up your computer is very important indeed. How to do it with Linux using a simple script.

The importance of backing up your computer can not be understated. This is an integral part of using a computer, judging by the outages of various cloud backup services like the Microsoft Azure and other cloud services it is not a good idea to only use a could service like Google Drive or Skydrive to store backups. You should have a removable drive that can store backups as well as a cloud backup if you must. That way, if the cloud backup service is down, then you still have backups on a local drive. Windows has a backup facility in the Windows 7 release; but the Windows 8 release has neutered this facility for some unknown reason. The Ubuntu Linux distribution has the Ubuntu One system where you connect your ~/Documents folder to the Ubuntu One cloud backup system and you may then copy or save files into this folder and they are automatically uploaded to the cloud service. But again, you need to ensure that any critical files are backed up to a local backup drive as well. This will make sure that your files are really safe. rsync on Linux can easily ensure that your files are backed up. A backup script is very useful in a cron(8) job, this will backup your computer to an external hard drive on a regular basis. A very good way to gain the peace of mind that comes with a good backup plan. Of course with a Linux system; you have the many scripting languages that can be used to write a useful backup script.

Perl is a perfect scripting language for coding a suitable backup script; you can copy all of the files to a tar.bz2 file to compress the files and then copy that compressed archive to the backup drive. This example below will backup a folder to a compressed tar.gz file and this may then be copied to an external hard disk drive. The files are time stamped; so that you may know when they were created. And this simple script even checks whether the files were written properly.

#!/usr/bin/perl -W
use strict;
use POSIX ("strftime");
# A script to backup some files.
my $homedir = "$ENV{'HOME'}";
my $user = $ENV{'LOGNAME'};
my $date = strftime("%A-%d-%B-%Y-%H-%M-%S", localtime);
my $backupdrive = "/mnt/MyMedia";
print "Which directory do you want to backup?n";
chomp(my $dir = <STDIN>);
if(!$dir) {
	print "No Directory selected!n";
	exit;
} else {
	my $target="$homedir/$dir";
	my $file = "$homedir/$user-$date.tgz";
	system("tar -cvf $file $target");
	print "nnSuccessful backup of directory: $dir.n";
# Naming the outfile.
	my $outfile="$user-$date.tgz";
# Moving the backup file to the destination.
	print "Moving the archive $outfile to the backup drive.n";
	system("mv $outfile /mnt/MyMedia/");
 
# Checking to see if the backup file exists.
 
	my	$filename = "$backupdrive/$outfile";
	if (-e $filename) {
		print "nnBackup file exists. Great Success!nn";
	}
	exit;
}

No comments have been made. Use this form to start the conversation :)

Leave a Reply