-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbackup_files_sendmail.sh
More file actions
96 lines (69 loc) · 2.76 KB
/
backup_files_sendmail.sh
File metadata and controls
96 lines (69 loc) · 2.76 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
#!/bin/bash
# Author: Ibrahim Ucar
# Written in: Debian 9.x
# Date: 19.10.2018
# Mail: ucribrahim@gmail.com
# -------ABOUT THIS SCRIPT------- #
# This script backup files of a directory and then send the (backup.tar.gz) file to the user with email.
# This script just an example, you can configure as you wish.
# You can set this script to CRON to make this script works every minute, hour, day, etc.
# **** Requirements! ****
# Install "Ssmtp" and configure it.
# Install "Mpack" utility.
# Variables of this shell script.
hostname=$(hostname)
date=$(date '+%Y-%m-%d %H:%M:%S')
# Write the path where your files include for backup.
backup_files="/home/my_logs"
# Write the path where backed up files will be stored.
check_dest="/home/backups"
dest="/home/backups/backups.tar.gz"
# Write e-mail address of the user.
email="username@domain.com"
# Create the directory which we will use this directory as a backup place. If it's not exist, create.
if [ ! -d $check_dest ]
then
mkdir $check_dest
echo "$check_dest has been created."
echo
fi
# Rest of the shell script commands.
if [ -d $backup_files ]; then
echo "$backup_files directory exists, looking for other neccesary directory..."
if [ -d $check_dest ]; then
echo "------------"
echo "$check_dest directory exists, that means it's ready to backup files with (tar) command."
else
echo "$check_dest directory doesn't exists, that means it's NOT ready to backup files with (tar) command."
fi
else
echo "$backup_files directory doesn't exists, that means it's NOT ready to backup files with (tar) command."
fi
echo
echo "Please wait while getting backup files of this directory: $backup_files"
echo
# The commands for backup the files.
cd $backup_files
PWD=$(pwd)
if [[ $PWD/ = $backup_files/ ]]; then
tar cvzf $dest *
echo
echo "Tar process has been finished. Now I will send the $dest file to the user with mail."
else
echo "You're not in right directory which is $backup_files"
fi
# The commands for send "backup.tar.gz" file to user with e-mail.
if [ -f $dest ]; then
echo "This is a message from $hostname server on $date that $backup_files has been backed up and send you the $dest fi$
mpack -s "Backup Message From Server" -d /tmp/message_body.txt $dest $email 2> /tmp/mail_err.txt
if [ -s "/tmp/mail_err.txt" ]; then
echo "Mail could not send!"
echo "You can read the error from ( /var/log/mail.err ) file."
else
echo "Mail sent successfully."
echo "You can see details of commands outputs in ( /var/log/syslog ) file."
fi
else
echo "The $dest file DOES NOT exists! Please review the shell script or check out at the beginning directories."
fi
rm -rf /tmp/mail_err.txt