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'