I have a file named backup.sh, I have installed and set path variable too. but when i double click backup.sh file its saying something which I don't know. scenario is back mongo db database backup using script in grails.
#!/bin/bash
#
# Michael Mottola
# <mikemottola#gmail.com>
# December 18, 2011
#
# Creates backup files (bson) of all MongoDb databases on a given server.
# Default behaviour dumps the mongo database and tars the output into a file
# named after the current date. ex: 2011-12-19.tar.gz
#
### Set server settings
HOST="localhost"
PORT="27017" # default mongoDb port is 27017
USERNAME=""
PASSWORD=""
# Set where database backups will be stored
# keyword DATE gets replaced by the current date, you can use it in either path below
BACKUP_PATH="Desktop/path/to/backup/directory" # do not include trailing slash
FILE_NAME="DATE" #defaults to [currentdate].tar.gz ex: 2011-12-19.tar.gz
##################################################################################
# Should not have to edit below this line unless you require special functionality
# or wish to make improvements to the script
##################################################################################
# Auto detect unix bin paths, enter these manually if script fails to auto detect
MONGO_DUMP_BIN_PATH="$(which mongodump)"
TAR_BIN_PATH="$(which tar)"
# Get todays date to use in filename of backup output
TODAYS_DATE=`date "+%Y-%m-%d"`
# replace DATE with todays date in the backup path
BACKUP_PATH="${BACKUP_PATH//DATE/$TODAYS_DATE}"
# Create BACKUP_PATH directory if it does not exist
[ ! -d $BACKUP_PATH ] && mkdir -p $BACKUP_PATH || :
# Ensure directory exists before dumping to it
if [ -d "$BACKUP_PATH" ]; then
cd $BACKUP_PATH
# initialize temp backup directory
TMP_BACKUP_DIR="mongodb-$TODAYS_DATE"
echo; echo "=> Backing up Mongo Server: $HOST:$PORT"; echo -n ' ';
# run dump on mongoDB
if [ "$USERNAME" != "" -a "$PASSWORD" != "" ]; then
$MONGO_DUMP_BIN_PATH --host $HOST:$PORT -u $USERNAME -p $PASSWORD --out $TMP_BACKUP_DIR >> /dev/null
else
$MONGO_DUMP_BIN_PATH --host $HOST:$PORT --out $TMP_BACKUP_DIR >> /dev/null
fi
# check to see if mongoDb was dumped correctly
if [ -d "$TMP_BACKUP_DIR" ]; then
# if file name is set to nothing then make it todays date
if [ "$FILE_NAME" == "" ]; then
FILE_NAME="$TODAYS_DATE"
fi
# replace DATE with todays date in the filename
FILE_NAME="${FILE_NAME//DATE/$TODAYS_DATE}"
# turn dumped files into a single tar file
$TAR_BIN_PATH --remove-files -czf $FILE_NAME.tar.gz $TMP_BACKUP_DIR >> /dev/null
# verify that the file was created
if [ -f "$FILE_NAME.tar.gz" ]; then
echo "=> Success: `du -sh $FILE_NAME.tar.gz`"; echo;
# forcely remove if files still exist and tar was made successfully
# this is done because the --remove-files flag on tar does not always work
if [ -d "$BACKUP_PATH/$TMP_BACKUP_DIR" ]; then
rm -rf "$BACKUP_PATH/$TMP_BACKUP_DIR"
fi
else
echo "!!!=> Failed to create backup file: $BACKUP_PATH/$FILE_NAME.tar.gz"; echo;
fi
else
echo; echo "!!!=> Failed to backup mongoDB"; echo;
fi
else
echo "!!!=> Failed to create backup path: $BACKUP_PATH"
fi
its giving --host command not found. updated for later help.
bash FilePath/backup.sh
For example if you have backup.sh file in E:/SohamShetty/Files then you should do like this.
bash E:/SohamShetty/Files/backup.sh
You can run it as :
bash backup.sh
or
chmod +x backup.sh
./backup.sh
Related
Some weeks ago I found in this site a very useful bash script that downloads images from google image results (download images from google with command line)
Although the script is quite complicate for me, I did some simple modifications so as not to rename the results so as to keep the original names.
However, since the last week, the script stopped working... probably Google updated the code or something, and the regexes of the script don't parse the results any more. I don't know enough about google's codes, web programing or regexing to see what is wrong, although I did some educated guesses, but still didn't work.
My (unworking) tweaked script is this
#! /bin/bash
# function to create all dirs til file can be made
function mkdirs {
file="$1"
dir="/"
# convert to full path
if [ "${file##/*}" ]; then
file="${PWD}/${file}"
fi
# dir name of following dir
next="${file#/}"
# while not filename
while [ "${next//[^\/]/}" ]; do
# create dir if doesn't exist
[ -d "${dir}" ] || mkdir "${dir}"
dir="${dir}/${next%%/*}"
next="${next#*/}"
done
# last directory to make
[ -d "${dir}" ] || mkdir "${dir}"
}
# get optional 'o' flag, this will open the image after download
getopts 'o' option
[[ $option = 'o' ]] && shift
# parse arguments
count=${1}
shift
query="$#"
[ -z "$query" ] && exit 1 # insufficient arguments
# set user agent, customize this by visiting http://whatsmyuseragent.com/
useragent='Mozilla/5.0 (X11; Ubuntu; Linux i686; rv:31.0) Gecko/20100101 Firefox/31.0'
# construct google link
link="www.google.cz/search?q=${query}\&tbm=isch"
# fetch link for download
imagelink=$(wget -e robots=off --user-agent "$useragent" -qO - "$link" | sed 's/</\n</g' | grep '<a href.*\(png\|jpg\|jpeg\)' | sed 's/.*imgurl=\([^&]*\)\&.*/\1/' | head -n $count | tail -n1)
imagelink="${imagelink%\%*}"
# get file extention (.png, .jpg, .jpeg)
ext=$(echo $imagelink | sed "s/.*\(\.[^\.]*\)$/\1/")
# set default save location and file name change this!!
dir="$PWD"
file="google image"
# get optional second argument, which defines the file name or dir
if [[ $# -eq 2 ]]; then
if [ -d "$2" ]; then
dir="$2"
else
file="${2}"
mkdirs "${dir}"
dir=""
fi
fi
# construct image link: add 'echo "${google_image}"'
# after this line for debug output
google_image="${dir}/${file}"
# construct name, append number if file exists
if [[ -e "${google_image}${ext}" ]] ; then
i=0
while [[ -e "${google_image}(${i})${ext}" ]] ; do
((i++))
done
google_image="${google_image}(${i})${ext}"
else
google_image="${google_image}${ext}"
fi
# get actual picture and store in google_image.$ext
wget --max-redirect 0 -q "${imagelink}"
# if 'o' flag supplied: open image
[[ $option = "o" ]] && gnome-open "${google_image}"
# successful execution, exit code 0
exit 0
one way to invetigate : provide -x option to bash so to have the trace of your script; that is change /bin/bash to /bin/bash -x in your script -or- simply invoke your script with
bash -x <yourscript>
You can also annotate your script with echo commands to track some variables.
I am coding a script to select some IPs from a table in a DB, and then use IPTables rules to ban theses IPs, last step is to notify by e-mail, but I am getting 2 errors:
#!/bin/bash
Now=$(date +"%d-%m-%Y %T")
fileLocation="/var/lib/mysql/DBName/"
fileName="ip2ban.txt"
filePath=$fileLocation$fileName
curLocation=$(pwd)
#Connect to DB and select ban_ip
mysql -u root -pPASSWORD -D DBName -e 'SELECT ip INTO OUTFILE "'$filePath'" FROM ban_ip WHERE ip_tables = "0"' >> banIP.log 2>&1
selRes=$?
# If the command was successful
if [ $selRes -eq "0" ]
then
# We need to check if the file exists on the saved location
#find $fileLocation -type f -iname "ip2ban.txt" -empty => To check if file empty or not
if [ -f $filePath ]
then
mv $filePath $curLocation'/ip2ban.txt'
# Connect to DB and update the ban_ip
mysql -u root -pPASSWORD -D DBName -e 'UPDATE ban_ip SET ip_tables = "1" WHERE ip_tables = "0"' >> banIP.log 2>&1
upRes=$?
if [ $upRes -eq "0" ]
then
# Send message for succesful result
echo -e "Database updated with new banned IPs on $Now \nThank you for using this script" 2>&1 | sed '1!b;s/^/To: myID#gmail.com\nSubject: New banned IPs[Success]\n\n/' | sendmail -t
else
# Send message for failure result
echo -e "We cannot update the ban_ip table on $Now \nThank you for using this script" 2>&1 | sed '1!b;s/^/To: myID#gmail.com\nSubject: [Failure] New banned IPs\n\n/' | sendmail -t
fi
fi
else
echo 'Something wrong with Select statment on' $Now >> banIP.log
fi
# Save IPTables rules
iptables-save > /root/Scripts/IPTables/BannedIPs.conf // LIGNE 53
I am getting 2 errors:
line 53: iptables-save: command not found
line 37: sendmail: command not found
However the sendamil is already installed, with mail, postfix:
# which sendmail
/usr/sbin/sendmail
# which mail
/usr/bin/mail
# which postfix
/usr/sbin/postfix
Thanks for your usual support
According to the crontab(5) man page for Linux:
PATH is set to "/usr/bin:/bin".
Meaning your shell script will not be able to find anything under /usr/sbin or /sbin. Change this with by adding the following near the top of your script:
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
Also the environment can be set from within the crontab. See https://stackoverflow.com/a/14694543/5766144 for how to do this.
To make my question more precise, here is my script. Getting unexpected end of file error. Can somebody point out what I'm missing.
#!/bin/bash
set -x
#
DZHOST='XX.XX.XX.XXX'
DZUSER='ftpuser'
DZSPASS='Ftppass'
DSHOST='XX.XX.XXX.XXX'
DSUSER='userftp'
DSPASS='passftp'
#
DNASN='/home/dsmn/ASN'
{
if [ "$(ls -A $DNASN)" ];
then
ftp -n -u $DZSHOST <<-ASN_END
quote USER $DZUSER
quote PASS $DZPASS
prompt
cd /home/DS/PROD/Import/ASN
lcd ASN
bin
mput *.*
bye
ASN_END
cp $DNASN/*.* ./backup/
fi
}
DNGRA='/home/ds/GRA'
#{
#if [ "$(ls -A $DNGRA)" ];
# then
# ftp -n -u $DSHOST <<-GRA_END
# quote USER $DSUSER
# quote PASS $DSPASS
# prompt
# cd GRA
# lcd GRA
# bin
# mput *.*
# bye
# GRA_END
# cp $DNGRA/*.* ./backup/
#fi
#}
DNGRB='/home/ds/GRB'
{
if [ "$(ls -A $DNGRB)" ];
then
ftp -n -u $DZHOST <<-GRB_END
quote USER $DZUSER
quote PASS $DZSPASS
prompt
cd /home/DS/PROD/Import/GRB
lcd GRB
bin
mput *.*
bye
GRB_END
cp $DNGRB/*.* ./backup/
fi
}
Some parts are remarked since the ftp access is not yet finalized. had to change the directory names,etc because of confidentiality so I apologize if there are inconsistencies.
I get this error that points to the end of the script.
syntax error: unexpected end of file
Many thanks from a newbie.
Of course you can include as many if statements in your script as you like. You can even have nested if's.
Yet maybe a case statement will make your code much cleaner...
you could try looping through your folders and using case to decide what to do with each one of them.
Here's the code:
#!/bin/bash
# -------------------------------------------------
# -------------------------------------------------
# Use a comma-delimited list of single-quoted
# strings of the usernames to batch
# if it's empty it will backup all user directories
# in /home
# -------------------------------------------------
# -------------------------------------------------
USER_ACCOUNT=();
# -------------------------------------------------
# -------------------------------------------------
# Make sure the aws script is installed on the
# server, and the bucket name to upload these too
# are exact... case-sensitive
# -------------------------------------------------
# -------------------------------------------------
S3BUCKET='Kevs-Bucket/Test';
# Loop through the user array
# If it's empty, then get all users in the /home
# directory, based on each folder
# do not include root user
if [ ${#USER_ACCOUNT[#]} -eq 0 ]; then
# turn off dotglob and nullglob
cd /home;
shopt -s dotglob;
shopt -s nullglob;
DIRARR=(*/);
# we have our directories, now loop them and grab the user
# once we have the user, skip the root user
for d in ${!DIRARR[#]}; do
# Assign an account variable
ACCT=stat -c '%U' ${DIRARR[$i]}; #NOT WORKING HERE
if [ "$ACCT" == "root" ]; then
echo "ROOT";
else
run_backup $ACCT $S3BUCKET;
fi;
done;
else
# we have our list, now loop through them all
for i in ${!USER_ACCOUNT[#]}; do
# Assign an account variable
ACCT=${USER_ACCOUNT[$i]};
run_backup $ACCT $S3BUCKET;
done;
fi;
# -------------------------------------------------
# -------------------------------------------------
# Run the actual backup
run_backup(){
LOGFILE=/batch-move.log
# Package the account
./scripts/pkgacct $1;
echo '##########################################' >> $LOGFILE;
echo "# Start: date +'%T'" >> $LOGFILE;
echo "# Backing Up: $1" >> $LOGFILE;
echo '##########################################' >> $LOGFILE;
# Upload it to S3
s3put $2/cpmove-$1.tar.gz /home/cpmove-$1.tar.gz;
echo '##########################################' >> $LOGFILE;
echo "# Uploading Backup: $1" >> $LOGFILE;
echo '##########################################' >> $LOGFILE;
# Remove the file from the server
/bin/rm -f /home/cpmove-$1.tar.gz;
echo '##########################################' >> $LOGFILE;
echo "# Removing Backup Up: $1" >> $LOGFILE;
echo "# Finish: date +'%T'" >> $LOGFILE;
echo '##########################################' >> $LOGFILE;
}
I'm getting an error here ACCT=stat -c '%U' ${DIRARR[$i]}; #NOT WORKING HERE and the error is stating that -c is not a valid option for stat on my CentOS server
I have verified through other means that stat -c does work, so I assume that my code attempting to get the folders owner into a variable is incorrect.
Can you help me figure it out?
The line that's not working (below) contains the variable $i which you have not defined and the $() notation is not present, see added code below "EDIT - Try this".
ACCT=stat -c '%U' ${DIRARR[$i]}; #NOT WORKING HERE
You are looping through your arrays in an unusual way. Here is an example of how to loop through the elements of an array of filenames in Bash.
files=( "/home/User/FileName1" "/home/User/FileName2" "/home/User/FileName3" )
for fileName in "${files[#]}" ; do
echo "$fileName"
done
Also instead of using globbing to build your array – DIRARR=(*/); – you might want to consider using a loop to iterate over the files, for example:
for fileName in /home/* ; do
echo "$fileName"
done
Hope this helps.
EDIT - Try this:
Note: On my system the following ignores '.' and '..'.
# To avoid confusion 'ACCT' would be better named as 'OWNER'.
# Loop through the files in: /home/
for filename in /home/* ; do
# Get the owner of $filename.
ACCT=$(stat -c '%U' "$filename")
# If the file is a directory NOT owned by root, run backup.
if [ -d "$filename" -a "$ACCT" != "root" ]; then
# Uncomment when satisfied
# run_backup "$ACCT" "$S3BUCKET"
echo "Run backup - not owned by root:"
echo "filename: $filename owner: $ACCT"
# If the file is a directory owned by root, DO NOT run backup.
elif [ -d "$filename" -a "$ACCT" = "root" ]; then
# Remove elif clause when satisfied.
echo "Do not run backup - owned by root:"
echo "filename: $filename owner: $ACCT"
fi
done
Please note use of $() in the "ACCT=" line.
I run the following script on my server:
#!/bin/bash
# System + MySQL backup script
# Full backup day - Fri (rest of the day do incremental backup)
# Copyright (c) 2005-2006 nixCraft <http://www.cyberciti.biz/fb/>
# This script is licensed under GNU GPL version 2.0 or above
# Automatically generated by http://bash.cyberciti.biz/backup/wizard-ftp-script.php
# ---------------------------------------------------------------------
### System Setup ###
DIRS="/var/www /etc"
BACKUP=/tmp/backup.$$
NOW=$(date +"%d-%m-%Y")
INCFILE="/root/tar-inc-backup.dat"
DAY=$(date +"%a")
FULLBACKUP="Fri"
### MySQL Setup ###
MUSER="xxx"
MPASS="xxx"
MHOST="localhost"
MYSQL="$(which mysql)"
MYSQLDUMP="$(which mysqldump)"
GZIP="$(which gzip)"
### FTP server Setup ###
FTPD="/backup/incremental"
FTPU="xxx"
FTPP="xxx"
FTPS="xxx"
NCFTP="$(which ncftpput)"
### Other stuff ###
EMAILID="xxx"
### Start Backup for file system ###
[ ! -d $BACKUP ] && mkdir -p $BACKUP || :
### See if we want to make a full backup ###
if [ "$DAY" == "$FULLBACKUP" ]; then
FTPD="/backup/full"
FILE="fs-full-$NOW.tar.gz"
tar -zcvf $BACKUP/$FILE $DIRS
else
i=$(date +"%Hh%Mm%Ss")
FILE="fs-i-$NOW-$i.tar.gz"
tar -g $INCFILE -zcvf $BACKUP/$FILE $DIRS
fi
### Start MySQL Backup ###
# Get all databases name
DBS="$($MYSQL -u $MUSER -h $MHOST -p$MPASS -Bse 'show databases')"
for db in $DBS
do
FILE=$BACKUP/mysql-$db.$NOW-$(date +"%T").gz
$MYSQLDUMP -u $MUSER -h $MHOST -p$MPASS $db | $GZIP -9 > $FILE
done
### Dump backup using FTP ###
#Start FTP backup using ncftp
ncftp -u"$FTPU" -p"$FTPP" $FTPS<<EOF
mkdir $FTPD
mkdir $FTPD/$NOW
cd $FTPD/$NOW
lcd $BACKUP
mput *
quit
EOF
### Find out if ftp backup failed or not ###
if [ "$?" == "0" ]; then
rm -f $BACKUP/*
T=/tmp/backup.pass
echo "Date: $(date)">$T
echo "Hostname: $(hostname)" >>$T
echo "Backup passed" >>$T
mail -s "BACKUP PASSED" "$EMAILID" <$T
rm -f $T
else
T=/tmp/backup.fail
echo "Date: $(date)">$T
echo "Hostname: $(hostname)" >>$T
echo "Backup failed" >>$T
mail -s "BACKUP FAILED" "$EMAILID" <$T
rm -f $T
fi
Everything is working well, except that it always tells me everything went well, even tho the remote ftp server is full. Can someone help me changing this, so it gives me an error, when the remote server is full?
Also, is there any way to remove old backups?
Thanks,
Daniel