In this area you´ll find some usefull scripts.
The following script will check whether a file in a directory isn´t older than 65 minutes and not smaller than 250k
#!/bin/sh t=0 first_check () { timestamp=`/usr/bin/find /opt/backup/mysql_hourly/* -mmin -65 | wc -l` if [ $timestamp -ne "0" ] then echo "Actual file exists" 1> /dev/null size=`/usr/bin/find /opt/backup/mysql_hourly/* -size -250k | wc -l` if [ $size -eq "0" ] then echo "Backup is working" 1> /dev/null t=1 else echo "Backup has failed - filesize < 250k" 1> /dev/null fi else echo "Backup has failed - file timestamp > 65 minutes" 1> /dev/null fi } second_check () { timestamp=`/usr/bin/find /opt/backup/mysql_hourly/* -mmin -65 | wc -l` if [ $timestamp -ne "0" ] then echo "Actual file exists" 1> /dev/null size=`/usr/bin/find /opt/backup/mysql_hourly/* -size -250k | wc -l` if [ $size -eq "0" ] then echo "Backup is working" 1> /dev/null else echo "`date` Backup has failed - filesize < 250k" >> /var/log/test_backup_errorlog fi else echo "`date` Backup has failed - file timestamp > 65 minutes" >> /var/log/test_backup_errorlog fi } inuse=1 while [ $inuse -ne "0" ] do inuse=`lsof +D /opt/backup/mysql_hourly/ | wc -l` if [ $inuse -eq "0" ] then first_check if [ $t -eq "0" ] then second_check else echo "Backup is working" 1> /dev/null fi fi sleep 20 done
Check (W)LAN adapter and reset if necessary:
#!/bin/sh ################################################################## # # Title: check_wlan0 # # Script checks to see if WiFi has a network IP and if not # restart WiFi # # Uses a lock file which prevents the script from running more # than one at a time. If lockfile is old, it removes it # # Instructions: # # o Install where you want to run it from like /usr/local/bin # o chmod 0755 /usr/local/bin/check_wlan0.sh # o Add to crontab # # Run Every 5 mins - Seems like ever min is over kill unless # this is a very common problem. If once a min change */5 to * # once every 2 mins */5 to */2 ... # # */5 * * * * /usr/local/bin/check_wlan0.sh # ################################################################## # Settings # Where and what you want to call the Lockfile lockfile='/var/run/check_wlan0.pid' #logger "logfile: $lockfile" # Which Interface do you want to check/fix # Example: wlan0, eth0, etc. wlan='wlan0' ################################################################## echo echo "check: $wlan" #date echo # Check to see if there is a lock file if [ -e $lockfile ]; then # A lockfile exists... Lets check to see if it is still valid pid=`cat $lockfile` if kill -0 &>1 > /dev/null $pid; then # Still Valid... lets let it be... echo "Process still running, Lockfile valid" exit 1 else # Old Lockfile, Remove it echo "Old lockfile, Removing Lockfile" rm $lockfile fi fi # If we get here, set a lock file using our current PID# #echo "Setting Lockfile" echo $$ > $lockfile # We can perform check echo "Performing Network check for $wlan" #if ifconfig $wlan | grep -q "inet addr:" ; then if /sbin/ifconfig $wlan | /bin/grep "inet addr:" ; then echo "Network is Okay" #logger "Network is Okay" else echo "Network connection down! Attempting reconnection." /sbin/ifdown $wlan logger "Network connection down! Attempting reconnection." logger "ifdown $wlan - sleep 5" /bin/sleep 5 /sbin/ifup --force $wlan /sbin/ifconfig $wlan | grep "inet addr" logger `/sbin/ifconfig $wlan | grep "inet addr"` /bin/sleep 5 fi echo echo "Current Setting:" ifconfig $wlan | grep "inet addr:" echo # Check is complete, Remove Lock file and exit echo "process is complete, removing lockfile" rm $lockfile exit 0 ################################################################## # End of Script ##################################################################
https://lunetikk.de/doku.php?id=linux:raspberry:raspbian:troubleshooting
Script for passiv check with nsca:
#/bin/sh #LOGDIR="/var/log/rsyslog" #LOGDIR2="/var/log/rsyslog/cas" NSCA_BIN="/usr/sbin/send_nsca" NSCA_CONF="/etc/send_nsca.cfg" TIMEOUT="10" ICINGA_SERVER="192.168.63.25" ICINGA_CHECK="Backup Status Check" #DATE=`date +%a` DATE=`date +%d.%m.%y` #COUNTER=0 SERVER="`hostname`" BACKUPLOG="/backup/log/backup.log" ERRORLOG="/backup/log/error.log" #OUTPUT=$RESULT if [ ! -e $BACKUPLOG ] then RESULT=1 /bin/echo $MESSAGE /bin/echo "File doesn´t exist, RESULT= $RESULT" /bin/echo -e "$SERVER\t$ICINGA_CHECK\t$RESULT\t$MESSAGE\n" | $NSCA_BIN -H $ICINGA_SERVER -c $NSCA_CONF else RESULT=0 MESSAGE="Backup Successfully" /bin/echo $MESSAGE /bin/echo "File exists, RESULT= $RESULT" if [ ! -s $BACKUPLOG ] then RESULT=1 MESSAGE="Backup failed" /bin/echo "BACKUPLOG empty RESULT= $RESULT (expected 1)" /bin/echo -e "$SERVER\t$ICINGA_CHECK\t$RESULT\t$MESSAGE\n" | $NSCA_BIN -H $ICINGA_SERVER -c $NSCA_CONF exit 1 else RESULT=0 MESSAGE="Backup Successfully" /bin/echo "Errorlog with content: $MESSAGE" /bin/echo $MESSAGE /bin/echo "BACKUPLOG with content, RESULT= $RESULT (expected 0)" if [ -s $ERRORLOG ] then RESULT=1 MESSAGE="Backup failed" /bin/echo "Errorlog with content: $MESSAGE" /bin/echo -e "$SERVER\t$ICINGA_CHECK\t$RESULT\t$MESSAGE\n" | $NSCA_BIN -H $ICINGA_SERVER -c $NSCA_CONF fi fi fi
#!/bin/bash #author: Thomas Roehm #version: 1.2 BACKUPDIR="/mnt/backup/raspberrypi/backup" LOGFILE="${BACKUPDIR}/`hostname -f`/backup.log" mkdir ${BACKUPDIR} 2> /dev/null #echo "mounting share to backup-server" ########################################################################################## #mount backup-device###################################################################### ########################################################################################## #mount -t cifs -o lfs //hostname.local/linux_images ${BACKUPDIR} -o username=tmade #if [ $? -ne "0" ] #then # echo "mounting device failed!" # exit 1 #else # echo "device on backup-server mounted" #fi ########################################################################################### #create backup-folder###################################################################### ########################################################################################### FILE=${BACKUPDIR}/`hostname -f` if [ -d $FILE ] then echo "Backup of host '`hostname -f`' already exists." echo "Rename it to '`hostname -f`.old'" mv ${BACKUPDIR}/`hostname -f` ${BACKUPDIR}/`hostname -f`.old #else #echo "'$FILE' does not exist." fi mkdir ${BACKUPDIR}/`hostname -f`/ 2> /dev/null cd ${BACKUPDIR}/`hostname -f`/ ############################################################################################ #backup partition table (sd*-devices)####################################################### ############################################################################################ #fdisk -l 2> /dev/null |grep GB |cut -f2 -d ' ' |cut -f1 -d ':' |grep -v "/dev/dm" |grep -v "/dev/mapper" > hd fdisk -l 2> /dev/null |grep -E '(G|i)B' |cut -f2 -d ' ' |cut -f1 -d ':' |grep -v "/dev/dm" |grep -v "/dev/mapper" |grep -v "loop" > hd HDCOUNT=`cat hd |wc -l` for ((i=1; i <=$HDCOUNT; i++)) do j=0 hdarray=($(sed -n "$i"p hd)) echo "Device = ${hdarray[$j]}" temp=${hdarray[$j]} echo $temp | sed 's/\/dev\/sd/dev_sd/g' t=`echo $temp | sed 's/\/dev\/sd/dev_sd/g'` sfdisk -d ${hdarray[$j]} > ${t}_part_table ((j++)) done ############################################################################################ #backup partition table (/dev/cciss/c0d*-devices)########################################### ############################################################################################ HDCOUNT=`cat hd |wc -l` for ((i=1; i <=$HDCOUNT; i++)) do j=0 hdarray=($(sed -n "$i"p hd)) echo "Device = ${hdarray[$j]}" temp=${hdarray[$j]} echo $temp | sed 's/\/dev\/cciss\/c0d/dev_cciss_c0d/g' t=`echo $temp | sed 's/\/dev\/cciss\/c0d/dev_cciss_c0d/g'` sfdisk -d ${hdarray[$j]} > ${t}_part_table ((j++)) done ############################################################################################ #backup partition table (/dev/mmcblk*-devices)########################################### ############################################################################################ HDCOUNT=`cat hd |wc -l` for ((i=1; i <=$HDCOUNT; i++)) do j=0 hdarray=($(sed -n "$i"p hd)) echo "Device = ${hdarray[$j]}" temp=${hdarray[$j]} echo $temp | sed 's/\/dev\/mmcblk/dev_mmcblk/g' t=`echo $temp | sed 's/\/dev\/mmcblk/dev_mmcblk/g'` sfdisk -d ${hdarray[$j]} > ${t}_part_table ((j++)) done rm hd ########################################################################################### #pv_setup################################################################################## ########################################################################################### pv_setup () { echo "start function pv_setup" ########################################################################################### #lvm-backup################################################################################ ########################################################################################### echo "creating lvm-backup" vgcfgbackup --file ${BACKUPDIR}/`hostname -f`/%s_vgcfgbackup.bku ########################################################################################### #count lvm-vg's############################################################################ ########################################################################################### VG_COUNT=`ls -al *vgcfgbackup.bku | wc -l` ########################################################################################### #check LVM mounts/ lv's#################################################################### ########################################################################################### /bin/ls *vgcfgbackup.bku | sed 's/_vgcfgbackup.bku//g' > temp_vg echo ###################################################################################### echo "following volume groups found on system:" echo "" cat temp_vg echo ###################################################################################### for ((i=1; i <=$VG_COUNT; i++)) do VG=`sed -n "$i"p temp_vg` sed -n -e '/logical_volumes/,$p' ${VG}_vgcfgbackup.bku |grep -B 1 "id" |grep -v "id" |cut -f1 -d '{' |grep -v "-" |cut -f3 > lvm_mounts_vg_${VG} done rm temp_vg } ########################################################################################### #tar backup################################################################################ ########################################################################################### backup () { cd ${BACKUPDIR}/`hostname -f`/ echo "" pwd echo "" echo "creating tar-backup of all mountpoints" echo "" echo "creating root-backup:" echo "" tar -czf root.tgz --one-file-system / && EXIT=$(echo $?) || EXIT=$(echo $?) if [ $EXIT -eq "0" ] then echo "root-backup created" >> summary else if [ $EXIT -eq "1" ] then echo "root-backup created with exit code \"$EXIT\" (files changed)" >> summary else echo "root-backup failed!" >> summary fi fi #tar -czf root.tgz --ignore-failed-read --warning='no-file-ignored' --warning=no-file-changed --one-file-system --exclude '/opt/InteProxy-SERVER-1.0.4/*' / && echo "root-backup created" >> summary || echo "root-backup failed!" >> summary echo "" echo "creating var-backup:" echo "" tar -czf var.tgz --one-file-system --exclude '/var/splunk/*' --exclude '/var/oldlogs' /var && EXIT=$(echo $?) || EXIT=$(echo $?) if [ $EXIT -eq "0" ] then echo "var-backup created" >> summary else if [ $EXIT -eq "1" ] then echo "var-backup created with exit code \"$EXIT\" (files changed)" >> summary else echo "var-backup failed!" >> summary fi fi echo "" echo "creating boot-backup:" echo "" tar -czf boot.tgz --one-file-system /boot && EXIT=$(echo $?) || EXIT=$(echo $?) if [ $EXIT -eq "0" ] then echo "boot-backup created" >> summary else if [ $EXIT -eq "1" ] then echo "boot-backup created with exit code \"$EXIT\" (files changed)" >> summary else echo "boot-backup failed!" >> summary fi fi echo "creating data-backup:" echo "" tar -czf data.tgz --one-file-system /data && EXIT=$(echo $?) || EXIT=$(echo $?) if [ $EXIT -eq "0" ] then echo "data-backup created" >> summary else if [ $EXIT -eq "1" ] then echo "data-backup created with exit code \"$EXIT\" (files changed)" >> summary else echo "data-backup failed!" >> summary fi fi echo "summary:" echo "`date +%d.%m.%Y_%H.%M.%S`" | tee -a $LOGFILE cat summary | tee -a $LOGFILE rm summary } ########################################################################################### #check if lvm-devices are available######################################################## ########################################################################################### #pvscan > pv_temp pvscan 2> /dev/null > pv_temp grep PV pv_temp if [ $? -ne "0" ] then echo "no physical volumes found" rm pv_temp backup else echo "physical volumes found" rm pv_temp pv_setup backup fi
#!/bin/sh echo "please enter hostname (fqdn) of the system to restore (without spaces!)" read HOST; echo "overview of existing devices and partitions:" echo "" cat /proc/partitions echo "" cat /proc/partitions > partitions_temp grep sda1 partitions_temp || grep cciss/c0d0p1 partitions_temp if [ $? == 1 ] then echo "no partitions on system found" rm partitions_temp else echo "partitions on system found!" echo "Please delete system-partitions with fdisk (check Wiki for instructions) and rerun the restore.sh script." rm partitions_temp exit 1 fi echo "" echo "overview of existing LVM-setup:" echo "" pvscan vgscan lvscan echo "" pvscan > pv_temp grep PV pv_temp if [ $? == 1 ] then echo "no physical volumes found" rm pv_temp else echo "physical volumes found, please remove LVM-setup and rerun restore.sh" rm pv_temp exit 1 fi echo "please enter device name to restore partition-table (without spaces!). example: sda , cciss/c0d0" echo "" read DEV; DEV_FORM=`echo $DEV | sed 's/cciss\/c0d/cciss_c0d/g'` cat /proc/partitions > /mnt/backup/${HOST}/check_part grep cciss /mnt/backup/${HOST}/check_part if [ $? == 1 ] then echo "sda-device found" rm /mnt/backup/${HOST}/check_part sfdisk --force /dev/$DEV < /mnt/backup/${HOST}/dev_${DEV}_part_table else echo "cciss-device found" rm /mnt/backup/${HOST}/check_part sfdisk --force /dev/$DEV < /mnt/backup/${HOST}/dev_${DEV_FORM}_part_table fi echo "please enter the name of the partition to restore LVM (without spaces!). example: sda2 , cciss/c0d0p2 , cciss/c0d1p2 ..." echo "" read PART; echo "please enter the name of the volume-group to restore (without spaces!). example: system, data .." echo "" read PV; echo "please enter the name of the boot-device (without spaces!). example: sda1 , cciss/c0d0p1 .." echo "" read BOOT; ID=`grep -m 2 "id = " /mnt/backup/${HOST}/${PV}_vgcfgbackup.bku |tail -n 1 | cut -f2 -d'"'` echo "ID=$ID" echo "PV=$PV" echo "DEV=$DEV" echo "PART=$PART" echo "restoring PV" pvcreate --uuid $ID --restorefile /mnt/backup/${HOST}/${PV}_vgcfgbackup.bku /dev/$PART echo "restoring VG/ LV" vgcfgrestore --file /mnt/backup/${HOST}/${PV}_vgcfgbackup.bku $PV vgchange -ay lvscan echo "format all devices:" mkfs.ext3 -Lhome /dev/$PV/home mkfs.ext3 -Lroot /dev/$PV/root mkswap -Lswap /dev/$PV/swap mkfs.ext3 -Ltmp /dev/$PV/tmp mkfs.ext3 -Lusr /dev/$PV/usr mkfs.ext3 -Lvar /dev/$PV/var mkfs.ext3 -Lboot /dev/${BOOT} echo "creating root-mountpoint" echo"" mkdir /mnt/root mount /dev/$PV/root /mnt/root cd /mnt/root echo "restoring root:" tar -xzf /mnt/backup/$HOST/root.tgz if [ $? == 1 ] then echo ################################################ echo "restore of root-backup failed" echo ################################################ exit else echo ################################################ echo "root-backup restored" echo ################################################ fi echo "mounting var, usr, home, tmp, boot" mount /dev/$PV/var var mount /dev/$PV/usr usr mount /dev/$PV/home home mount /dev/$PV/tmp tmp mount /dev/${BOOT} boot echo ################################################ echo "restore var" echo ################################################ tar -xzf /mnt/backup/$HOST/var.tgz echo "var-backup restored" echo ################################################ echo "restore usr" echo ################################################ tar -xzf /mnt/backup/$HOST/usr.tgz echo "usr-backup restored" echo ################################################ echo "restore home" echo ################################################ tar -xzf /mnt/backup/$HOST/home.tgz echo "home-backup restored" echo ################################################ echo "restore boot" echo ################################################ tar -xzf /mnt/backup/$HOST/boot.tgz echo "home-backup restored" echo ################################################ echo "install mbr" echo ################################################ grub --batch <<EOT device (hd0) /dev/$DEV root (hd0,0) setup (hd0) quit EOT echo "" file=/mnt/root/etc/udev/rules.d/70-persistent-net.rules if [ -e $file ] then echo "edit "70-persistent-net.rules"" echo "" > /mnt/root/etc/udev/rules.d/70-persistent-net.rules echo "70-persistent-net.rules edited" fi cd / echo "" umount /dev/$PV/var umount /dev/$PV/usr umount /dev/$PV/home umount /dev/$PV/tmp umount /dev/${BOOT} umount /dev/$PV/root echo "" fsck /dev/$PV/var fsck /dev/$PV/usr fsck /dev/$PV/home fsck /dev/$PV/tmp fsck /dev/${BOOT} fsck /dev/$PV/root echo "" echo "restore of $HOST done" mount /dev/$PV/root /mnt/root mount /dev/$PV/var var mount /dev/$PV/usr usr mount /dev/$PV/home home mount /dev/$PV/tmp tmp mount /dev/${BOOT} boot chmod 1777 tmp echo "" echo "please remove (out-commenting) all non-system /mnt/root/etc/fstab entries with vi before restart the system! example: /opt/oracle , /opt/whatever/ ..."
To automate adding vsftpd-users on SLES11 and SLES12:
#!/bin/sh LOGFILE="/var/log/add_vsftpd.log" LOGERR="/var/log/add_vsftpd-error.log" GREP=$(which grep) ECHO=$(which echo) USERADD=$(which useradd) SYSTEMCTL=$(which systemctl 2> /dev/null) CAT=$(which cat) MAILADDR="" FTPUID=3199 FTPUIDMIN=3150 ERROR=0 #Check OS Version function f_checkOS () { OS=$($GREP -i "VERSION" /etc/SuSE-release |cut -d " " -f3) if [ "${OS}" -eq "11" ] then #$ECHO "Operating System = SLES$OS" OFFVSFTPD=$(chkconfig vsftpd |cut -d " " -f3) if [ "${OFFVSFTPD}" == "off" ] then $ECHO "add vsftpd to autostart" /sbin/chkconfig vsftpd xinetd OFFXINETD=$(/sbin/chkconfig xinetd |cut -d " " -f3) fi if [ "${OFFXINETD}" == "off" ] then $ECHO "add xinetd to autostart" /sbin/chkconfig xinetd on $ECHO "start xinetd" /sbin/service xinetd restart else $ECHO "start xinetd" /sbin/service xinetd restart fi else #Check vsftpd enabled via xinetd $GREP -i disable /etc/xinetd.d/vsftpd | $GREP -iq "^#" T1=$? if [ "${T1}" -ne "0" ] ; then $ECHO "enabling vsftpd" sed -i '/disable/s|^|#|' /etc/xinetd.d/vsftpd #else #$ECHO "vsftpd is already enabled - nothing to do" fi XINETDSTAT=$(systemctl is-enabled xinetd.service) if [ "${XINETDSTAT}" = "disabled" ] then #$ECHO "add xinetd to autostart" $SYSTEMCTL enable xinetd.service $ECHO "start xinetd" $SYSTEMCTL restart xinetd.service else $ECHO "restart xinetd" $SYSTEMCTL restart xinetd.service fi fi } function f_readftpinfos () { #Useradd read -p "Please enter the Ticketnumber: " TNUMBER $ECHO "Ticketnumber \"$TNUMBER\" added!" $ECHO "" read -p "Please enter FTP-Username: " FTPUSER $ECHO "FTP-Username \"$FTPUSER\" added!" $ECHO "" read -p "Please enter FTP-Userpassword: " FTPPW $ECHO "FTP-Userpassword \"$FTPPW\" added!" $ECHO "" read -p "Please enter FTP-Userdescription: " FTPCOMMENT $ECHO "FTP-Userdescription \"$FTPCOMMENT\" added!" $ECHO "" read -p "Please enter FTP-Path (example: /usr/sap/interfaces/abc/cdf): " FTPPATH $ECHO "FTP-Path \"$FTPPATH\" added!" $ECHO "" read -p "Please enter Groupname (default-group: sapsys): " GROUPNAME GROUPNAME=${GROUPNAME:-sapsys} $ECHO "Groupname \"$GROUPNAME\" added!" $ECHO "" read -p "Please enter Emailadress: " MAILADDR $ECHO "Emailadress \"$MAILADDR\" added!" $ECHO "" FIRSTNAME=$(echo ${MAILADDR^} | cut -d "." -f1) } #Add user to ftp-config function f_ftpserviceadd () { $ECHO "$FTPUSER" >> /etc/vsftpd.users } function f_searchfreeUID () { CHECK="true" while [ "$CHECK" == "true" ] do T2=$($CAT /etc/passwd | /usr/bin/cut -d ":" -f3 | $GREP $FTPUID) if [ -z ${T2} ] ; then #$ECHO "Freie UID \"${FTPUID}\" gefunden" CHECK="false" else FTPUID=`expr $FTPUID - 1` if [ "$FTPUID" -eq "$FTPUIDMIN" ]; then $ECHO "Min UID \"${FTPUIDMIN}\" reached! Please contact COM-Support." CHECK="false" #else #$ECHO "Naechste freie UID \"$FTPUID\" gefunden" fi fi done } function f_addftpaccount () { ERROR=$($USERADD -d "$FTPPATH" -u "$FTPUID" -g "$GROUPNAME" -s /bin/false -c "$FTPCOMMENT" -m -p $(openssl passwd -1 "$FTPPW") "$FTPUSER" 3>&2 2>&1 1>&3) T3=$? if [ "${T3}" -ne "0" ] ; then $ECHO "FTP-User "$FTPUSER" not added. Please contact COM-Support!" else $ECHO "FTP-User "$FTPUSER" added" fi } function f_logandmail () { if [ "${T3}" -ne "0" ] ; then $ECHO "FTP Useradd - ERRORS REPORTED!" | mail -s "FTP Useradd - ERRORS REPORTED!" -S "smtp=x.x.x.x" "${MAILADDR}" -r "${MAILADDR}" $ECHO "------------------------------------------------------------------------" >> ${LOGERR} $ECHO "$ERROR" >> ${LOGERR} $ECHO "Ticketnumber "$TNUMBER" executed as user $USER at `date +%H:%M_%d.%m.%y`" >> ${LOGERR} $ECHO "FTP-User "$FTPUSER" not added. Please contact Support!" >> ${LOGERR} $ECHO "------------------------------------------------------------------------" >> ${LOGERR} else $CAT <<EOF | mail -s "FTP Useradd Information" -S "smtp=x.x.x.x" "${MAILADDR}" -r "${MAILADDR}" Some text... ##################################################### Ticketnummer: $TNUMBER FTP-Username: $FTPUSER FTP-Password: $FTPPW FTP-Pfad: $FTPPATH ##################################################### Some more text... EOF $ECHO "------------------------------------------------------------------------" >> ${LOGFILE} $ECHO "Ticketnumber "$TNUMBER" executed as user $USER at `date +%H:%M_%d.%m.%y`" >> ${LOGFILE} $ECHO "FTP-Username: $FTPUSER" >> ${LOGFILE} $ECHO "FTP-Pfad: $FTPPATH" >> ${LOGFILE} $ECHO "------------------------------------------------------------------------" >> ${LOGFILE} fi } f_readftpinfos f_searchfreeUID f_addftpaccount f_ftpserviceadd f_checkOS f_logandmail
Automate sftp-upload through proxy-server!
Usage:
./sftpupload.sh "ftpusername" "/path/to/sourcefiles/" "/path/to/archivedir/"
#!/bin/bash #set -x #$1 FTPUSER #$2 FILEPATH (local source path) #$3 ARCHIVEDIR #$4 FTPPATH (path on ftp-server) if [ -z ${1} ] || [ -z ${2} ] || [ -z ${3} ]; then echo "Usage: $0 \"FTPUSER\" \"FILEPATH\" \"ARCHIVEDIR\"" exit 1 fi PROXY="proxy.local:80" #PROXY="5.6.7.8:80" #LOGFILE="/var/log/sftpupload.log" NC=$(which nc) ARCHIVEDIR=$3 LOGFILE="${ARCHIVEDIR}sftpupload.log" #ARCHIVEDIR="/some/dir/ARCHIVE/" FILEPATH=$2 #FILEPATH="/ftptest/test/" #FTPFILENAME="*.csv" FTPFILENAME=$(ls $FILEPATH 2> /dev/null |grep .csv) #FTP-Hostname HOST="123sftp.com" #FTP-Username FTPUSER=$1 echo "FTPUSER=\"$FTPUSER\"" #FTPUSER=${USER} #Destination path on FTP-Server FTPPATH="/incoming/" #FTPPATH=$4 echo $(date +%d.%m.%Y_%H.%M) >> $LOGFILE #Check if local path exists if [ -d ${FILEPATH} ] then echo "FILEPATH=\"$FILEPATH\"" else echo "Path \"${FILEPATH}\" doesn´t exist!" | tee -a $LOGFILE exit 1 fi #Check if local file exists if [ -f ${FILEPATH}${FTPFILENAME} ] then echo "File \"$FTPFILENAME\" exists - starting to archive" cp -a ${FILEPATH}${FTPFILENAME} ${ARCHIVEDIR}${FTPFILENAME}.$(date +%H.%M_%d.%m.%Y) echo "file \"${FTPFILENAME}\" archived!" else echo "No file in folder!" | tee -a $LOGFILE echo "Filename: \"$FTPFILENAME\"" exit 1 fi #sftp -v -oProxyCommand='/bin/nc -v -X connect -x 1.2.3.4:3128 %h %p' ${FTPUSER}@${HOST} << EOFFTP #sftp -b - ${FTPUSER}@${HOST} << EOFFTP #Connect to FTP via SecureFTP (SSH needed) and upload files from FILEPATH to FTPPATH sftp -o ProxyCommand="'${NC}' -X connect -x '${PROXY}' %h %p" ${FTPUSER}@${HOST} << EOFFTP put ${FILEPATH}${FTPFILENAME} ${FTPPATH} quit EOFFTP T1=$? if [ "${T1}" -ne "0" ] then echo "Upload failed! Filename: \"${FTPFILENAME}\"" | tee -a $LOGFILE exit 1 else echo "File \"${FTPFILENAME}\" has been uploaded to ftp-server!" | tee -a $LOGFILE echo $LOGFILE rm ${FILEPATH}${FTPFILENAME} echo "File \"${FTPFILENAME}\" has been deleted from source!" exit 0 fi
Additional key-file:
sftp -oIdentityFile=/home/user/.ssh/id_rsa_2019 ...
SFTP-Server:
#!/bin/bash PGPFILEPATH="/home/scripts" PASSPHRASEFILE="/home/scripts/passphrase" ARCHIVEDIR="/home/scripts/archive" #Check if local path exists if [ -d ${PGPFILEPATH} ] then echo "PGPFILEPATH=\"$PGPFILEPATH\"" else echo "Path \"${PGPFILEPATH}\" doesn´t exist!" | tee -a $LOGFILE exit 1 fi #Check if local path exists if [ -d ${ARCHIVEDIR} ] then echo "ARCHIVEDIR=\"$ARCHIVEDIR\"" else echo "Path \"${ARCHIVEDIR}\" doesn´t exist!" | tee -a $LOGFILE exit 1 fi cd $PGPFILEPATH array=(*.pgp) arraylength=${#array[@]} echo $arraylength for filename in "${array[@]}" do echo "Filename=$filename" gpg --batch --passphrase-file=$PASSPHRASEFILE $filename done mv *.pgp $ARCHIVEDIR
#!/bin/sh LOGFILE="/var/log/logfile.log" LOGERR="/var/log/error.log" { # IO redirection for logging. touch ${LOGFILE} exec 6>&1 # Link file descriptor #6 with stdout. # Saves stdout. exec > ${LOGFILE} # stdout replaced with file ${LOGFILE}. touch ${LOGERR} exec 7>&2 # Link file descriptor #7 with stderr. # Saves stderr. exec 2> ${LOGERR} # stderr replaced with file ${LOGERR}. }
Delete all *.log & *.gz files in folder “/var/log/tomcat6” older than 14 Days:
find /var/log/tomcat6/* -name "*.gz" -mtime +14 -exec rm -f {} \; && find /var/log/tomcat6/* -name "*.log" -mtime +14 -exec rm -f {} \;
Copy all files from a list (restore_clients_test.txt) from folder “/data” to “/mnt/target/”:
for (( i = 1; i <=3; i++ )); do file=`sed -n "$i"p restore_clients_test.txt`; echo $file; cp -r data/$file /mnt/target/.; done
#!/bin/sh while true; do echo `date` >> /data/testfile ls -al /data/testfile sleep 10; done &