How to back directories in Linux

* Open your favorite text editor and create a file under:

/usr/local/bin/backup.sh
  • Copy and paste the script into the backup.sh file
# What to backup.
backup_files="/var/www /etc /root /home/ubuntu"

# Where to backup to.
dest="/mnt/backup"

# Create archive filename.
hostname=$(hostname -s)  
archive_file="$hostname-`date --iso-8601`.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  
  • Modify the backup_files and add/remove the directory you wish to backup
  • Create cron job to automate the backup – Example of a weekly back cron
@weekly         /usr/local/bin/backup.sh 2>/dev/null

Good Luck!

Hassan El-Masri

Network Consultant Engineer

comments powered by Disqus