The Universal Backup script
What it is
This is a backup system that uses three files:
- universalbackup – A text file containing the script.
- includes.txt – A text file for listing files and directories to back up.
- excludes.txt – A text file for listing files to exclude from the directories listed in includes.txt.
What it does
- Announces the date and time of the backup.
- Checks if the backup directory exists.
- Creates the backup directory if it don’t exist, announcing the creation.
- Reads the includes.txt file to know which files and directories to back up.
- Reads the excludes.txt file to know which files not to back up.
- Announces that it’s backing up files and directories.
- Compresses your files and folders into individual zip files in the backup directory, overwriting any files by the same name.
- Announces where it created the zip files.
- Announces that it’s testing the zip files.
- Tests all zip files in the temporary directory.
- Stops the script if any of the zip files fail the test, announcing the failure.
- Announces that it’s finished, and where to find your zip files.
What it doesn’t do
- It doesn’t create incremental backups.
- It doesn’t update existing zip files.
- It doesn’t exclude entire directories, but rather the files in them.
See the script
This is a fully commented script that lets you know what each step does.
#!/bin/bash # Universal Backup Script v3 ######################### # Configuration Section # ######################### # Edit the path in this line, changing it to the path to your backup directory: BACKUPDIRECTORY='/home/username/Desktop/backups/' # Edit the path in this line, changing it to the path to the includes.txt file that comes with this script: INCLUDES='/home/username/Desktop/includes.txt' # Edit the path in this line, changing it to the path to the excludes.txt file that comes with this script: EXCLUDES='/home/username/Desktop/excludes.txt' ################# # Backup Script # ################# # Announces the date and time the backup begins: echo echo "Beginning backup on `date`." # Checks for needed directories, creating them if they don't exist: echo if [ ! -d $BACKUPDIRECTORY ]; then mkdir $BACKUPDIRECTORY; echo "Creating the $BACKUPDIRECTORY directory..."; fi # Creates zip files in the backup directory: echo echo "Backing up files and directories..." awk '{print $1, $2}' $INCLUDES | while read line; do zip -qr $BACKUPDIRECTORY`basename $line`.zip $line -x@$EXCLUDES; done # Announces that the zip files were created: echo echo "Zip files have been created in $BACKUPDIRECTORY" # Tests the zip files in the backup directory, stopping the script if any of the files fail the test: echo echo "Testing zip files in $BACKUPDIRECTORY..." echo unzip -qt "$BACKUPDIRECTORY*.zip" if [ $? != "0" ] ; then echo "ZIP test failed - aborting script!"; exit 1; fi # Announces that it's finished: echo echo "Done! Your files are in $BACKUPDIRECTORY" # Rings the system bell to notify you: echo -e '\a' >&2
See an example includes.txt file
/home/username/Documents/ /home/username/Graphics/ /home/username/Music/
See an example excludes.txt file
/home/username/Documents/*.pdf
See what it looks like when it runs
Beginning backup on Fri Feb 12 16:55:19 EST 2010. Creating the /home/username/Desktop/backups/ directory... Backing up files and directories... Zip files have been created in /home/username/Desktop/backups/ Testing zip files in /home/username/Desktop/backups/... No errors detected in compressed data of /home/username/Desktop/backups/Documents.zip. No errors detected in compressed data of /home/username/Desktop/backups/Graphics.zip. No errors detected in compressed data of /home/username/Desktop/backups/Music.zip. 3 archives were successfully processed. Done! Your files are in /home/username/Desktop/backups/
See the backup directory after it runs
Documents.zip Graphics.zip Music.zip
Get it
Download UniversalBackupScript-v4.zip
Extract it
Double-click the file to extract it.
Before you begin
Before you run the script for the first time, you’ll need to customize the files that come with it so that it knows what you want to back up, and which directory you want to use for your backups.
Customize the backupscript file
- Edit the backupscript file by opening it in a text editor.
- Find the Configuration Section at the top of the file.
- Change /home/username/Desktop/backups/ to the full path to the the directory you would like to use for your backups.
- Change /home/username/Desktop/includes.txt to the full path to the includes.txt file that comes with this script.
- Change /home/username/Desktop/excludes.txt to the full path to the excludes.txt file that comes with this script.
- Save and close the file.
Customize the includes.txt file
This file contains some example entries you can remove. You’ll surely also wish to add some entries.
- Edit the file by opening it in a text editor.
- To delete an entry, delete an entire line from the file.
- To add an entry, type the full path to the file or directory you’d like to back up. Be sure to add a forward slash at the end of the path if the entry is a directory. Press the Enter key.
- Save and close the file.
Customize the excludes.txt file
This file contains an example entry you can remove. You may also wish to add some entries.
- Edit the file by opening it in a text editor.
- To delete an entry, delete an entire line from the file.
- To add an entry, type the full path to the file you’d like to exclude from the backup. Wildcards are accepted. Press the Enter key.
- The file should be completely empty if you have no files to exclude.
- Save and close the file.
Make sure you have room
- If disk space is at a premium, open a terminal window and type this command to check how much space you have available:
-
df -h
Run the script.
- Open a terminal window in the directory that contains the universalbackup, includes.txt, and excludes.txt files.
- Type this command:
bash universalbackup
Testing the universalbackup, it is a nice tool. Thanks a lot for sharing it!
But I noticed that the example excludes.txt-file doesn’t do anything. The excludes.txt needs to refer to files, and not to folders in order to do anything…
Comment by paul — September 29, 2010 @ 10:08 am
Oh, thank you for spotting that! I’ve updated the instructions on the page and in the README inside the zip file. The zip file is at v4, although the script didn’t change.
I also added a note on Mirrordir while I was in here making changes. Enjoy! :)
Comment by mostlylinux — September 29, 2010 @ 2:22 pm