How to back directories in Linux

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

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

# Where to backup to.

# 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"  

# Backup the files using tar.
tar czf $dest/$archive_file $backup_files

# Print end status message.
echo "Backup finished"  

# 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/ 2>/dev/null

Good Luck!

Hassan El-Masri

Network Consultant Engineer

comments powered by Disqus