Skip to main content

An approach to generating a backup server is using a shell script.  A script can define directories to backup, and pass those directories as arguments to the tar utility, which creates an archive file. The archive file can then be moved or copied to another location.

 

Example Shell Script

#!/bin/bash

#
# Backup script
#
# NAME: daily-backup.sh
# PATH: /mnt/e/bin
# DESC: Backup scripts, documents and configuration files to .tar
#

# What to backup
# backup_files="/home /var/spool/mail /etc /root /boot /opt"
backup_files="/home /var /var/spool/mail /etc /root /boot /opt /usr /lib /bin"

# Backup destination
dest="/mnt/backup"

# Create archive filename
dateStr=$(date +"%Y-%m-%d")
hostname=$(hostname -s)
archive_file="$hostname-$dateStr.tgz"

# Print start 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 message
echo
echo "Backup finished"
date

# Long listing of files in $dest to check file sizes
ls -lh $dest

 

Definitions

backup_files a variable listing denoting the directories requiring backup
dateStr a string variable noting the year-month-day
hostname the variable containing the short hostname of the system
archive_file the full archive filename
dest destination of the archive file.  Ensure this directory exists
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

Relating to the dateStr, see the appendix below for more options.

 

Executing the Script

Create a backup script with a file named backup.sh (or whatever you prefer).  However, this file must be made executable:

chmod u+x backup.sh

Now test that the script runs without issue by executing the command:

sudo ./backup.sh

 

Restoring from the Archive

Already have an archive?  This archive can be checked by listing the files it contains.  However, it's highly recommended that you test by restoring a file from the archive.

To see a listing of the archive contents.  From a terminal prompt type:

tar -tzvf /mnt/backup/host-2022-08-03.tgz

Want to restore it to a different directory then execute:

tar -xzvf /mnt/backup/host-2022-08-03.tgz -C /tmp etc/hosts

The -C option to tar redirects the extracted files to the specified directory.  The above script will extract the /etc/hosts file to /tmp/etc/hosts. tar recreates the directory structure that it contains.

Also, notice the leading “/” is left off the path of the file to restore.

To restore all files in the archive enter the following:

cd /
sudo tar xzvf /mnt/backup/host-2022-08-03.tgz

Note: This will overwrite the files currently on the file system

 

List the contents of a tar archive

This lists all the files in the archive but does not extract them.  To list all the files in a compressed archive, add the z flag:

tar tzvf my-archive.tgz

I almost always list the contents of an unknown archive before I extract the contents. I think this is always a good practice, especially when you’re logged in as the root user.

 

Extracting a tar archive

To extract the contents of a Linux tar archive, now just replace the t flag with the x ("extract") flag. For uncompressed archives the extract command looks like this:

tar xzvf my-archive.tgz

 

 

Appendix

%FORMAT String Description
%% a literal %
%a locale’s abbreviated weekday name (e.g., Sun)
%A locale’s full weekday name (e.g., Sunday)
%b locale’s abbreviated month name (e.g., Jan)
%B locale’s full month name (e.g., January)
%c locale’s date and time (e.g., Thu Mar 3 23:05:25 2005)
%C century; like %Y, except omit last two digits (e.g., 21)
%d day of month (e.g, 01)
%D date; same as %m/%d/%y
%e day of month, space padded; same as %_d
%F full date; same as %Y-%m-%d
%g last two digits of year of ISO week number (see %G)
%G year of ISO week number (see %V); normally useful only with %V
%h same as %b
%H hour (00..23)
%I hour (01..12)
%j day of year (001..366)
%k hour ( 0..23)
%l hour ( 1..12)
%m month (01..12)
%M minute (00..59)
%n a newline
%N nanoseconds (000000000..999999999)
%p locale’s equivalent of either AM or PM; blank if not known
%P like %p, but lower case
%r locale’s 12-hour clock time (e.g., 11:11:04 PM)
%R 24-hour hour and minute; same as %H:%M
%s seconds since 1970-01-01 00:00:00 UTC
%S second (00..60)
%t a tab
%T time; same as %H:%M:%S
%u day of week (1..7); 1 is Monday
%U week number of year, with Sunday as first day of week (00..53)
%V ISO week number, with Monday as first day of week (01..53)
%w day of week (0..6); 0 is Sunday
%W week number of year, with Monday as first day of week (00..53)
%x locale’s date representation (e.g., 12/31/99)
%X locale’s time representation (e.g., 23:13:48)
%y last two digits of year (00..99)
%Y year
%z +hhmm numeric timezone (e.g., -0400)
%:z +hh:mm numeric timezone (e.g., -04:00)
%::z +hh:mm:ss numeric time zone (e.g., -04:00:00)
%:::z numeric time zone with : to necessary precision (e.g., -04, +05:30)
%Z alphabetic time zone abbreviation (e.g., EDT)

Related articles

Andrew Fletcher16 Jan 2025
get IP address from terminal OSX
When troubleshooting network issues or configuring devices, knowing your IP address can be essential. Whether you're connected via Wi-Fi, Ethernet, or tethering through a mobile provider, macOS offers powerful built-in tools to quickly identify your IP address. Here's a practical guide tailored to...