Skip to content

howto - simple backupscript for your linux home

The backupscripts i provide in this blog entry doesn't contain any voodoo. But when you start with linux, i hope it will put a lot of personal insecurity away. Perfectly, you will became more secure working with the shell by using this scripts. Since you have to adapt the scripts, you have to read it. I tried to add a lot of comments to it but i do not repeat a comment.

The heart of my backup script is a script that calls further scripts. This lets you easily extend your backup process without changing your backup command. Furthermore, you just have to set one alias in your bashrc (like "alias backupHome='sh ~/code/sh/local/bachup.sh';") you have to call. For me, the backupscript first calls a script that tar's all my settings. After this script is finished, a second script rsync's all wished files to a backup path. Rsync should be your first choice for syncing folders. Read the manual for rsync or some howto's in the web if you want to know more about it works or why i used the settings i used.

The script that tar's all my settings is quit simple. It first renames/moves the existing tar to an old name (if it exists), then tar's all the given settings into an archive and deletes the backup if the new tar was created. You can see, it is more or less just a tar call with a list of files or directories you want to have in the tar.

The second script executes the rsync call with some parameters and with the "from" and the "to" directory path. Pretty straight forward, isn't it? When you download this script, you only have to edit some areas. In the backup.sh, you have to edit the path where your other scripts where stored. In the tarSettings.sh, you have to edit the settings you want to tar. I don't think you are using all the tools i use ;-). In the rsyncDirectorys.sh, you have to edit the two paths and also the directories you want to rsync. I created a tar with all three scripts for you as download backup.tar.gz and also added the scripts for copy and paste below. Have fun with it and if you find errors or something you want to add, feel free.

tarSettings.sh

#!/bin/bash # This script tars all the files into an tar.gz archive. It backups an old copy before creating the new one and deletes the backup on success. # # @author artodeto # @param string TARBALL - the name of the tar.gz you want to create # @param string TARBALLOLD - the name for the backup # @param string DIRECTORY - the name where you want to store the TARBALL # @since 2012-07-25 TARBALL='settings.tar.gz' TARBALLOLD="$TARBALL.old" DIRECTORY='/home/artodeto/backup/' if [ -f "$DIRECTORY$TARBALL" ]; then echo 'renaming old settings' mv "$DIRECTORY$TARBALL" "$DIRECTORY$TARBALLOLD" fi tar -czf "$DIRECTORY$TARBALL" ~/.viminfo ~/.filezilla ~/.wireshark ~/.umlet ~/.vim ~/.netbeans ~/.config ~/.ssh ~/.bashrc ~/.notion ~/.irssi ~/.VirtualBox ~/.conkyrc ~/.mozilla ~/.gnome2_private ~/.pentadactyl if [ -f "$DIRECTORY$TARBALL" ]; then echo 'removing old settings' rm "$DIRECTORY$TARBALLOLD" fi

rsyncDirectorys.sh

#!/bin/bash # This script rsyncs all given diretories from on path to another. # # @author artodeto # @param string DIRECTORIES - all the directories you want to rsynced # @param string SOURCE - from where you want to rsync # @param string DESTINATION - to where you want to rsync # @since 2012-07-25 DIRECTORIES='coding data backup log scripts bin config doc tool' SOURCE='/home/artodeto/' DESTINATION='/home/artodeto/share/in/backup' for DIRECTORY in $DIRECTORIES; do rsync -vrptgoLDu --delete $SOURCE$DIRECTORY/ $DESTINATION/$DIRECTORY/ done;

backup.sh

#!/bin/bash

This script executes all steps for your backup.

@author artodeto

@since 2012-07-25

if file exists, execute it

if [ -f ~/code/sh/local/backup/tarSettings.sh ]; then echo '----' echo 'starting tar the settings' echo '----' sh ~/code/sh/local/backup/tarSettings.sh fi

if [ -f ~/code/sh/local/backup/rsyncDirectorys.sh ]; then echo '----' echo 'starting rsync directorys' echo '----' sh ~/code/sh/local/backup/rsyncDirectorys.sh fi