Skip to content

Instantly share code, notes, and snippets.

@sriramster
Last active January 2, 2016 12:39
Show Gist options
  • Save sriramster/8304896 to your computer and use it in GitHub Desktop.
Save sriramster/8304896 to your computer and use it in GitHub Desktop.
backup script
#!/bin/bash
# Move these stuffs to config, no need to check for last backed date. Run the script every week
DIR=('/home/sriramr/Mail/' '/home/sriramr/pub_webs/Wrk/cdapp/cdapp/')
FILES=('/home/sriramr/src/scratch' '/var/lib/iptable/active')
DROPBOX=('/home/sriramr/src/scratch' '/home/sriramr/src/notes/weekall.org' '/var/lib/iptables/active')
# Not used
CMN_BACKUP_FILE="lastbacked"
CMN_BACKUP_DIR="/store/bckup/"
# dropbox stuffs
DROP_DEST="/home/sriramr/Dropbox/"
#Mostly global's
date=$(date +%Y%m%d)
ldate=$(date)
log="run$date.log"
log_format="$ldate :: mzombie/bckup "
#technology begins here
init_dir()
{
local -a cnt
for i in "${DIR[@]}"
do
dir=$i
if [ -d $dir ]; then
for i in $(echo $i | tr "/" "\n")
do
name=$i
done
dir_bckup $dir $name
else
cat >> $log <<EOF
$log_format Error unable to find dir $i
EOF
fi
done
}
# Create a tgz and copies to sink folder
dir_bckup()
{
tar -Pczvf $CMN_BACKUP_DIR$2_$date.tgz $1
if [ $? == 0 ];then
cat >> $log <<EOF
$log_format 'Backed up $1 on'
EOF
elif [ $? == 2 ]; then
cat >> $log <<EOF
$log_format 'File exists'
EOF
else
cat >> $log <<EOF
$log_format 'Mail directory file not found '
EOF
fi
}
init_file()
{
echo "Running file backup.."
local -a cnt
len=${#DIR[*]}
for i in "${FILES[@]}"
do
file=$i
if [ -f $file ]; then
for i in $(echo $i | tr "/" "\n")
do
name=$i
done
file_bckup $file $name
fi
done
}
file_bckup()
{
dest=$3
oname=$CMN_BACKUP_DIR$2.gpg
passwd 6 | gpg -v --homedir '/home/sriramr/.gnupg' --batch -r '[email protected]' --passphrase-fd 0 --output $oname --encrypt $1
mv $oname $dest
if [ $? == 0 ];then
cat >> $log <<EOF
$log_format 'Backed up $1 on '
EOF
else
cat >> $log <<EOF
$log_format 'File $1 not found'
EOF
fi
}
init_dropbox()
{
echo "Running dropbox backup.."
local -a cnt
len=${#DROPBOX[*]}
for i in "${DROPBOX[@]}"
do
file=$i
if [ -f $file ]; then
for i in $(echo $i | tr "/" "\n")
do
name=$i
done
file_bckup $file $name $1
fi
done
}
# check_last_backed
case "$1" in
dropbox)
echo "Dropbox backing up.."
#destination
if [ $3 == "" ]; then
init_dropbox $2
else
init_dropbox $2 $3
fi
;;
code)
init_code
echo "code"
;;
mail)
init_mail
echo "Mail backup"
;;
all)
init
echo "Running all backup"
;;
*)
usage
;;
esac
# init_dir
# init_file
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment