File indexing completed on 2024-07-21 04:36:26

0001 #                mysqlcrypt.sh
0002 # This shell script will run KMyMoney in a MySql database
0003 # and uses the MySql dump program to maintain a copy of the data.
0004 # Additionally, another copy is written to a backup
0005 # directory before running KMyMoney. These backups will be deleted after a
0006 # user-specified number of days.
0007 
0008 # Optionally, these data files may be encrypted.
0009 # For encryption, it is necessary that you have the 'gpg' program installed,
0010 # and have set up a key identified by an email address (which may be a pseudo address).
0011 # See 'man gpg' for more info. The kgpg program will help set up these keys.
0012 # DO NOT forget the password associated with gpg; you will need it to access your data.
0013 
0014 # Optionally, the data can be removed from the database after KMyMoney finishes, 
0015 # and reloaded next time you run.
0016 
0017 # It is assumed that your logon user name has the necessary database permissions.
0018 
0019 # Please set the following variables to your requirements
0020 MYDIR=$HOME/money # directory where the encrypted copy is to be held
0021 BUDIR=$MYDIR/backup # directory where the backups should go;
0022 FILE=myfin # name for the encrypted copy
0023 DBNAME=KMyMoney # mysql database name
0024 DROP=y # (y/n) - whether to delete info from database after running kmm
0025 SILENT=n # if set to y, backups will be deleted silently, else you will be asked
0026 CRYPTEMAIL=me@googlemail.com # delete this line if you don't want encrypted copies
0027 declare -i KEEP=7 # number of days to keep backups
0028 # end of user-changeable directives
0029 
0030 if [ ! -d $MYDIR ] ; then
0031   mkdir $MYDIR;
0032 fi
0033 if [ ! -d $BUDIR ] ; then
0034   mkdir $BUDIR;
0035 fi
0036 
0037 if [ -z $CRYPTEMAIL ]; then
0038   EFILE=${FILE};
0039 else
0040   EFILE=${FILE}.gpg;
0041 fi
0042 
0043 declare NOW=`date +%Y%m%d%H%M%S`
0044 
0045 if [ ! -f $MYDIR/$EFILE ] ; then
0046   kdialog --warningcontinuecancel "A version of $FILE does not exist in $MYDIR.\nWhen KMyMoney starts, please open or create a file and use the 'File/Save as Database' function."
0047   if [ $? -ne 0 ]; then
0048     exit;
0049   fi
0050   KMMCMD=-n;
0051 else
0052   KMMCMD=sql://$USER@localhost/$DBNAME?driver=QMYSQL3
0053   # backup file
0054   cp $MYDIR/$EFILE $BUDIR/$NOW$EFILE
0055   LOAD=y
0056   mysql -e "use $DBNAME;" 2>/dev/null
0057   if [ $? -eq 0 ]; then
0058     kdialog --warningyesno "A $DBNAME database exists. Do you wish to run with this?\n If not, the database will be reloaded from the encrypted file"
0059     if [ $? -eq 0 ]; then # replied yes
0060       LOAD=n
0061     fi;
0062   fi;
0063   if [ $LOAD = y ]; then
0064     echo "Reloading from file"
0065     if [ -z $CRYPTEMAIL ]; then
0066       mysql <$MYDIR/$EFILE;
0067     else
0068       gpg --decrypt $MYDIR/$EFILE |mysql;
0069     fi;
0070   fi;
0071 fi
0072 #run kmymoney
0073 kmymoney $KMMCMD
0074 mysqldump --databases -R $DBNAME >$MYDIR/$FILE
0075 if [ ! -z $CRYPTEMAIL ]; then
0076   rm -f $MYDIR/$EFILE
0077   gpg -e -r $CRYPTEMAIL $MYDIR/$FILE
0078   rm $MYDIR/$FILE;
0079 fi
0080 
0081 case ${DROP:0:1} in
0082   y | Y) mysql -e "drop database KMyMoney;";;
0083 esac
0084 
0085 # delete outdated backup files
0086 cd $BUDIR
0087 for i in `find . -name "*${EFILE}" -ctime +${KEEP}`; do
0088   case ${SILENT:0:1} in
0089     y | Y) ANSWER=y
0090     ;;
0091     *) read -p "Delete $i?" ANSWER
0092     ;;
0093   esac
0094   case ${ANSWER:0:1} in
0095     y | Y) echo "Deleting $i!"
0096     rm $i
0097     ;;
0098   esac
0099 done