How To: Simple Backups
Many computer users realize how invaluable a backup scheme can be and most linux distros already include the required software for a simple backup scheme. I am going to show you how to write a simple bash script that will archive specified files in your home directory and add the date/time to the filename of the archive.
|
|
Many computer users realize how invaluable a backup scheme can be and most linux distros already include the required software for a simple backup scheme. I am going to show you how to write a simple bash script that will archive specified files in your home directory and add the date/time to the filename of the archive. A GUI backup application is great for folks are comfortable with GUI apps. However, those types of apps are, in my opinion, overkill for simple backups.
Creating the backup script
Go to a console, or open a terminal emulator if you happen to be in a GUI environment.
Change directories to a path where you want to store the script. I keep all bash scripts in a "bin" directory in my home directory, this keeps things a bit more organized. You should create a bin directory in your home directory if it doesn't already exist.
cd $HOME/bin
Create a new file.
touch mybackups.sh
Open your favorite text editor and add the below code to the new file. I have included comments to show how the script can be personalized, text appearing in red is emphasized to show areas that you may want to change but is still part of the script. Lines 2 - 8 can be omitted but I feel this information should always be included in scripts for obvious reasons.
#!/bin/bash
# Filename:
# Version:
# Date:
# Author:
# License:
# Requires:
# Description:
# Change directories to /home
cd /home
# Archive all files in $HOME
# Change /target/path to your desired target path
# Certain files can be excluded from the archive with --exclude=filename
tar -cjf /target/path/temp.tar.bz2 --exclude=*gvfs $USER
# Check for a previous backup with identical filename
# Change /target/path to your desired target path
# Change date +%Y%m%d to your desired date/time; see man date
# Change backups as needed
if [ -e /target/path/$(date +%Y%m%d)-backups.tar.bz2 ]
then
rm /target/path/$(date +%Y%m%)-backups.tar.bz2
fi
# Move the archive
# Change /target/path to your desired target path
# Change date +%Y%m%d to your desired date/time; see man date
# Change backups as needed
mv /target/path/temp.tar.bz2 /target/path/$(date +%Y%m%d)-backups.tar.bz2
# Done
exit
That's it. This script can be executed with sh mybackups.sh after which you should have a new archive located in /target/path. I have two hard drives in all of my computers - the second hard drive is used to store files such as backups.
I like this type of script because it's simple - it's only 9 lines of code if you omit the comments - and can be edited as the user desires. This type of script can also be used in a cronjob to make scheduled backups without user intervention. For a tutorial on cronjobs, please see my Crontab Tutorial.
Full Story |
This topic does not have any threads posted yet!
You cannot post until you login.