Shell script to backup directories & files from your home directory and email them as .tar.gz file

# mybackup - Backup selected files & directories and email them as .tar.gz file to
# your email account.
# List of BACKUP files/dirs stored in file ~/.mybackup
# Usage   : ./mybackup
# Notes   : Very handy tool to take backup (nowdays we have 1 gig+ email a/c)
# Copyright (C) 2004 nixCraft project
# Email/Contact   :
# Date            : Aug-2004
# Last updated    : Aug-2005
# -------------------------------------------------------------------------
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
# -------------------------------------------------------------------------
# This script is part of nixCraft shell script collection (NSSC)
# Visit for more information.
# -------------------------------------------------------------------------
NOW=`date +"%d-%m-%Y"`
OUT="`echo $USER.$HOSTNAME`.$NOW.tar.gz"
TAR=`which tar`
# mail setup
MSUB="Backup (`echo $USER @ $HOSTNAME`) as on `date`"
# make sure we put backup in our own tmp and not in /tmp
[ ! -d ~/tmp ] && mkdir ~/tmp || :
if [ -f $FILE ]; then
	IN="`cat $FILE | grep -E -v "^#"`"
	echo "File $FILE does not exists"
	exit 3
if [ "$IN" == "" ]; then
	echo "$FILE is empty, please add list of files/directories to backup"
	echo "Use mybackupadd script"
	 exit 2
$TAR -zcf ~/tmp/$OUT $IN >/dev/null
# create message for mail
echo "Backup successfully done. Please see attached file." > $MES
echo "" >> $MES
echo "Backup file: $OUT" >> $MES
echo "" >> $MES
# bug fix, we can't send email with attachment if mutt is not installed
which mutt > /dev/null
if [ $? -eq 0 ]; then
	# now mail backup file with this attachment
	mutt -s "$MSUB" -a "$MATT" $MTO < $MES
	echo "Command mutt not found, cannot send an email with attachment"
# clean up
/bin/rm -f $MATT
/bin/rm -f $MES
Get the latest tutorials on SysAdmin, Linux/Unix, Open Source, and DevOps topics:
Category List of Unix and Linux commands
File Management cat
Firewall Alpine Awall CentOS 8 OpenSUSE RHEL 8 Ubuntu 16.04 Ubuntu 18.04 Ubuntu 20.04
Network Utilities dig host ip nmap
OpenVPN CentOS 7 CentOS 8 Debian 10 Debian 8/9 Ubuntu 18.04 Ubuntu 20.04
Package Manager apk apt
Processes Management bg chroot cron disown fg jobs killall kill pidof pstree pwdx time
Searching grep whereis which
User Information groups id lastcomm last lid/libuser-lid logname members users whoami who w
WireGuard VPN Alpine CentOS 8 Debian 10 Firewall Ubuntu 20.04
4 comments… add one
  • abhishek Dec 28, 2012 @ 11:01

    what does this line will do. I too new for scripting :

    IN=”`cat $FILE | grep -E -v “^#”`”

  • vamsi Jan 2, 2010 @ 15:15

    thanks :D
    its very useful ;)

  • Marcelo Saied Apr 6, 2009 @ 17:04

    nice script

  • Boris C. Jul 12, 2008 @ 21:33

    Hi, i am not experienced in this, but i would like to know where to find help files on this script.

Leave a Reply

Your email address will not be published. Required fields are marked *

Use HTML <pre>...</pre>, <code>...</code> and <kbd>...</kbd> for code samples.