backup script for linux server using rsync deamon
For backup, we have daily and weekly for weekly we mention --delete
even for database both daily and weekly with ---delete will run
the server whose backup needs to be set up there will be a script to create database dump backup daily
on backup_parent_dir="/usr/src/bacula/ then we rsync this folder to our backup server
get root password for mysql DA
cat /usr/local/directadmin/conf/mysql.conf
[root@sm254 ~]# crontab -l
13 01 * * * /bin/sh /home/sas/dbbackup_scripts.sh &> /home/sas/dbbackup_scripts.log
[root@sm254 ~]# cat /home/sas/dbbackup_scripts.sh
#!/bin/bash
# Simple script to backup MySQL databases
# Parent backup directory
backup_parent_dir="/usr/src/bacula/"
# MySQL settings
mysql_user="root"
mysql_password="a0ZcKSRDl6"
# Read MySQL password from stdin if empty
if [ -z "${mysql_password}" ]; then
echo -n "Enter MySQL ${mysql_user} password: "
read -s mysql_password
echo
fi
# Check MySQL password
echo exit | mysql --user=${mysql_user} --password=${mysql_password} -B 2>/dev/null
if [ "$?" -gt 0 ]; then
echo "MySQL ${mysql_user} password incorrect"
exit 1
else
echo "MySQL ${mysql_user} password correct."
fi
# Create backup directory and set permissions
backup_date=`date +%Y_%m_%d_%H_%M`
backup_dir="${backup_parent_dir}/${backup_date}"
echo "Backup directory: ${backup_dir}"
mkdir -p "${backup_dir}"
chmod 700 "${backup_dir}"
# Get MySQL databases
mysql_databases=`echo 'show databases' | mysql --user=${mysql_user} --password=${mysql_password} -B | sed /^Database$/d`
# Backup and compress each database
for database in $mysql_databases
do
if [ "${database}" == "information_schema" ] || [ "${database}" == "performance_schema" ]; then
additional_mysqldump_params="--skip-lock-tables"
else
additional_mysqldump_params=""
fi
echo "Creating backup of \"${database}\" database"
mysqldump ${additional_mysqldump_params} --user=${mysql_user} --password=${mysql_password} ${database} | gzip > "${backup_dir}/${database}.gz"
chmod 600 "${backup_dir}/${database}.gz"
done
### modifyied on 5 Mar 2019
echo "Backing up all mysql databases in single file"
mysqldump -u da_admin -pMyUS\@2k19\#F --all-databases > "${backup_dir}/all_databases.sql"
above script will create DB backup on /usr/src/bacula/ we will rsync this folder to the server where we will store backup
now on the server where we will have backup stored we set rsync there as downloading pull will be faster
Backup storing server
################New namaksha###########################
21 22 * * 1,2,3,4,5,6 /bin/sh /backup_script/INDIA/192.227.117.47/Namak_backup_daily.sh
30 21 * * 7 /bin/sh /weeklybackup/INDIA/192.227.117.47/namak_backup_weekly.sh
[root@vps 192.227.91.254]# cat /backup_script/INDIA/192.227.117.47/Namak_backup_daily.sh
#!/bin/bash
start=`date`
/bin/sh /backup_script/INDIA/192.227.117.47/etc.sh
/bin/sh /backup_script/INDIA/192.227.117.47/usr.sh
/bin/sh /backup_script/INDIA/192.227.117.47/var.sh
/bin/sh /backup_script/INDIA/192.227.117.47/home.sh
/bin/sh /backup_script/INDIA/192.227.117.47/dbbackup.sh
end=`date`
echo "Start time :${start}
End time :${end}" > /backup_script/INDIA/192.227.117.47/Namak_backup_daily.log
[root@vps 192.227.91.254]# cat/backup_script/INDIA/192.227.117.47/etc.sh
-bash: cat/backup_script/INDIA/192.227.117.47/etc.sh: No such file or directory
[root@vps 192.227.91.254]# cat /backup_script/INDIA/192.227.117.47/etc.sh
#!/bin/sh
Date1=`date +%d-%m-%Y`
T1=`date +%H-%M`
rsync -avz --password-file=/backup_script/pass -u root@192.227.117.47::etc/ /backup3/INDIA/192.227.117.47/DAILY/etc/ > /backup_script/INDIA/192.227.117.47/logs/etc.log 2>&1
T2=`date +%H-%M`
echo "START : " $T1 >> /backup_script/INDIA/192.227.117.47/logs/etc.log
echo "END : " $T2 >> /backup_script/INDIA/192.227.117.47/logs/etc.log
[root@vps 192.227.91.254]# cat /backup_script/INDIA/192.227.117.47/usr.sh
#!/bin/sh
Date1=`date +%d-%m-%Y`
T1=`date +%H-%M`
rsync -avz --password-file=/backup_script/pass -u root@192.227.117.47::usr/ /backup3/INDIA/192.227.117.47/DAILY/usr/ > /backup_script/INDIA/192.227.117.47/logs/usr.log 2>&1
T2=`date +%H-%M`
echo "START : " $T1 >> /backup_script/INDIA/192.227.117.47/logs/usr.log
echo "END : " $T2 >> /backup_script/INDIA/192.227.117.47/logs/usr.log
[root@vps 192.227.91.254]# cat /backup_script/INDIA/192.227.117.47/var.sh
#!/bin/sh
Date1=`date +%d-%m-%Y`
T1=`date +%H-%M`
rsync -avz --password-file=/backup_script/pass -u root@192.227.117.47::var/ /backup3/INDIA/192.227.117.47/DAILY/var/ > /backup_script/INDIA/192.227.117.47/logs/var.log 2>&1
T2=`date +%H-%M`
echo "START : " $T1 >> /backup_script/INDIA/192.227.117.47/logs/var.log
echo "END : " $T2 >> /backup_script/INDIA/192.227.117.47/logs/var.log
[root@vps 192.227.91.254]# cat /backup_script/INDIA/192.227.117.47/home.sh
#!/bin/sh
Date1=`date +%d-%m-%Y`
T1=`date +%H-%M`
rsync -avz --password-file=/backup_script/pass -u root@192.227.117.47::home/ /backup3/INDIA/192.227.117.47/DAILY/home/ > /backup_script/INDIA/192.227.117.47/logs/home.log 2>&1
T2=`date +%H-%M`
echo "START : " $T1 >> /backup_script/INDIA/192.227.117.47/logs/home.log
echo "END : " $T2 >> /backup_script/INDIA/192.227.117.47/logs/home.log
[root@vps 192.227.91.254]# cat /backup_script/INDIA/192.227.117.47/dbbackup.sh
#!/bin/sh
Date1=`date +%d-%m-%Y`
T1=`date +%H-%M`
rsync -avz --password-file=/backup_script/pass -u root@192.227.117.47::mnt/sdd1/bacula/ /backup3/INDIA/192.227.117.47/DAILY/usr/dbbackup/ > /backup_script/INDIA/192.227.117.47/logs/dbbkp.log 2>&1
T2=`date +%H-%M`
echo "START : " $T1 >> /backup_script/INDIA/192.227.117.47/logs/dbbkp.log
echo "END : " $T2 >> /backup_script/INDIA/192.227.117.47/logs/dbbkp.log
now weekly
[root@vps 192.227.91.254]# cat /weeklybackup/INDIA/192.227.117.47/namak_backup_weekly.sh
#!/bin/bash
start=`date`
/bin/sh /weeklybackup/INDIA/192.227.117.47/etc.sh
/bin/sh /weeklybackup/INDIA/192.227.117.47/usr.sh
/bin/sh /weeklybackup/INDIA/192.227.117.47/var.sh
/bin/sh /weeklybackup/INDIA/192.227.117.47/home.sh
/bin/sh /weeklybackup/INDIA/192.227.117.47/dbbackup.sh
end=`date`
echo "Start time :${start}
End time :${end}" > /weeklybackup/INDIA/192.227.117.47/namak_backup_weekly.log
[root@vps 192.227.91.254]# cat /weeklybackup/INDIA/192.227.117.47/etc.sh
#!/bin/sh
Date1=`date +%d-%m-%Y`
T1=`date +%H-%M`
rsync -avz --password-file=/backup_script/pass --delete root@192.227.117.47::etc/ /backup3/INDIA/192.227.117.47/DAILY/etc/ > /weeklybackup/INDIA/192.227.117.47/logs/etc.log 2>&1
T2=`date +%H-%M`
echo "START : " $T1 >> /weeklybackup/INDIA/192.227.117.47/logs/etc.log
echo "END : " $T2 >> /weeklybackup/INDIA/192.227.117.47/logs/etc.log
[root@vps 192.227.91.254]# cat /weeklybackup/INDIA/192.227.117.47/usr.sh
#!/bin/sh
Date1=`date +%d-%m-%Y`
T1=`date +%H-%M`
rsync -avz --password-file=/backup_script/pass --delete root@192.227.117.47::usr/ /backup3/INDIA/192.227.117.47/DAILY/usr/ > /weeklybackup/INDIA/192.227.117.47/logs/usr.log 2>&1
T2=`date +%H-%M`
echo "START : " $T1 >> /weeklybackup/INDIA/192.227.117.47/logs/usr.log
echo "END : " $T2 >> /weeklybackup/INDIA/192.227.117.47/logs/usr.log
[root@vps 192.227.91.254]# cat /weeklybackup/INDIA/192.227.117.47/var.sh
#!/bin/sh
Date1=`date +%d-%m-%Y`
T1=`date +%H-%M`
rsync -avz --password-file=/backup_script/pass --delete root@192.227.117.47::var/ /backup3/INDIA/192.227.117.47/DAILY/var/ > /weeklybackup/INDIA/192.227.117.47/logs/var.log 2>&1
T2=`date +%H-%M`
echo "START : " $T1 >> /weeklybackup/INDIA/192.227.117.47/logs/var.log
echo "END : " $T2 >> /weeklybackup/INDIA/192.227.117.47/logs/var.log
[root@vps 192.227.91.254]# cat /weeklybackup/INDIA/192.227.117.47/home.sh
#!/bin/sh
Date1=`date +%d-%m-%Y`
T1=`date +%H-%M`
rsync -avz --password-file=/backup_script/pass --delete root@192.227.117.47::home/ /backup3/INDIA/192.227.117.47/DAILY/home/ > /weeklybackup/INDIA/192.227.117.47/logs/home.log 2>&1
T2=`date +%H-%M`
echo "START : " $T1 >> /weeklybackup/INDIA/192.227.117.47/logs/home.log
echo "END : " $T2 >> /weeklybackup/INDIA/192.227.117.47/logs/home.log
[root@vps 192.227.91.254]# cat /weeklybackup/INDIA/192.227.117.47/dbbackup.sh
#!/bin/sh
Date1=`date +%d-%m-%Y`
T1=`date +%H-%M`
rsync -avz --password-file=/backup_script/pass --delete -u root@192.227.117.47::mnt/sdd1/bacula/ /backup3/INDIA/192.227.117.47/DAILY/usr/dbbackup/ > /weeklybackup/INDIA/192.227.117.47/logs/dbbkp.log 2>&1
T2=`date +%H-%M`
echo "START : " $T1 >> /weeklybackup/INDIA/192.227.117.47/logs/dbbkp.log
echo "END : " $T2 >> /weeklybackup/INDIA/192.227.117.47/logs/dbbkp.log
ERROR: The remote path must start with a module name not a /
remove / after ::
------------------------------------------------------------
Rsync daemon setup on server whose backup is required
[root@usa dbbackup]# cat /etc/rsyncd.conf
transfer logging = true
log format = %h %o %f %l %b
log file = /var/log/rsyncd.log
motd file = /etc/motd
hosts allow = 115.112.176.41 192.168.2.41 178.33.224.175 192.151.147.82
[etc]
path = /etc
comment = An Example
auth users = root
secrets file = /etc/rsyncd.secrets
read only = no
uid = root
gid = root
use chroot = yes
[home]
path = /home/
comment = An Example
auth users = root
secrets file = /etc/rsyncd.secrets
read only = no
uid = root
gid = root
use chroot = yes
[usr]
path = /usr/
comment = An Example
auth users = root
secrets file = /etc/rsyncd.secrets
read only = no
uid = root
gid = root
use chroot = yes
[var]
path = /var/
comment = An Example
auth users = root
secrets file = /etc/rsyncd.secrets
read only = no
uid = root
gid = root
use chroot = yes
[dbbackup]
path =/mnt/sdd1/bacula/
comment = database backup
auth users = root
secrets file = /etc/rsyncd.secrets
read only = no
uid = root
gid = root
use chroot = yes
[root@usa dbbackup]# cat /etc/motd
cat /etc/motd is blank
[root@usa dbbackup]# cat /etc/rsyncd.secrets
root:rsyc@JBOSS
chmod 600 /etc/rsyncd.secrets
server where backup is kept
[root@vps 192.227.117.47]# pwd
/backup_script/INDIA/192.227.117.47
[root@vps 192.227.117.47]# ls
dbbackup.sh etc.sh home.sh logs Namak_backup_daily.log Namak_backup_daily.sh usr.sh var.sh
[root@vps 192.227.67.74]# cat /backup_script/for_144
rsyc@JBOSS
############## Mail Alerts ##################
grep "rsync error" /backup_script/INDIA/192.227.75.135/logs/etc.log
i=`echo $?`
if [ $i -eq 0 ]
then
echo "
Dear All,
Please note that backup of the script "135_da/etc.sh" is not proper.
Thanx " > /tmp/40mail
cat /tmp/40mail | mail -s "JBOSS Improper-Backup alert" backup@privatebrandhost.com
else
echo "
Dear All,
Please note that backup of the script "135_da/etc.sh" is proper.
Thanx " > /tmp/40mail
cat /tmp/40mail | mail -s "JBOSS Proper-Backup alert" backup@privatebrandhost.com
fi
------------------------------------------------------------------
[root@ws242 /]# cat /weeklybackup/INDIA/192.227.112.109/109_backup_weekly.sh
#!/bin/bash
start=`date`
/bin/sh /weeklybackup/INDIA/192.227.112.109/etc.sh
/bin/sh /weeklybackup/INDIA/192.227.112.109/usr.sh
/bin/sh /weeklybackup/INDIA/192.227.112.109/var.sh
/bin/sh /weeklybackup/INDIA/192.227.112.109/home.sh
end=`date`
echo "Start time :${start}
End time :${end}" > /weeklybackup/INDIA/192.227.112.109/109_backup_weekly.log
[root@ws242 /]# cat /weeklybackup/INDIA/192.227.112.109/etc.sh
#!/bin/sh
Date1=`date +%d-%m-%Y`
T1=`date +%H-%M`
rsync -avz --password-file=/backup_script/pass -u root@192.227.112.109::etc/ /Backup/INDIA/192.227.112.109/DAILY/ > /weeklybackup/INDIA/192.227.112.109/logs/etc.log 2>&1
T2=`date +%H-%M`
echo "START : " $T1 >> /backup_script/INDIA/192.227.112.109/logs/etc.log
echo "END : " $T2 >> /backup_script/INDIA/192.227.112.109/logs/etc.log
############## Mail Alerts ##################
grep "rsync error" /backup_script/INDIA/192.227.112.109/logs/etc.log
i=`echo $?`
if [ $i -eq 0 ]
then
echo "
Dear All,
Please note that backup of the script "png-109/etc.sh" is not proper.
Thanx " > /tmp/arkinc
cat /tmp/arkinc | mail -s "JBOSS Improper-Backup alert" linux@drushti.in
else
echo "
Dear All,
Please note that backup of the script "png-109/etc.sh" is proper.
Thanx " > /tmp/arkinc
cat /tmp/arkinc | mail -s "JBOSS Proper-Backup alert" linux@drushti.in
fi
[root@ws242 /]#
Note that the files on the destination directory storing the backup data will be kept if the files do not exist in the source directory to be backup’ed. If you would like
rsync
to delete the files/directories in the destination directory if the files/directories does not exist in the source directory, you can add --delete
to rsync
:$ rsync -avxP --delete \
/path/to/directory/to/backup \
/path/to/directory/for/storing/backup
For database
###########################
13 01 * * * /bin/sh /home/sas/dbbackup_scripts.sh &> /home/sas/dbbackup_scripts.log
cat /home/sas/dbbackup_scripts.sh
#!/bin/bash
# Simple script to backup MySQL databases
# Parent backup directory
backup_parent_dir="/mnt/sdd1/bacula/"
# MySQL settings
mysql_user="da_admin"
mysql_password="NcXtcNp9aT"
# Read MySQL password from stdin if empty
if [ -z "${mysql_password}" ]; then
echo -n "Enter MySQL ${mysql_user} password: "
read -s mysql_password
echo
fi
# Check MySQL password
echo exit | mysql --user=${mysql_user} --password=${mysql_password} -B 2>/dev/null
if [ "$?" -gt 0 ]; then
echo "MySQL ${mysql_user} password incorrect"
exit 1
else
echo "MySQL ${mysql_user} password correct."
fi
# Create backup directory and set permissions
backup_date=`date +%Y_%m_%d_%H_%M`
backup_dir="${backup_parent_dir}/${backup_date}"
echo "Backup directory: ${backup_dir}"
mkdir -p "${backup_dir}"
chmod 700 "${backup_dir}"
# Get MySQL databases
mysql_databases=`echo 'show databases' | mysql --user=${mysql_user} --password=${mysql_password} -B | sed /^Database$/d`
# Backup and compress each database
for database in $mysql_databases
do
if [ "${database}" == "information_schema" ] || [ "${database}" == "performance_schema" ]; then
additional_mysqldump_params="--skip-lock-tables"
else
additional_mysqldump_params=""
fi
echo "Creating backup of \"${database}\" database"
mysqldump ${additional_mysqldump_params} --user=${mysql_user} --password=${mysql_password} ${database} | gzip > "${backup_dir}/${database}.gz"
chmod 600 "${backup_dir}/${database}.gz"
done
### modifyied on 5 Mar 2019
echo "Backing up all mysql databases in single file"
mysqldump -u da_admin -pMyUS\@2k19\#F --all-databases > "${backup_dir}/all_databases.sql"
this will create backup here /mnt/sdd1/bacula/
#!/bin/bash
start=`date`
/bin/sh /backup_script/INDIA/192.227.91.254/etc.sh
/bin/sh /backup_script/INDIA/192.227.91.254/usr.sh
/bin/sh /backup_script/INDIA/192.227.91.254/var.sh
/bin/sh /backup_script/INDIA/192.227.91.254/home.sh
/bin/sh /backup_script/INDIA/192.227.91.254/backup3.sh
end=`date`
echo "Start time :${start}
End time :${end}" > /backup_script/INDIA/192.227.91.254/logic_backup_daily.log
cat backup3.sh
#!/bin/sh
Date1=`date +%d-%m-%Y`
T1=`date +%H-%M`
rsync -avz --exclude=/home/backup/*.tar --password-file=/backup_script/for_254_farmerp -u root@192.227.91.254::backup3/bacula /dbbackup/INDIA/192.227.91.254/DAILY/backup3/ > /backup_script/INDIA/192.227.91.254/logs/backup3.log 2>&1
T2=`date +%H-%M`
echo "START : " $T1 >> /backup_script/INDIA/192.227.91.254/logs/backup3.log
echo "END : " $T2 >> /backup_script/INDIA/192.227.91.254/logs/backup3.log
Comments
Post a Comment