Linux Cron Backup Script – run once every month via cron

  Linux Cron Backup Script
==========

I set cron job with crontab -e for running 20:20 8th everyMonth any weekDay

Script appears below as backup-cron.sh,… feel free to COPY to your Linux VM for experimentation…

[root@server1 backup-cron]# crontab -l

20 20 8 * * /root/backup-cron.sh

[root@server1 backup-cron]# cd
[root@server1 ~]# cat backup-cron.sh
#!/usr/bin/bash

# script: /root/backup-cron.sh
# Author LEONIDAS SAVVIDES - LSEPOLIS123@GMAIL.COM
# script for backup auto with cron the Directory /etc/ to /root/backup-cron/etc-YYYY-MM-DD.tar.bz2 type
# of Archives once a month the 8th of each month 20:00

# Get Current date in format YYYY-MM-DD

DATE=`date +%Y-%m-%d`;

# directory to backup
BACKUP_THIS_DIR="/etc";

# save backups in the name /root/backup-cron/etc-DATE.tar.bz2

BACKUP_TO="/root/backup-cron/";

EXT=".tar.bz2";

BACKUP_FILE="etc-"$DATE$EXT; # not spaced

FULL_BACKUP_URI=$BACKUP_TO$BACKUP_FILE;  # not spaced

# Command to execute

tar jcvf $FULL_BACKUP_URI $BACKUP_THIS_DIR;  # all spaced with one space

# check if success or failure backup done
if [ $? -eq 0 ]; then
echo "Success"; mail -s "Success Backup on "$DATE root@localhost; exit 0;
else
echo "Failure"; mail -s "Failed Backup on "$DATE root@localhost; exit 1;
fi

# END OF THE SCRIPT

[root@server1 ~]#

any Question email me here : 

LSEPOLIS123@GMAIL.COM 



Backup, archive, compress Linux files tricks

Backup, archive, compress Linux files tricks

==================

Some of Linux Backup Utilities are

cpio tar gzip bzip2 xz dd rsync dump restore mt

Also exist some Backup Programs for Linux like Amanda and Bacula

Some commands you may use with the Backup Utilities above are [/dev/st0 tape exist the backup or other storage medium]

cpio
ls | cpio --create -o /dev/st0  # create an archive

cpio -i *.c -I /dev/st0   
# extract *.c files from archive if no RE all extracted

cpio -t -I /dev/st0  # list archive contents

tar
tar --create --file /dev/st0 /root  
tar -cvf /dev/st0 /root
tar cvf file.tar dir1  # "-" is optional for tar command
# create an archive

tar --extact --same-permissions --verbose --file /dev/st0
tar -xpvf /dev/st0
tar xpvf /dev/st0
# extract from archive

tar xvf /dev/st0 *.c  # restore only *.c files

tar --list --file /dev/st0  # list contents of tar backup
tar -tf /dev/st0

gzip, bzip, xz

tar zcvf source.tar.gz source  # compress as gz in combination with tar 

tar jcvf source.tar.bz2 source  # compress as bzip in combination with tar 

tar Jcvf source.tar.xz source  # compress as xz in combination with tar 

these commands are more efficient than the two together commands:
tar cvf source.tar source ; gzip -v source.tar  # because this has intermediate step but above archiving and compression happen same time

extraction
tar xzvf source.tar.gz
tar xjvf source.tar.bz2
tar xJvf source.tar.xz

modern version of tar detect compression used auto and Not needed z in extraction
tar xvf source.tar.gz

rsync  // Synchronize the two file system locations - or between the network machines(folders of)
rsync file.tar someone@backup.mydomain:/usr/local
rsync -r a-machine:/usr/local b-machive:/usr/
rsync -r --dry-run /usr/local /BACKUP/usr

find Linux command tricks asked in exams and how to write

find Linux command tricks asked in exams and how to write

======================

A

The find Operators (!, -o, and -a)
There are three operators that are commonly used with find. The ! operator is used
before an option to negate its meaning. So,

find . ! -name "*.c" -print

selects all but the C program files.
B
To look for both shell and perl scripts, use the -o operator, which represents an OR condition. 
We need to use an escaped pair of parentheses here:
find /home \( -name "*.sh" -o -name "*.pl" \) -print

The ( and ) are special characters that are interpreted by the shell to run commands in
a group. The same characters are used by find to group expressions using the
-o and -a operators, the reason why they need to be escaped.
C
Also to find all *.sh (shell scripts) files for user tux execute:
find /home -user "tux" -name "*.sh" -print 
find /home -user "tux" -name "*.sh" -exec ls {} >| file.txt \; # write listing in file.txt 

these two commands  are equivalent to these two >>>

find /home \( -user "tux" -a -name "*.sh" \) -print
find /home \( -user "tux" -a -name "*.sh" \) -exec ls {} >| file.txt \; # write listing in file.txt 

I conclude in some commands use find for finding:

Directories with sticky bit set:
find / -type d -perm -1000 -exec ls -ld {} \;

Files with SGID set:
find / -type f -perm -2000 -exec ls -l {} \;

Files with SUID set:
find / -type f -perm -4000 -exec ls -l {} \;

Files with SUID and SGID set:
find / -type f \( -perm -4000 -a -perm -2000 \) -exec ls -l {} \;

Files with SUID or SGID set:
find / -type f \( -perm -4000 -o -perm -2000 \) -exec ls -l {} \;








































Note:
ALL THESE COMMANDS - IF NOT WORK - MAKE THE APPROPRIATE SPACING eg  \( -user "tux" like exactly I write

Further Reading Resources

https://likegeeks.com/linux-command-line-tricks/