Showing posts with label script. Show all posts
Showing posts with label script. Show all posts

Wednesday, August 23, 2023

Create script backup on windows

Ref : Batch script to make backup folder only new and modified files

----------------Script.bat----------------------------

@echo off


set destination=E:\Backup_Express

set source=D:\Eexpress


for /F "tokens=2-4 delims=/ " %%i in ('date /t') do set yyyymmdd1=%%j.%%i.%%k

xcopy %source%"\*" %destination%"\*" /m/e/y

echo Last Backup Date %yyyymmdd1% >> E:\Backup_Express\Log_backup_express.txt

echo A folder containing the latest date has been created in root directory of %source%.

echo Finished copying %source% to %destination%

echo.

----------------End Script--------------------------- 

Create schedule


 


Friday, July 21, 2023

Map network drive on windows server sv2019 share file on ubuntu desktop 22.04.2

1. Windows Server 2019

Step

Create a shared file and add a user to access the file.

-D:\Fileshare

-E:\Backup

2. Ubuntu Desktop 22.04.2

Step

2.1 Create 2 Folder for mount file

sudo mkdir /media/user/Fileshare

sudo mkdir /media/user/backup

2.2 Mount file by command

- Fileshare

sudo -S mount -t cifs -w -o username=usewindows -o password=passworduser //192.168.x.xx/fileshare /media/user/Fileshare/

- Backup

sudo -S mount -t cifs -w -o username=usewindows -o password=passworduser //192.168.x.xx/backup /media/user/backup/

2.3 Run test rsync backup withe delete source and then destination follow.

sudo rsync -ahrvz --delete --stats --progress /media/user/Fileshare/ /media/user/backup/



Tuesday, March 29, 2016

script test ping

          วันนี้ไปเจอเว็บไซต์หนึ่ง เขียนวิธีทำสคริป Test ping server จึงลองเอามาเทสดู ผลคือ ok แต่จำได้ว่าแต่ก่อนเคยทำสคริปแบบนี้ไว้ ถ้าปิงไม่เจอ server มันจะ alert message เข้ามือถือเลย สคริปนั้นหายไปพร้อม ๆ กับเว็บไซต์ multiply.com

          วิธีการ
1. สร้างไฟล์ สคริปชื่อ testping.sh

แก้ไข
HOSTS="............"
EMAILID=".................."

-----------------------------------testping.sh-----------------------------------------
root@mail:~# cat testping.sh
#!/bin/bash
# add ip / hostname
HOSTS="IP or Host server"

# no ping request
COUNT=1

# email report when
SUBJECT="Ping failed"
EMAILID="email@mydomain.com"
for myHost in $HOSTS
do
  count=$(ping -c $COUNT $myHost | grep 'received' | awk -F',' '{ print $2 }' | awk '{ print $1 }')
  if [ $count -eq 0 ]; then
    # 100% failed
    echo "Host : $myHost is down (ping failed) at $(date)" | mail -s "$SUBJECT" $EMAILID
  fi
done
---------------------------------End---------------------------------------------------

2. เปลี่ยน permission
chmod u+x testping.sh

3. ตั้งเวลา crontab ให้สคริปทำงานตามเวลาที่เราต้องการ
crontab -e

ให้สคริปเช็คปิงทุก 1 ชม.
--------------------------------------------------------------------------------------
*/1 * * * * /path script/testping.sh

--------------------------------------------------------------------------------------

อ้างอิง: shell-scripts/monitoring-server-ecaoene-ping

Test ping ไปยังหลายๆ เครื่อง Server

bash-script-check-servers-availability

Tuesday, September 29, 2015

auto shutdown / restart window 7

Reference Social.technet:

1.   Launch Task Scheduler.
2.   Click Action and select Create Basic task.
3.   Type AutoRestart (or others you want) in the Name box and click Next.
4.   Select Daily and click Next
5.   Type the time you want to restart the computer and click Next.
6.   Select Start a program and click Next.
7.   Click Brower and navigate to %SystemRoot%\System32 and select Shutdown.exe and click Open. Type –R -T 3 -F in the Add arguments (optional) box and click Next.
8.   Click Finish.

or 

create bat file autoshutdown.bat

c:\windows\system32\shutdown.exe /R /T 3 /F


Friday, May 29, 2015

Backup, check space and delete old file backup

1.Backup file

------------------------script backup.sh---------------------------------------------------------
###mount -o username=administrator,password=xxxx //IP address/Path /mnt/backup

LISTS="/home"
BACKUPLAST=2
BACKUP_PATH="/media/Backup"

DAY_DELETE=`date -d "+%Y%m%d"`
DAY_BACKUP=`date "+%Y%m%d"`
DELETE="${BACKUP_PATH}/${DAY_DELETE}.tar.bz2"
BACKUP="${BACKUP_PATH}/${DAY_BACKUP}.tar.bz2"

if [ -s ${DELETE} ]
then
   rm -rf ${DELETE}
fi
if [ -s ${BACKUP} ]
then
   rm -rf ${BACKUP}
fi
#tar cfvz ${BACKUP} ${LISTS}
tar -jPcf ${BACKUP} ${LISTS}

--------------------------------------------------------------------------------------------------

2. Check space and send mail every day
-------------------------checkspace.sh----------------------------------------------------------
df -H >checkspace.txt
echo "Check disk space complete" | mutt -a "checkspace.txt" -s "Check disk space complete" -- email@domain.com
---------------------------------------------------------------------------------------------------

3. delete old file backp
------------------------script removeoldfilebackup.sh--------------------------------------------
find /media/Backup/ -type f -mtime +7 -name '*.bz2' -execdir rm -- {} +
ls -lt /media/Backup > listbackup.txt
echo "Delete old file backup complete" | mutt -a "listbackup.txt" -s "Delete old file backup complete" -- email@domain.com
-----------------------------------------------------------------------------------------------

* find /location/* -type f -mtime +7 -exec rm -rf {} \;
* find /location/findname.log -type f -mtime +7 -exec rm -rf {} \;

4.crontab -l
0 */3 * * * /sbin/service dovecot reload
0 0 * * * /root/./backup.sh
0 4 * * * /root/./checkspace.sh
0 5 * * * /root/./removeoldfilebackup.sh

Tuesday, November 18, 2014

script add new user on centos


---------------------------adduser.sh----------------------------------------------------
#!/bin/bash
# Script to add a user to Linux system

if [ $(id -u) -eq 0 ]; then
        read -p "Enter realname : " realname
#       read -p "Enter company : " company
        read -p "Enter username : " username
        read -s -p "Enter password : " password
        egrep "^$username" /etc/passwd >/dev/null
        if [ $? -eq 0 ]; then
                echo "$username exists!"
                exit 1
        else
                sender=email@mydomain.com
                pass=$(perl -e 'print crypt($ARGV[0], "password")' $password)
                useradd -p $pass $username -c "$realname" -m -g group -s /sbin/nologin
                [ $? -eq 0 ] && echo "User has been added to system!" && echo | mail -s "Welcome new email, Do not reply mail" < user.txt mooy@mydomain.com -c image@mydomain.com -- -f $sender  || echo "Failed to add a user!"
        fi
else
        echo "Only root may add a user to the system"
        exit 2
fi

-------------------------------------------------------------------------------

----------------------------------user.txt------------------------------------
Hi
You email address: mooy@mydomain.com
password: m12345


By.
Auto Generate mail.
-----------------------------------------------------------------------------