Simple Bash Backup Script

I often play around with little Bash scripts and Perl One-Liners in my quest to learn more. This particular script backs up specific data that I need when I reinstall an (Debian) OS.

I’ve got various versions and this is just a base (which you can add-to and play with). As always, with any code, especially Bash, there are a whole heap of ways to write backup scripts, both long and short, but this script is only intended to show newer users some basic commands, and to act as a template for further investigation.

#! /bin/bash
#
#SIMPLE BASH BACKUP SCRIPT BY RICHS-LXH
#
#SAVES FILES TO A DATED DIR ON YOUR /STORAGE PARTITION
#
#ON SCREEN MESSAGE AND LOG CREATED
echo Backing up your data…
echo Backup started `date` >> ~/backuplog
#CREATE NECESSARY FILES
cd /storage
mkdir -p backups/`date +%Y%m%d`
mkdir -p backups/tmpdata/important/
#COPY FILES YOU WANT BACKED UP
sudo cp /etc/host* backups/tmpdata/important/
sudo cp /etc/X11/xorg.conf backups/tmpdata/important/
sudo cp /etc/network/interfaces backups/tmpdata/important/
#EMPTY THE TRASH BEFORE HOME BACKUP !!!
sudo rm -rf /home/rich/.local/share/Trash/files/*
#WAIT UNTIL IT HAS FINISHED REMOVING ALL THE TRASH
wait
#COPY ENTIRE HOME DIR RECURSIVELY TO TMPDATA
sudo cp -a /home/rich backups/tmpdata/
#CD TO TMPDATA AND CREATE A COMPRESSED BACKUP TAR.GZ
cd backups/tmpdata
tar -czf /storage/backups/`date +%Y%m%d`/bkp.tar.gz *
cd ..
#REMOVE THE TEMPORARY DATA
wait
sudo rm -rf tmpdata
#WRITE A LOG AND PRINT A MESSAGE
echo Backup Finished `date` >> ~/backuplog
echo All your data successfully backed up!

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s