Category Archives: Scripting

Rsync Backup Script for Synology.

It is necessary to make the following adjustments to Synology (Personal Cloud) before backing up any data. If not, the script will fail.

First of all, install the “sshpass” package onto the client machine (On Local or Laptop PC). It provides the password directly to the script without having to enter it manually (create a text file with password).

[root@localhost ~]# dnf install sshpass -y

Enable the Rsync service to render your DiskStation as a backup destination for another Synology server or Rsync-compatible service via the following services. Be sure to enable Rsync and the Rsync account.

(control Panel → File services → rsync-→ enable rsync service and enable rynsc)

NB:- If needed, we can change port 22 to different port for secure synchronization by assigning a different port value. (If the port is 31947,the following command is required for synchronization)

sshpass -f /root/file1 rsync -av -e ‘ssh -p 31947’ –update –timeout=60 /home/admin/Documents/ admin@192.168.1.105:/volume1/admin/Laptop/Documents/ 2> /dev/null # (ssh port 31947)

Further Edit rsync Account and enter admin username and password.

After that, access “Terminal & SNMP” option and enable the ssh service. If needed, you can change port 22 to a different value.

Further Control Panel – > Terminal & SNMP -→Enable ssh services

Run this script as a cron job to automatically backup the necessary files and folders, as shown below.

  • Cron job

[root@localhost ~]# vi /etc/crontab

HELL=/bin/bash

PATH=/sbin:/bin:/usr/sbin:/usr/bin

MAILTO=root

# For details see man 4 crontabs

# Example of job definition:

# .—————- minute (0 – 59)

# | .————- hour (0 – 23)

# | | .———- day of month (1 – 31)

# | | | .——- month (1 – 12) OR jan,feb,mar,apr …

# | | | | .—- day of week (0 – 6) (Sunday=0 or 7) OR sun,mon,tue,wed,thu,fri,sat

# | | | | |

# * * * * * user-name command to be executed

00 23 * * * root /root/mybackup.sh >> /var/log/backup.log

  • Script

[root@localhost ~]# vi mybackup.sh

#!/bin/bash

#Backup Sources

ORIGIN_PATH1=/home/admin/Documents/

ORIGIN_PATH2=/home/admin/Downloads/

#Backup destination listed below

BACKUP_PATH1=/volume1/NetBackup/

BACKUP_PATH2=/volume1/NetBackup/

#File path for password file

PASS_PATH=/root/mypassword.txt

#This is used to detect the IP address automatically.

IP=`arp -an | grep 00:11:32:9d:a0:bc | awk ‘{print $2}’ | sed ‘s/[()]//g’`

#Backup to the right destination

sshpass -f $PASS_PATH rsync -av -e ‘ssh -p 22’ –update –timeout=150 $ORIGIN_PATH1 admin@$IP:$BACKUP_PATH1 2> /dev/null

sleep 15

sshpass -f $PASS_PATH rsync -av -e ‘ssh -p 22’ –update –timeout=60 $ORIGIN_PATH2 admin@$IP:$BACKUP_PATH2 2> /dev/null

Script To Run ClamAV Anti-virus Automatically on Linux.

This isn’t a virus scanning software, but you can use this script for scanning viruses with ClamAV, an open source antivirus available on multiple platforms. After defining the path variable, the script automatically generates a report corresponding to your destination path. Not only that, but it can also be used as a cron job for scanning the system against viruses routinely. There are no background processes when running with this script for updating the virus signatures, so it does not unnecessarily consume resources. However, it updates the virus signatures with ClamAV before scanning.

Step 1

#Define the virus backup path(s)

BKP_PATH=/tmp/clamav

#Define the process report destination

REP_DEST=/var/log/clamav

#Define the scan target path(s)

SCAN_PATH=/home/john /var/pub/ftp

  • It is not necessary to define the backup path destination(BKP_PATH) if you have selected option three. When it identifies the virus, it will copy or move it into the defined path destination. (Eg:-/tmp/clamav)
  • The scan’s output is transferred to the report destination(REP_DEST). It explains the scanning result and updates information. If you are planning to define this path as a special path make sure that (assuming you are not root) you have necessary permission do so. (Eg:-/var/og/clamav)
  • Scan path (SCAN_PATH), does not mean that it scans the whole folder path that you defined recursively, but it scans the last folder in that defined path. In my example, the John folder and FTP are scanned recursively but not the home or pub. However, if you need to scan the whole machine, define the path as root (/). You can define the number of the paths with space.

Step 2

  • Option 1

The safest option as it gives us an indication of a virus and extracts it into the defined path. But, it does not remove the virus from its original location. Later, we can decide what actions are needed to mitigate the issue.

clamscan –recursive –infected –copy=$BKP_PATH –exclude-dir=$BKP_PATH $SCAN_PATH >>$REP_DEST/scan_$today1.log

  • Option 2

This is a relatively safe option, even though there can be issues if the virus affects the boot or any other system files. It moves all the infected into the backup destination, but there may be issues on the next reboot.

#clamscan –recursive –infected –move=$BKP_PATH –exclude-dir=$BKP_PATH $SCAN_PATH >>$REP_DEST/scan_$today1.log

  • Option 3

It is very unsafe if it identifies the infected files on boot or other systems files, which clears them all without inquiring. Think twice before selecting this option.

#clamscan –recursive –infected –remove –exclude-dir=$BKP_PATH $SCAN_PATH >>$REP_DEST/scan_$today1.log

Step 3

I have selected option 3 and I got the following report for my scan. The first part of the report indicates that the database is up to date. The second section of the report which shows that the decision that has made against found viruses. Further, it gives the summary that includes in scan base against how many viruses, number of files, directories and the files infected. Finally, the script gives us an indication of the termination of the scanning process at the end.

Step 4

We can deploy this script regularly automatically with a cron job. I deployed this script as root, but you can do it as any other user, as long as you have the correct permissions.

Step 5

You can see the script layout below.

Download this script from Github: https://github.com/ade9alwis/clamav/blob/master/myscript.sh

You can download it and amend as necessary.

Reference

  • clamscan_selinux(8) – Linux man page

https://linux.die.net/man/8/clamscan_selinux