How To: Bash Backup Script Leave a reply


Alright so here is my simple version of a bash backup script… Feel free to comment, if I miss something or you think something should be added please let me know. For this example I assume that you have the proper user access and have some minimal command line experience!
First you should know that I am using several basic commands like “cp” to copy files and “tar” to compress files. You will also need to know how to use command line text editors, I am using “emacs” but “vi,”  “view” or whatever text editor you use will work.
Any line beginning with the number sign # and has text colored in green indicates a comment. Please note that I do not take responsibility for any issues you may cause when using my backup method, I do recommend testing the backup method on a directory that does not have important contents (so create a test directory with test files and try backing it up first). However I highly doubt the method will cause problems unless you severely screw up on my instructions…
1) The first thing we need to do is make a backup directory on the Linux box:
## Change to the / (root) directory (again I am assuming you have proper access to your system - as root)
cd /
## make your backup directory and create a folder for your backups
mkdir -p /backup/my-backup
2) Now we need to create the script:
# Change to the home directory (remember you are root so you may have to change permissions later)
cd ~
## now lets create the bash file for our backup script using emacs
emacs my-backup.bsh
## at the start/top of the file please add the following, note the number sign which looks like a comment, this allows your system check the file and run it as bash script
#!/bin/bash
## save the bash file and change its permissions so that the script can be executed
chmod +x my-backup.bsh
## now go back into the backup bash file and start the code
emacs my-backup.bsh
3) Now we write the script:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#!/bin/bash
# This is my backup file - created by
# here I am setting a time stamp variable which I like to use for logging
TIMESTAMP=`date +%Y%m%d.%H%M`
# here I am setting up the backup directory as a variable
DEST_DIR="/backup/my-backup"
# here I am setting up the directory in which I want to backup, again another variable
SRC_DIR="/home//Documents"
# let's create a variable for the backup file name file
FNAME="MyBackup"
# let's create a variable for the log file, let's also name the log file with the filename and timestamp it
LOG="/home//log/$FNAME-$TIMESTAMP.log"
# start the backup, create a log file which will record any messages run by this script
echo -e "Starting backup of $SRC_DIR directory" >> ${LOG}
# compress the directory and files, direct the tar.gz file to your destination directory
tar -vczf ${DEST_DIR}/${FNAME}-${TIMESTAMP}.tar.gz ${SRC_DIR} >> ${LOG}
# end the backup, append to log file created by this script
echo -e "Ending backup of $SRC_DIR" >> ${LOG}
The backup is now complete, we have created a compressed copy of the /home//Documents directory in the /backup/my-backup directory. If there were any errors with backup process the log file we created in /home//log will tell us what the error may be. Normally you don’t have to create variables to do a backup but if you wanted to backup another directory all you need to do is duplicate this script and change the SOURCE DIRECTORY, DESTINATION DIRECTORY, LOG-FILE (location) and FILE-NAME to the additional backup directory.
Advanced Settings: This backup script is a great way to backup your files but you should really consider moving your files off the same drive (computer) to an alternate location. If you do not have an off-site location perhaps you have another local Linux box or NAS device which will allow you to preform backups via scp (secure copy) or rsync. Finally the last thing you should do is set this script to run on a regular basis. To do this you need to create a cronjob using your systems crontab.
Source/ http://www.skrakes.com/2010/08/20/how-to-bash-backup-script#respond