Loading variables in bash from another file - bash

I build some ROMs and other software for various Android devices and to make it easier for me, I use bash based scripts.
Here's an example from my TWRP build script:
#!/usr/bin/env bash
# Variables
export TW_DEVICE_VERSION="0"
export BRANCH="android-5.1"
# Don't touch this
VERSION=$( grep "TW_MAIN_VERSION_STR" bootable/recovery/variables.h -m 1 | cut -d \" -f2 )-${TW_DEVICE_VERSION}
# Acer Liquid Z500 specific TWRP build configuration
export BRAND="acer"
export DEVICE="acer_Z500"
git clone https://github.com/liquidporting/android_device_${BRAND}_${DEVICE}.git -b ${BRANCH} device/${BRAND}/${DEVICE}
. build/envsetup.sh
lunch omni_${DEVICE}-eng
mka recoveryimage > twrp_${DEVICE}.log
cd out/target/product/${DEVICE}
if [ -f "recovery.img" ]
then
mv recovery.img twrp-${VERSION}-${DEVICE}.img
else
echo ""
echo "*******************************************************************************"
echo "Something went wrong during the build process, try checking your device tree."
echo "After that, run the script again and see if you messed up something new or not."
echo "*******************************************************************************"
echo ""
fi
if [ -f "twrp-${VERSION}-${DEVICE}.img" ]
then
megarm /Root/LPAD/TWRP/twrp-${VERSION}-${DEVICE}.img
megarm /Root/LPAD/TWRP/twrp_${DEVICE}.log
megaput --no-progress --path /Root/LPAD/TWRP twrp-${VERSION}-${DEVICE}.img
megaput --no-progress --path /Root/LPAD/TWRP ../../../../twrp_${DEVICE}.log
fi
if [ -f "twrp-${VERSION}-${DEVICE}.img" ]
then
cd ../../../..
rm twrp_${DEVICE}.log
make clean
cd device
rm -rf ${BRAND}
cd ..
else
rm twrp_${DEVICE}.log
make clean
cd device
rm -rf ${BRAND}
cd ..
echo ""
echo "**************************************************************"
echo "The build process of TWRP Recovery failed for device ${DEVICE}"
echo "**************************************************************"
echo ""
exit
fi
# Lenovo A328 specific TWRP build configuration
export BRAND="lenovo"
export DEVICE="A328"
git clone https://github.com/liquidporting/android_device_${BRAND}_${DEVICE}.git -b ${BRANCH} device/${BRAND}/${DEVICE}
. build/envsetup.sh
lunch omni_${DEVICE}-eng
mka recoveryimage > twrp_${DEVICE}.log
cd out/target/product/${DEVICE}
if [ -f "recovery.img" ]
then
mv recovery.img twrp-${VERSION}-${DEVICE}.img
else
echo ""
echo "*******************************************************************************"
echo "Something went wrong during the build process, try checking your device tree."
echo "After that, run the script again and see if you messed up something new or not."
echo "*******************************************************************************"
echo ""
fi
if [ -f "twrp-${VERSION}-${DEVICE}.img" ]
then
megarm /Root/LPAD/TWRP/twrp-${VERSION}-${DEVICE}.img
megarm /Root/LPAD/TWRP/twrp_${DEVICE}.log
megaput --no-progress --path /Root/LPAD/TWRP twrp-${VERSION}-${DEVICE}.img
megaput --no-progress --path /Root/LPAD/TWRP ../../../../twrp_${DEVICE}.log
fi
if [ -f "twrp-${VERSION}-${DEVICE}.img" ]
then
cd ../../../..
rm twrp_${DEVICE}.log
make clean
cd device
rm -rf ${BRAND}
cd ..
else
rm twrp_${DEVICE}.log
make clean
cd device
rm -rf ${BRAND}
cd ..
echo ""
echo "**************************************************************"
echo "The build process of TWRP Recovery failed for device ${DEVICE}"
echo "**************************************************************"
echo ""
exit
fi
Is possible to have a separated bash script containing build instructions and another one containing set of variables?
I Mean something like this:
#!/usr/bin/env bash
export TW_DEVICE_VERSION="0"
export BRANCH="android-5.1"
VERSION=$( grep "TW_MAIN_VERSION_STR" bootable/recovery/variables.h -m 1 | cut -d \" -f2 )-${TW_DEVICE_VERSION}
export BRAND="acer"
export DEVICE="acer_Z500"
export BRAND="lenovo"
export DEVICE="A328"
export BRAND="doogee"
export DEVICE="X5"
But after each device specific configuration I need it to launch the bash script containing the build instructions.

Yes, that is very much feasible and it is a good thing to do as well.
Put your configuration in a file, say, tw_config.sh. Then, invoke the configuration script this way:
source /path/to/tw_config.sh
if [ $? -ne 0 ]; then
# couldn't load config
# your logic here - makes sense to exit
fi
If tw_config.sh is in your PATH, you can simply say:
source tw_config.sh

Related

Sending a file as a parameter to a container in order to be compiled causes problems

I'm facing some problems when sending a file to a docker container in order to be compiled and executed.
In the folder that I'm running the command I have a "compiler" folder and a file.cpp(a basic hello world"). I'm using this command:
sudo docker run --rm -it -v /compiler/ dockcompiler file.cpp
And this is the output:
useradd: warning: the home directory already exists.
Not copying any file from skel directory into it.
chgrp: cannot access '/code': No such file or directory
chmod: cannot access '/code': No such file or directory
file does not exist
My Dockerfile:
FROM ubuntu
RUN apt-get update
RUN apt-get install -y gcc g++
ADD entrypoint.sh entrypoint.sh
ADD run-code.sh run-code.sh
ENTRYPOINT ["/bin/bash", "entrypoint.sh"]
entrypoint.sh
prog=$1
uid=$2
if [ -z $1 ]; then
echo "you must provide a file"
exit 1
fi
if [ -z $uid ]; then
uid=10000
fi
echo "127.0.0.1 $(hostname)" >> /etc/hosts
groupadd code
useradd -u "$uid" -G code -d "/compiler" -m codecube
chgrp code /code
chmod 0775 /code
cd /compiler
sudo -u codecube /bin/bash /run-code.sh $prog
And the run-code.sh file(it works if I run it in terminal):
prog=$1
if [ -z $1 ]; then
echo "you must provide a file"
exit 1
fi
if [ ! -f $1 ]; then
echo "file does not exist"
exit 1
fi
extension="${prog##*.}"
case "$extension" in
"cpp")
g++ $prog && ./a.out
;;
"c")
gcc $prog && ./a.out
;;
"go")
go run $prog
;;
"pl")
perl $prog
;;
"py")
python $prog
;;
"rb")
ruby $prog
;;
*)
echo "invalid language"
exit 1
;;
esac
Updated:
The problem is -m codecube where the it says codecube directory already exists. Also, the /code directory is not present.
If the user directory is already in place on your docker image (assuming a modified image being used here). Then just skel files copy should do the trick and you can get away without doing useradd. A post here offers more info on this.
#copy skel files
cp -r /etc/skel/. /codecube

Run wget and other commands in shell script

I'm trying to create a shell script that I will download the latest Atomic gotroot rules to my server, unpack them, copy them to the correct folder, etc.,
I've been reading shell tutorials and forum posts for most of the day and the syntax escapes me for some of these. I have run all these commands and I know they work if I manually run them.
I know I need to develop some error checking, but I'm just trying to get the commands to run correctly. The main problem at the moment is the syntax of the wget commands, i've got errors about missing semi-colons, divide by zero, unsupported schemes - I've tried various quoting (single and double) and escaping - / " characters in various combinations.
Thanks for any help.
The raw wget command is
wget --user="jim" --password="xxx-yyy-zzz" "http://updates.atomicorp.com/channels/rules/subscription/VERSION"
#!/bin/sh
update_modsec_rules(){
wget=/usr/bin/wget
tar=/bin/tar
apachectl=/usr/bin/apache2ctl
TXT="Script Run Finished"
WORKING_DIR="/var/asl/updates"
TARGET_DIR="/usr/local/apache/conf/modsec_rules/"
EXISTING_FILES="/var/asl/updates/modsec/*"
EXISTING_ARCH="/var/asl/updates/modsec-*"
WGET_OPTS='--user=jim --password=xxx-yyy-zzz'
URL_BASE="http://updates.atomicorp.com/channels/rules/subscription"
# change to working directory and cleanup any downloaded files and extracted rules in modsec/ directory
cd $WORKING_DIR
rm -f $EXISTING_ARCH
rm -f $EXISTING_FILES
rm -f VERSION*
# wget to download VERSION file
$wget ${WGET_OPTS} "${URL_BASE}/VERSION"
# get current MODSEC_VERSION from VERSION file and save as variable
source VERSION
TARGET_DATE=$MODSEC_VERSION
echo $TARGET_DATE
# wget to download current archive
$wget ${WGET_OPTS} "${URL_BASE}/modsec-${TARGET_DATE}.tar.gz"
# extract archive
echo "extracting files . . . "
tar zxvf $WORKING_DIR/modsec-${TARGET_DATE}.tar.gz
echo "copying files . . . "
cp -uv $EXISTING_FILES $TARGET_DIR
echo $TXT
}
update_modsec_rules $# 2>&1 | tee -a /var/asl/modsec_update.log
RESTART_APACHE="/usr/local/cpanel/scripts/restartsrv httpd"
$RESTART_APACHE
Here are some guidelines to use when writing shell scripts.
Always quote variables when you use them. This helps avoid the possibility of misinterpretation. (What if a filename contains a space?)
Don't trust fileglobbing on commands like rm. Use for loops instead. (What if a filename starts with a hyphen?)
Avoid subshells when possible. Your lines with backquotes make me itchy.
Don't exec if you can help it. And especially don't expect any parts of your script after your exec to actually get run.
I should point out that while your shell may be bash, you've specified /bin/sh for execution of this script, so it is NOT a bash script.
Here's a rewrite with some error checking. Add salt to taste.
#!/bin/sh
# Linux
wget=/usr/bin/wget
tar=/bin/tar
apachectl=/usr/sbin/apache2ctl
# FreeBSD
#wget=/usr/local/bin/wget
#tar=/usr/bin/tar
#apachectl=/usr/local/sbin/apachectl
TXT="GOT TO THE END, YEAH"
WORKING_DIR="/var/asl/updates"
TARGET_DIR="/usr/local/apache/conf/modsec_rules/"
EXISTING_FILES_DIR="/var/asl/updates/modsec/"
EXISTING_ARCH="/var/asl/updates/"
URL_BASE="http://updates.atomicorp.com/channels/rules/subscription"
WGET_OPTS='--user="jim" --password="xxx-yyy-zzz"'
if [ ! -x "$wget" ]; then
echo "ERROR: No wget." >&2
exit 1
elif [ ! -x "$apachectl" ]; then
echo "ERROR: No apachectl." >&2
exit 1
elif [ ! -x "$tar" ]; then
echo "ERROR: Not in Kansas anymore, Toto." >&2
exit 1
fi
# change to working directory and cleanup any downloaded files
# and extracted rules in modsec/ directory
if ! cd "$WORKING_DIR"; then
echo "ERROR: can't access working directory ($WORKING_DIR)" >&2
exit 1
fi
# Delete each file in a loop.
for file in "$EXISTING_FILES_DIR"/* "$EXISTING_ARCH_DIR"/modsec-*; do
rm -f "$file"
done
# Move old VERSION out of the way.
mv VERSION VERSION-$$
# wget1 to download VERSION file (replaces WGET1)
if ! $wget $WGET_OPTS $URL_BASE}/VERSION; then
echo "ERROR: can't get VERSION" >&2
mv VERSION-$$ VERSION
exit 1
fi
# get current MODSEC_VERSION from VERSION file and save as variable,
# but DON'T blindly trust and run scripts from an external source.
if grep -q '^MODSEC_VERSION=' VERSION; then
TARGET_DATE="`sed -ne '/^MODSEC_VERSION=/{s/^[^=]*=//p;q;}' VERSION`"
echo "Target date: $TARGET_DATE"
fi
# Download current archive (replaces WGET2)
if ! $wget ${WGET_OPTS} "${URL_BASE}/modsec-$TARGET_DATE.tar.gz"; then
echo "ERROR: can't get archive" >&2
mv VERSION-$$ VERSION # Do this, don't do this, I don't know your needs.
exit 1
fi
# extract archive
if [ ! -f "$WORKING_DIR/modsec-${TARGET_DATE}.tar.gz" ]; then
echo "ERROR: I'm confused, where's my archive?" >&2
mv VERSION-$$ VERSION # Do this, don't do this, I don't know your needs.
exit 1
fi
tar zxvf "$WORKING_DIR/modsec-${TARGET_DATE}.tar.gz"
for file in "$EXISTING_FILES_DIR"/*; do
cp "$file" "$TARGET_DIR/"
done
# So far so good, so let's restart apache.
if $apachectl configtest; then
if $apachectl restart; then
# Success!
rm -f VERSION-$$
echo "$TXT"
else
echo "ERROR: PANIC! Apache didn't restart. Notify the authorities!" >&2
exit 3
fi
else
echo "ERROR: Apache configs are broken. We're still running, but you'd better fix this ASAP." >&2
exit 2
fi
Note that while I've rewritten this to be more sensible, there is certainly still a lot of room for improvement.
You have two options:
1- changing this to
WGET1=' --user="jim" --password="xxx-yyy-zzz" "http://updates.atomicorp.com/channels/rules/subscription/VERSION"'
then run
wget $WGET1 same to WGET2
Or
2- encapsulating $WGET1 with backquotes ``.
e.g.:
`$WGET`
This applies to any command your executing out of a variable.
Suggested changes:
#!/bin/sh
TXT="GOT TO THE END, YEAH"
WORKING_DIR="/var/asl/updates"
TARGET_DIR="/usr/local/apache/conf/modsec_rules/"
EXISTING_FILES="/var/asl/updates/modsec/*"
EXISTING_ARCH="/var/asl/updates/modsec-*"
WGET1='wget --user="jim" --password="xxx-yyy-zzz" "http://updates.atomicorp.com/channels/rules/subscription/VERSION"'
WGET2='wget --user="jim" --password="xxx-yyy-zzz" "http://updates.atomicorp.com/channels/rules/subscription/modsec-$TARGET_DATE.tar.gz"'
## change to working directory and cleanup any downloaded files and extracted rules in modsec/ directory
cd $WORKING_DIR
rm -f $EXISTING_ARCH
rm -f $EXISTING_FILES
## wget1 to download VERSION file
`$WGET1`
## get current MODSEC_VERSION from VERSION file and save as variable
source VERSION
TARGET_DATE=`echo $MODSEC_VERSION`
## WGET2 command to download current archive
`$WGET2`
## extract archive
tar zxvf $WORKING_DIR/modsec-$TARGET_DATE.tar.gz
cp $EXISTING_FILES $TARGET_DIR
## restart server
exec '/usr/local/cpanel/scripts/restartsrv_httpd' $*;
Pro Tip: If you need string substitution, using ${VAR} is much better to eliminate ambiguity, e.g.:
tar zxvf $WORKING_DIR/modsec-${TARGET_DATE}.tar.gz

bad character showing up in bash script execution

I have a bash script that is getting an accented character appended to some strings that is causing it to fail, and I can't find where or how these characters are getting in there.
Here is some example output:
mv: cannot move â/tmp/myapp.zipâ to â/opt/myserver/myapp/deploys/myapp.1.2.21.zipâ: No such file or directory
ln: failed to create symbolic link â/opt/myserver/myapp/deploys/myapp_beta.zipâ: No such file or directory
cp: cannot stat â/opt/myserver/myapp/deploys/myapp_beta.zipâ: No such file or directory
the invalid character is the â.
The script is below:
#!/bin/bash
BRANCH=$1
SVN_LOC="https://svn/svn/myserver/"
MYAPP_REPO="myapp.git"
COREJS_REPO="core-js.git"
SPARTAN_REPO="core-spartan.git"
MYAPP_LOCATION="myapp/"
COREJS_LOCATION="corejs/"
SPARTAN_LOCATION="spartan/"
DEPLOY_LOCATION="/tmp/deploy/"
CLEANUP="${DEPLOY_LOCATION}*"
DEPLOY_STORE="/opt/myserver/myapp/deploys/"
DEPLOY_TIME=$(date +%s)
failed ()
{
rm -rf $CLEANUP
exit 1
}
mkdir -p $DEPLOY_LOCATION
echo "Retrieving Code from Git Branch ${BRANCH}"
echo "Retrieving myapp code"
mkdir -p "${DEPLOY_LOCATION}${MYAPP_LOCATION}"
pushd /opt/myserver/myapp/myapp
git archive $BRANCH | tar -x -C "${DEPLOY_LOCATION}${MYAPP_LOCATION}"
if [ $? -ne 0 ]
then
echo "Failed retrieving code from git ${MYAPP_REPO} repo";
failed
fi
popd
echo "Checking version numbers"
VERSION=$(php "${DEPLOY_LOCATION}${MYAPP_LOCATION}version.php" output)
DEPLOY_PACKAGE="${DEPLOY_STORE}myapp.${VERSION}.zip"
if [ -f $DEPLOY_PACKAGE ]
then
echo "A deploy with the same version number (${VERSION}) already exists! Please increment version number or manually deal with existing ${DEPLOY_PACKAGE}";
failed
fi
echo "Retrieving corejs code"
mkdir -p "${DEPLOY_LOCATION}${COREJS_LOCATION}"
pushd /opt/myserver/myapp/core-js
git archive $BRANCH | tar -x -C "${DEPLOY_LOCATION}${COREJS_LOCATION}"
if [ $? -ne 0 ]
then
echo "Failed retrieving code from git ${COREJS_REPO} repo";
failed
fi
popd
echo "Retrieving spartan code"
mkdir -p "${DEPLOY_LOCATION}${SPARTAN_LOCATION}"
pushd /opt/myserver/myapp/spartan
git archive $BRANCH | tar -x -C "${DEPLOY_LOCATION}${SPARTAN_LOCATION}"
if [ $? -ne 0 ]
then
echo "Failed retrieving code from git ${SPARTAN_REPO} repo";
failed
fi
popd
echo "Minifying js and css"
pushd "${DEPLOY_LOCATION}${MYAPP_LOCATION}Server/Deploy/"
php MinifyLyroke.php --deploytime $DEPLOY_TIME
popd
ASSETS_DEPLOY_PACKAGE="${DEPLOY_STORE}myappassets.${VERSION}.zip"
TEMP_ASSETS_ZIP_LOC="/tmp/myappassets.zip"
DEPLOY_ASSETS="${DEPLOY_LOCATION}myapp/Assets/"
ASSETS_DEPLOY_LOCATION="/tmp/assetsdeploy/"
DEPLOYED_ASSETS="${ASSETS_DEPLOY_LOCATION}myappassets_${DEPLOY_TIME}"
mkdir -p $ASSETS_DEPLOY_LOCATION
echo "Packaging assets deploy to ${ASSETS_DEPLOY_PACKAGE}"
mv $DEPLOY_ASSETS $DEPLOYED_ASSETS
pushd $ASSETS_DEPLOY_LOCATION
zip -r ${TEMP_ASSETS_ZIP_LOC} *
popd
mv ${TEMP_ASSETS_ZIP_LOC} ${ASSETS_DEPLOY_PACKAGE}
ln -sfn ${ASSETS_DEPLOY_PACKAGE} "${DEPLOY_STORE}myappassets_beta.zip"
cp "${DEPLOY_STORE}myappassets_beta.zip" "/opt/myserver/myapp/myapp/Server/Deploy/"
rm -rf $DEPLOYED_ASSETS
rm -rf $ASSETS_DEPLOY_LOCATION
echo "Packaging deploy to ${DEPLOY_PACKAGE}"
TEMP_ZIP_LOC="/tmp/myapp.zip"
pushd ${DEPLOY_LOCATION}
zip -r ${TEMP_ZIP_LOC} *
popd
mv "${TEMP_ZIP_LOC}" "${DEPLOY_PACKAGE}"
ln -sfn "${DEPLOY_PACKAGE}" "${DEPLOY_STORE}myapp_beta.zip"
cp "${DEPLOY_STORE}myapp_beta.zip" "/opt/myserver/myapp/myapp/Server/Deploy"
echo "Cleaning up"
rm -rf $CLEANUP
can anyone possibly see the issue or suggest a way I can go about finding where the issue is?
Those â characters are just mangled smart quotes printed from your shell. Your shell is probably outputting UTF-8, but your terminal is reading ISO-8859-1. Note that â is the rendering of a UTF-8 encoded smart quote ‘ in ISO-8859-1, with two nonprintable characters following the â. Most modern terminal emulators come with an option to enable UTF-8; see if you can enable that (it will make your life easier).
The problem is in your script, not the funny characters.
Try opening the script in another text editor like Notepad++ and see if there are any special characters present.
From the command line, type both of these commands. One or more of the files/directories you are expecting to exist, does not exist.
ls /tmp/myapp.zip
ls /opt/myserver/myapp/deploys
The accepted answer explains the problem, thanks #nneonneo. This is what you can do for a quick fix:
A) check your locale settings with:
locale
B) before calling your script or in the top of your bash-script try:
export LANG=en_US.UTF-8
export LC_ALL=C

unix bash syntax error near unexpected token 'do'

I created a unix minecraft launcher. it worked perfectly fine just an hour and a half ago (as of 9:30). then I got this:
/home/axium1998/MinecraftMegaLauncher.sh: line 14: syntax error near unexpected token ~'$'do\r''.
/home/Axium1998/MinecraftMegaLauncher.sh: line 14: 'do
I have no idea what caused this.
# If code needs to be changed, just send me a PM saying something like: Project:MinecraftMegaLauncher Line #<line number> = <changed code>
# if it works (I bet it will, but for me to learn xP )it will be replaced/fixed.
export mc=$HOME/.minecraft
export mcB=$HOME/officialBackup
export tekkit=$HOME/.technic
export tekkitB=$HOME/tekkitBackup
export ftb=$HOME/.feedthebeast
export ftbB=$HOME/ftbBackup
export options=("Official" "MagicLauncher" "Tekkit" "FTB" "Backup" "Restore" "Quit")
echo "==========MinecraftMegaLauncher=========="
echo "This currently supports the following launchers: Official, Magic, Tekkit, and FTB, and doing backups as well!"
echo "I (AXIUM1998) am not responsible for data loss/corruption while backing up/restoring. (It is still indev)"
echo "Also, if there is a launcher you want to be in this mega launcher, I will consider implementing them."
echo "BUG: Running restore twice in a row (running restore, then running it again immeditely) will erase all mc data."
cd $HOME
select optL in "${options[#]}"
do
case $optL in
"Official")
echo "Starting the Official launcher..."
java -jar minecraft.jar
;;
"MagicLauncher")
echo "Starting the MagicLauncher..."
java -jar magic.jar
;;
"Tekkit")
echo "Starting the Tekkit launcher..."
java -jar tekkit.jar
;;
"FTB")
echo "Starting the FTB launcher..."
java -jar ftb.jar
;;
"Quit")
echo "Quitting..."
break
;;
"Backup")
echo "Starting the backup..."
echo "Please input your password (Admin needed :( )"
sudo touch dv
sudo rm dv
if [ ! -d $mcB ]; then
sudo mkdir $HOME/officialBackup
fi
if [ ! -d $tekkitB ];then
sudo mkdir $HOME/tekkitBackup
fi
if [ ! -d $ftbB ]; then
sudo mkdir $HOME/ftbBackup
fi
cd $mcB
sudo rm -rf *
cd $tekkitB
sudo rm -rf *
cd $ftbB
sudo rm -rf *
sudo cp -R $mc/* $mcB/
sudo cp -R $tekkit/* $tekkitB/
sudo cp -R $ftb/* $ftbB/
echo "Backup complete"
echo "Making current user owner of files..."
sudo chown -R $USER $mcB
sudo chown -R $USER $tekkitB
sudo chown -R $USER $ftbB
echo "User $USER now can write to backed up folders"
;;
"Restore")
echo "Starting the restoration..."
echo "Admin is, again, required :( "
sudo touch dv
sudo rm dv
cd $mc
sudo rm -rf *
cd $tekkit
sudo rm -rf *
cd $ftb
sudo rm -rf *
cd $HOME
sudo mv $mcB/* $mc/
sudo mv $tekkitB/* $tekkit/
sudo mv $ftbB/* $ftb/
echo "Restore complete"
;;
*)
echo "Invalid operand.";;
esac
done
edit: may not be exact line. I changed it after I last uploaded it
My wild guess is that you converted your script to Windows format (perhaps copying it from Windows) and then you receive this error: unexpected do\r because the \r is unexpected.
Use dos2unix to convert it.

rdiff-backup errors: script keeps quitting with error

Ive recently been introduced to bash scripting... So, Ive used my advanced theft course to throw together the attached script. it runs... and exits with "/xxx/ not mounted. You are not root! I have rdiff-backup and sshfs installed and working. The commands work fine on their own on the commandline, but in the script, well... Can you guys take a look and lemme know? PS I copied a LOT of this from scripts I found here and a few other places.
<code>
#!/bin/bash
# Version 1.5
# Prior to running this make sure you have ssh-keygen -t rsa to generate a key, then
# ssh username#target "mkdir .ssh/;chmod 700 .ssh"
# scp .ssh/id_rsa.pub username#target:.ssh/authorized_keys
#
# then check you can login and accept the ssh key
# ssh username#target "ls -la"
#
# Key things to remember, no spaces in pathnames, and try to use full paths (beginning with / )
#
# variables determine backup criteria
DATESTAMP=`date +%d%m%y`
USERNAME=username #remote site username here
TARGET=remote.ip.here #add the ip v4 address of the target
INCLUDES=/path/to/file/includes.txt #this is a txt file containing a list of directories you want backed up
EXCLUDES="**" #this is a list of files etc you want to skip
BACKUPLOG=/path/to/logfile/in/home/backuplog${DATESTAMP}.txt
OLDERTHAN=20W #change 20 to reflect how far back you want backups to exist
# to activate old backup expiry, uncomment the line below
#RMARGS=" --force --remove-older-than ${OLDERTHAN}"
TARGETMAIL="yourmailaddress#your.domain"
HOSTNAME=`hostname` #Dont change this!
TMPDIR=/backups Change this to the source folder
TARGETFOLDER=/backups change this to the TARGET folder
ARGS=" -v0 --terminal-verbosity 0 --exclude-special-files --exclude-other-filesystems --no-compression -v6"
# detecting distro and setting the correct path
if [ -e /etc/debian_version ];then
NICE=/usr/bin/nice
elif [ -e /etc/redhat-release ];then
NICE=/bin/nice
fi
if [ -e /tmp/backup.lock ];then
exit 0
fi
touch /tmp/backup.lock
touch -a ${BACKUPLOG}
cd /
/bin/mkdir -p ${TMPDIR}
/usr/bin/sshfs -o idmap=user -o ${USERNAME}#${TARGET}:/${TARGETFOLDER} ${TMPDIR} &>${BACKUPLOG}
# if you get errors mounting this then try
# mknod /dev/fuse -m 0666 c 10 229
for ITEMI in ${INCLUDES} ; do
ARGS="${ARGS} --include ${ITEMI} "
done
for ITEME in ${EXCLUDES} ; do
ARGS="${ARGS} --exclude-regexp '${ITEME}' "
done
# the --exclude ** / is a hack because it wont by default do multiple dirs, so use --include for all dirs then exclude everything else and sync / - if you dont understand dont worry
# ref: http://www.mail-archive.com/rdiff-backup-users#nongnu.org/msg00311.html
#echo /usr/bin/rdiff-backup ${ARGS} --exclude \'**\' / ${TMPDIR}/ &&
cat ${INCLUDES} | while read DIR; do
${NICE} -19 /usr/bin/rdiff-backup --exclude '**' ${DIR} ${TMPDIR}/ &>${BACKUPLOG}
if [ $? != 0 ]; then
echo "System Backup Failed" | mutt -s "Backup Log: System Backup Failed, Log attached!" -a ${BACKUPLOG} ${TARGETMAIL}
exit 1;
fi
done
#${NICE} -19 /usr/bin/rdiff-backup ${ARGS} --exclude '**' / ${TMPDIR}/ &>${BACKUPLOG} &&
echo Removing backups older than ${RMARGS}
${NICE} -19 /usr/bin/rdiff-backup -v0 --terminal-verbosity 0 ${RMARGS} ${TMPDIR}/ &>${BACKUPLOG}
/bin/umount ${TMPDIR} && /bin/rm -rf ${TMPDIR}/ &>${BACKUPLOG}
echo "System Backup Run" | mutt -s "Backup Log: System Backup Done!" -a ${BACKUPLOG} ${TARGETMAIL}
rm /tmp/backup.lock
rm ${BACKUPLOG}
Sorry, cannot paste, couldnt attach... BLIKSEM!
Thanks for ANY input... One HELL of a learning curve!!!
Regards,
B.

Resources