Advertisement
Tech

Creating an Ubuntu Backup Files Script

It is important to backup your data regularly. The following three scripts can be used to backup your data whether you use Ubuntu or another Linux distribution.

By Kristen Grubb
Desk Tech
Reading time 3 min read
Word count 604
Linux Computing Linux security
Creating an Ubuntu Backup Files Script
Advertisement
Quick Take

It is important to backup your data regularly. The following three scripts can be used to backup your data whether you use Ubuntu or another Linux distribution.

On this page

Very Simple Backup

Home users may find that they only need to backup their home directory in order to satisfy their backup needs. The following script meets that need nicely:

#!/bin/bash

Advertisement

## The directory that should be backed up

folder=/home/$USER

Advertisement

## name of backup without file extensions

backup=/path/to/backup

Advertisement

##The tar command creates a backup of the file with the date appended to it.

tar -cpzf “${backup%}`date +%m%d%Y`.tar.gz” –ignore-failed-read $folder

Advertisement

The most important part of the above script is the line “tar -cpzf “${backup%}`date +%m%d%Y`.tar.gz” –ignore-failed-read $folder.” This line says to create a gzipped tar file using the name supplied in the $backup variable (in this case “backup”), appended with the current date in the form mmddYY. For example, the filename could be “/mount/usb1/backup093009.tar.gz.”

This script can be saved as “backup.sh” and set to run as a regular cron job . If you set the path to the backup file to an external hard drive, you don’t even have to worry about moving the file once it is created.

Advertisement

Backing up to CD or DVD

If you don’t have an external hard drive, or you simply prefer to store your backups on CD- or DVD-rom, you can create ISO images and burn the image to optical media. This particular script places the temporary .iso file in the Ubuntu /tmp directory, then removes it before the script ends. This ensures that your hard drive will not be clogged up with large .iso files.

#! /bin/bash

Advertisement

#The directory that should be backed up

folder=/home/$USER

Advertisement

#The temporary .iso file (this will be removed)

temp_file=/tmp/backup.iso

Advertisement

mkisofs -o $temp_file $folder

##Uncomment the line that corresponds to the media you want to use

Advertisement

##cdrecord is used for cd-rom

##growisofs is used for dvd-rom

Advertisement

#cdrecord -v -dao dev=2,0,0 $temp_file

#growisofs -dvd-compat -Z /dev/cdrom=$temp_file

Advertisement

#Remove the temporary .iso file.

rm -f $temp_file

Advertisement

This script can be used to backup to either a CD or DVD. To use it with a CD, uncomment the “cdrecord” line. To use it with a DVD line, uncomment the “growisofs” line. The only caveat here is that the backups cannot exceed the size of the removable media. If you have a lot of data that needs to be backed up, you will have to separate the data and create multiple scripts.

Backing up to a file server

If you have a file server at your disposal, you can use the rsync to create a backup. The rsync utility syncs two versions of a file system or directory structure across a network. The utility only updates the differences between files and directories, without requiring that the complete set of files be present on both sides of the transfer. A simple backup script for rsync would look like:

Advertisement

#! /bin/bash

#The directory that should be backed up

folder=/home/$USER

#The name of the server

server=Server_Name

#The backup directory on the file server

backup=/$USER/backup

#Password on the file server

export RSYNC_PASS=XXXXXX

#Create and maintain the backup

rsync -avz $folder $server::$backup

This script does not create incremental backups, it simply creates a mirror of the original, and then ensures that the mirror is maintained. The rsync documentation has a nice script for a 7 day incremental backup that you can change to suit your needs.

Conclusion

The above are very simple backup scripts that can be used on a home system. They can be tweaked and changed to suit your particular needs. The most important thing to remember is to choose a backup method that meets your needs and then remember to use it regularly. The first and the third method can be placed in a cron job to create an automatic backup.

Keep Exploring

More from Tech

Filed under
Linux Computing
More topics
Linux security
Advertisement