A Simple Backup Script on Linux
Everyone whom uses Linux wants to implement a backup solution for their Linux servers. After a lot of research, I finally decided to implement a script-based backup making use of TAR, GZIP, and CROND based on Jason Pepas's backup script.
Step 1: Login to your Linux server as user "root" and type this command
mkdir /root/scripts
Step 2: Decide on what device you want to use as a storage for all the backups. For this scenario, I use a 30GB SCSI HDD plug to my Linux server. Then, I reboot my server to find the device name and edit my /etc/fstab file to add the storage device so that it is automatically mounted every time the server is rebooted. To find out the device name, type dmesg. In my case, the SCSI HDD is /dev/sdb2
vi /etc/fstab
LABEL=/dev/sdb2 /backup ext3 defaults 1 2
LABEL=/ / ext3 defaults 1 1
LABEL=/boot1 /boot ext3 defaults 1 2
none /dev/pts devpts gid=5,mode=620 0 0
none /dev/shm tmpfs defaults 0 0
none /proc proc defaults 0 0
none /sys sysfs defaults 0 0
LABEL=/var /var ext3 defaults 1 2
LABEL=SWAP-sdb1 swap swap defaults 0 0
type :wq to save and exit from the editing mode.
Step 3: Open your favourite text editor and paste this script. Save it as backup.sh
#!/bin/sh
# --------------------------------------------------
# set variables:
# --------------------------------------------------
directoryname=`date +%Y-%m-%d`"_"`hostname`"_backup"
current="current_"`hostname`
fullbackuplabel="Full Backup of "`hostname`" on "`date '+%B %e, %Y'`
fullbackupname=`date +%Y-%m-%d`"_full.tar.gz"
fullbackuplogname=`date +%Y-%m-%d`"_full.log"
incrementalbackuplabel="Incremental Backup of "`hostname`" on "`date '+%B %e, %Y'`
incrementalbackupname=`date +%Y-%m-%d`"_incremental"`date +%H%M`".tar.gz"
incrementalbackuplogname=`date +%Y-%m-%d`"_incremental"`date +%H%M`".log"
# --------------------------------------------------
# functions:
# --------------------------------------------------
fullbackup()
{
# create backup directory
if test ! -e /backup/$directoryname; then
echo "Creating /backup/$directoryname directory"
mkdir /backup/$directoryname
fi
# create (or update) a shortcut called current to this directory
echo "Updating /backup/$current pointer"
rm /backup/$current
ln -s /backup/$directoryname /backup/$current
# keep track of creation date of full backup (used with incremental backups)
echo "Updating /backup/$current/lastfullbackupdate"
date>/backup/$current/lastfullbackupdate
# create backup
echo "Running tar..."
tar --create --label "$fullbackuplabel" --files-from /root/scripts/whattobackup.txt --exclude-from /root/scripts/whatnottobackup.txt --igno
re-failed-read --absolute-names --verbose --gzip --file /backup/$current/$fullbackupname > /backup/$current/$fullbackuplogname 2>&1
gzip /backup/$current/$fullbackuplogname
echo "Done. Created /backup/$current/$fullbackupname"
echo "To view the log, type:"
echo " zcat /backup/$current/$fullbackuplogname"
}
incrementalbackup()
{
# create variable with date of last full backup
lastfullbackupdatevar=`cat /backup/$current/lastfullbackupdate`
# check for existence of incremental backup
if test -e "/backup/$current/$incrementalbackupname"; then
echo "Your last incremental backup was less than 60 seconds ago."
echo "Wait a minute and try again."
else
# create incremental backup
echo "Running tar..."
tar --create --label "$incrementalbackuplabel" --files-from /root/scripts/whattobackup.txt --exclude-from /root/scripts/whatnottobackup
.txt --ignore-failed-read --after-date "$lastfullbackupdatevar" --absolute-names --verbose --gzip --file /backup/$current/$incrementalbackupnam
e > /backup/$current/$incrementalbackuplogname 2>&1
gzip /backup/$current/$incrementalbackuplogname
echo "Done. Created /backup/$current/$incrementalbackupname"
echo "To view the log, type:"
echo " zcat /backup/$current/$incrementalbackuplogname"
fi
}
# --------------------------------------------------
# main routine:
# --------------------------------------------------
# first get a list of all packages installed.
rpm -qa | sort > /etc/apt/installed-packages
# clear out apt's packages
apt-get clean
# now perform the backup.
echo "---------- Backup Script Running... ----------"
if test `date +%A` = "Sunday" && ! -e "/backup/$directoryname"; then
# if it is sunday and you havent yet done a full backup, do so
echo "Performing Weekly Full Backup..."
fullbackup;
elif test ! -e /backup/$current/*full.tar.gz; then
# if there is no current fullbackup, make one
echo "No Current Full Backup - Performing Full Backup Now..."
fullbackup;
else
# otherwise, do an incremental backup
echo "Performing Incremental Backup..."
incrementalbackup;
fi # end if statement
echo "---------- Backup Script Done ----------"
-- End of backup script --
Step 4: Now, we need to create two files to tell the backup script what to include and exclude during the backups.
Type these commands;
touch /root/scripts/whattobackup.txt
touch /root/scripts/whatnottobackup.txt
vi touch /root/scripts/whattobackup.txt
Paste the following content (change according to your needs);
-- Start of /root/scripts/whattobackup.txt file --
# This file is required by /root/scripts/full-backup.sh
#
/boot
/etc
/home
/root
/usr/local
/var
-- End of /root/scripts/whattobackup.txt file --
-- Start of /root/scripts/whatnottobackup.txt file --
# This file is required by /root/scripts/full-backup.sh
#
*.mp3
*.mpg
*.avi
*.wav
*.mov
*.asf
*.rm
*.iso
*.flac
*.zip
*.exe
*.tmp
data.bin
-- End of /root/scripts/whatnottobackup.txt file --
Step 5: Now, schedule the backup script to execute everyday at 7pm Monday - Friday using cron.
type command;
crontab -e
You may see a blank cron schedule or something like that;
30 04 * * * /bin/sh -c '/usr/bin/sa-learn --mbox --ham /home/spam-police/mail/Non-spam'
35 04 * * * /bin/sh -c '/usr/bin/sa-learn --mbox --spam /home/spam-police/mail/Spam'
40 04 * * * /bin/sh -c '/usr/bin/sa-learn --mbox --spam /home/spam-police/mail/.imap/INBOX/'
Add these line for your backup script;
00 19 * * * /root/scripts/backup.sh
That's it!
Everyone whom uses Linux wants to implement a backup solution for their Linux servers. After a lot of research, I finally decided to implement a script-based backup making use of TAR, GZIP, and CROND based on Jason Pepas's backup script.
Step 1: Login to your Linux server as user "root" and type this command
mkdir /root/scripts
Step 2: Decide on what device you want to use as a storage for all the backups. For this scenario, I use a 30GB SCSI HDD plug to my Linux server. Then, I reboot my server to find the device name and edit my /etc/fstab file to add the storage device so that it is automatically mounted every time the server is rebooted. To find out the device name, type dmesg. In my case, the SCSI HDD is /dev/sdb2
vi /etc/fstab
LABEL=/dev/sdb2 /backup ext3 defaults 1 2
LABEL=/ / ext3 defaults 1 1
LABEL=/boot1 /boot ext3 defaults 1 2
none /dev/pts devpts gid=5,mode=620 0 0
none /dev/shm tmpfs defaults 0 0
none /proc proc defaults 0 0
none /sys sysfs defaults 0 0
LABEL=/var /var ext3 defaults 1 2
LABEL=SWAP-sdb1 swap swap defaults 0 0
type :wq to save and exit from the editing mode.
Step 3: Open your favourite text editor and paste this script. Save it as backup.sh
#!/bin/sh
# --------------------------------------------------
# set variables:
# --------------------------------------------------
directoryname=`date +%Y-%m-%d`"_"`hostname`"_backup"
current="current_"`hostname`
fullbackuplabel="Full Backup of "`hostname`" on "`date '+%B %e, %Y'`
fullbackupname=`date +%Y-%m-%d`"_full.tar.gz"
fullbackuplogname=`date +%Y-%m-%d`"_full.log"
incrementalbackuplabel="Incremental Backup of "`hostname`" on "`date '+%B %e, %Y'`
incrementalbackupname=`date +%Y-%m-%d`"_incremental"`date +%H%M`".tar.gz"
incrementalbackuplogname=`date +%Y-%m-%d`"_incremental"`date +%H%M`".log"
# --------------------------------------------------
# functions:
# --------------------------------------------------
fullbackup()
{
# create backup directory
if test ! -e /backup/$directoryname; then
echo "Creating /backup/$directoryname directory"
mkdir /backup/$directoryname
fi
# create (or update) a shortcut called current to this directory
echo "Updating /backup/$current pointer"
rm /backup/$current
ln -s /backup/$directoryname /backup/$current
# keep track of creation date of full backup (used with incremental backups)
echo "Updating /backup/$current/lastfullbackupdate"
date>/backup/$current/lastfullbackupdate
# create backup
echo "Running tar..."
tar --create --label "$fullbackuplabel" --files-from /root/scripts/whattobackup.txt --exclude-from /root/scripts/whatnottobackup.txt --igno
re-failed-read --absolute-names --verbose --gzip --file /backup/$current/$fullbackupname > /backup/$current/$fullbackuplogname 2>&1
gzip /backup/$current/$fullbackuplogname
echo "Done. Created /backup/$current/$fullbackupname"
echo "To view the log, type:"
echo " zcat /backup/$current/$fullbackuplogname"
}
incrementalbackup()
{
# create variable with date of last full backup
lastfullbackupdatevar=`cat /backup/$current/lastfullbackupdate`
# check for existence of incremental backup
if test -e "/backup/$current/$incrementalbackupname"; then
echo "Your last incremental backup was less than 60 seconds ago."
echo "Wait a minute and try again."
else
# create incremental backup
echo "Running tar..."
tar --create --label "$incrementalbackuplabel" --files-from /root/scripts/whattobackup.txt --exclude-from /root/scripts/whatnottobackup
.txt --ignore-failed-read --after-date "$lastfullbackupdatevar" --absolute-names --verbose --gzip --file /backup/$current/$incrementalbackupnam
e > /backup/$current/$incrementalbackuplogname 2>&1
gzip /backup/$current/$incrementalbackuplogname
echo "Done. Created /backup/$current/$incrementalbackupname"
echo "To view the log, type:"
echo " zcat /backup/$current/$incrementalbackuplogname"
fi
}
# --------------------------------------------------
# main routine:
# --------------------------------------------------
# first get a list of all packages installed.
rpm -qa | sort > /etc/apt/installed-packages
# clear out apt's packages
apt-get clean
# now perform the backup.
echo "---------- Backup Script Running... ----------"
if test `date +%A` = "Sunday" && ! -e "/backup/$directoryname"; then
# if it is sunday and you havent yet done a full backup, do so
echo "Performing Weekly Full Backup..."
fullbackup;
elif test ! -e /backup/$current/*full.tar.gz; then
# if there is no current fullbackup, make one
echo "No Current Full Backup - Performing Full Backup Now..."
fullbackup;
else
# otherwise, do an incremental backup
echo "Performing Incremental Backup..."
incrementalbackup;
fi # end if statement
echo "---------- Backup Script Done ----------"
-- End of backup script --
Step 4: Now, we need to create two files to tell the backup script what to include and exclude during the backups.
Type these commands;
touch /root/scripts/whattobackup.txt
touch /root/scripts/whatnottobackup.txt
vi touch /root/scripts/whattobackup.txt
Paste the following content (change according to your needs);
-- Start of /root/scripts/whattobackup.txt file --
# This file is required by /root/scripts/full-backup.sh
#
/boot
/etc
/home
/root
/usr/local
/var
-- End of /root/scripts/whattobackup.txt file --
-- Start of /root/scripts/whatnottobackup.txt file --
# This file is required by /root/scripts/full-backup.sh
#
*.mp3
*.mpg
*.avi
*.wav
*.mov
*.asf
*.rm
*.iso
*.flac
*.zip
*.exe
*.tmp
data.bin
-- End of /root/scripts/whatnottobackup.txt file --
Step 5: Now, schedule the backup script to execute everyday at 7pm Monday - Friday using cron.
type command;
crontab -e
You may see a blank cron schedule or something like that;
30 04 * * * /bin/sh -c '/usr/bin/sa-learn --mbox --ham /home/spam-police/mail/Non-spam'
35 04 * * * /bin/sh -c '/usr/bin/sa-learn --mbox --spam /home/spam-police/mail/Spam'
40 04 * * * /bin/sh -c '/usr/bin/sa-learn --mbox --spam /home/spam-police/mail/.imap/INBOX/'
Add these line for your backup script;
00 19 * * * /root/scripts/backup.sh
That's it!
Comments