dobackup

Submitted scripts and programs
Forum rules
Your own work only.
User avatar
GekkoP
Emacs Sancho Panza
Posts: 5878
Joined: Tue Sep 03, 2013 7:05 am

dobackup

Unread post by GekkoP » Thu May 29, 2014 12:21 pm

As DebianJoe once said: "There is no better way to learn to do something manually than to write a way to automate it."

So I wrote this little script to help me with my regular backups. Nothing really fancy, but it is more than enough for my needs.

Code: Select all

#!/bin/bash

check_dir_file() {
    if [ ! -e $1 ]; then
	echo 'directory "'$1'" does not exist'
	exit 1
    elif [ ! -d $1 ]; then
	echo '"'$1'" is not a valid directory'
	exit 1
    fi
}

usage() {
    echo 'dobackup: copies or compresses a folder to a destination'
    echo '          updates existing file if any'
    echo '          asks confirm to overwrite previous archive'
    echo 'usage: dobackup from_directory to_directory'
    exit 1
}

if [ -z $1 ]; then
    usage
fi

if [ -z $2 ]; then
    usage
fi

sudo -v # gets user password for next sudos

check_dir_file $1
bak=$1

check_dir_file $2
loc=$2

compressed=0
tar_name=`basename $bak`
read -n1 -p 'do you want to compress your backup? (y/n) > '
echo
if [ $REPLY = 'y' ]; then
    compressed=1
    echo 'compressing "'$bak'"'
    tar zcf $tar_name.tar.gz -C $bak .
fi

if [ $compressed = 1 ]; then
    echo 'moving "'$tar_name'.tar.gz" to "'$loc'"'
sudo mv -i $bak.tar.gz $loc
else
    echo 'copying "'$bak'" to "'$loc'"'
sudo cp -ru $bak $loc
fi

echo 'backup done'

machinebacon
Baconator
Posts: 10253
Joined: Thu Sep 16, 2010 11:03 am
Location: Pfälzerwald
Contact:

Re: dobackup

Unread post by machinebacon » Thu May 29, 2014 1:12 pm

that's a nice idea :) thanks for sharing

I think we need a bbq-toolbox (again, something that DebJoe once wrote, for killx)
..gnutella..

User avatar
Alad
should take a shower
Posts: 447
Joined: Wed May 21, 2014 12:52 am

Re: dobackup

Unread post by Alad » Thu May 29, 2014 1:21 pm

Did you consider using rsync?
It's funny how we used to be able to do real stuff with rudimentary computers, but now we can't. -- ratcheer

User avatar
GekkoP
Emacs Sancho Panza
Posts: 5878
Joined: Tue Sep 03, 2013 7:05 am

Re: dobackup

Unread post by GekkoP » Thu May 29, 2014 1:33 pm

^ feel free to grab the code and do whatever you like with it. If you come up with a better solution please share it here. I'm always happy to fix my scripts if the suggestion does not bloat them too much. ;)

Post Reply