Your submission was sent successfully! Close

You have successfully unsubscribed! Close

Thank you for signing up for our newsletter!
In these regular emails you will find the latest updates about Ubuntu and upcoming events where you can meet our team.Close

Basic backup shell script

The following shell script uses tar to create an archive file on a remotely mounted NFS file system. The archive filename is determined using additional command line utilities.

Either copy the code into a file, or for instructions of how to use the script, refer to this guide.

#!/bin/bash
####################################
#
# Backup to NFS mount script.
#
####################################
    
# What to backup. 
backup_files="/home /var/spool/mail /etc /root /boot /opt"
    
# Where to backup to.
dest="/mnt/backup"
    
# Create archive filename.
day=$(date +%A)
hostname=$(hostname -s)
archive_file="$hostname-$day.tgz"
    
# Print start status message.
echo "Backing up $backup_files to $dest/$archive_file"
date
echo
    
# Backup the files using tar.
tar czf $dest/$archive_file $backup_files
    
# Print end status message.
echo
echo "Backup finished"
date
    
# Long listing of files in $dest to check file sizes.
ls -lh $dest
  • $backup_files: A variable listing which directories you would like to backup. The list should be customized to fit your needs.

  • $day: A variable holding the day of the week (Monday, Tuesday, Wednesday, etc). This is used to create an archive file for each day of the week, giving a backup history of seven days. There are other ways to accomplish this including using the date utility.

  • $hostname: A variable containing the short hostname of the system. Using the hostname in the archive filename gives you the option of placing daily archive files from multiple systems in the same directory.

  • $archive_file: The full archive filename.

  • $dest: Destination of the archive file. The directory needs to be created and in this case mounted before executing the backup script. See NFS for details of using NFS.

  • status messages: Optional messages printed to the console using the echo utility.

  • tar czf $dest/$archive_file $backup_files: The tar command used to create the archive file.

    • c: Creates an archive.

    • z: Filter the archive through the gzip utility, compressing the archive.

    • f: Output to an archive file. Otherwise the tar output will be sent to STDOUT.

  • ls -lh $dest: Optional statement prints a -l long listing in -h human-readable format of the destination directory. This is useful for a quick file size check of the archive file. This check should not replace testing the archive file.

This is a simple example of a backup shell script; however there are many options that can be included in such a script. For more information on shell scripting see the Advanced Bash-Scripting Guide.

Further reading

  • The CronHowto Wiki Page contains details on advanced cron options.

  • See the GNU tar Manual for more tar options.

  • The Wikipedia Backup Rotation Scheme article contains information on other backup rotation schemes.

  • The shell script uses tar to create the archive, but there many other command line utilities that can be used. For example:

    • cpio: Used to copy files to and from archives.

    • dd: Part of the coreutils package. A low level utility that can copy data from one format to another.

    • rsnapshot: A filesystem snapshot utility used to create copies of an entire file system. Also check the Install rsnapshot guide for more information.

    • rsync: A flexible utility used to create incremental copies of files.

This page was last modified 10 months ago. Help improve this document in the forum.