powered by simpleCommunicator - 2.0.18     © 2024 Programmizd 02
Map
Форумы / UNIX-подобные OC [закрыт для гостей] / Linux: простые скрипты резервного копирования
4 сообщений из 4, страница 1 из 1
Linux: простые скрипты резервного копирования
    #470897
basename
Скрыть профиль Поместить в игнор-лист Сообщения автора в теме
Участник
Например, работают на сервере форума

System FS
Код: BASH
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.
97.
98.
99.
100.
101.
102.
103.
104.
105.
106.
107.
108.
109.
110.
111.
112.
113.
114.
115.
116.
117.
118.
119.
120.
121.
122.
123.
124.
125.
126.
127.
128.
129.
130.
131.
132.
133.
134.
135.
136.
137.
138.
139.
140.
141.
142.
143.
144.
145.
146.
147.
148.
149.
150.
151.
152.
153.
154.
155.
156.
#!/bin/bash
#

sname="dragon.itwrks.org"
archdir="/tmp/backup/$sname/fs"
dstdir="/mnt/yadisk/itwrks/IT-WORKS/BACKUP/$sname/fs"
tdate=`date '+%Y%m%d%H%M%S'`
problem="Failed to make system backup on $sname"
success="System backup on $sname has been completed successfully"
email=root
RETVAL=0

#
# Define TAR
tcmd="tar -cvjf"
tsuffix="tar.bz2"
TAR[0]="root_$tdate.$tsuffix \
    --exclude=/root/.cache \
    --exclude=/root/.cpanm \
    --exclude=/root/.local \
/root"
TAR[1]="etc_$tdate.$tsuffix /etc"
TAR[2]="opt_$tdate.$tsuffix \
    --exclude=/opt/etc \
    --exclude=/opt/itwrks/backup \
/opt"
TAR[3]="var_spool_cron_$tdate.$tsuffix /var/spool/cron"
#

if [ -f $dstdir/flag ] ; then
   rm -f $dstdir/flag
fi

#echo -n "Check $archdir... press Enter:"
#read

if [ ! -d $archdir ] ; then
    mkdir -p $archdir
    chown root:root $archdir
    chmod 750 $archdir
fi

#echo -n "Purge $archdir... press Enter:"
#read

for file in `ls $archdir`
    do
      rm -f $archdir/$file
    done

#echo -n "Start TAR... press Enter:"
#read

cd $archdir

ttar=0
while [ $ttar -lt ${#TAR[*]} ]
    do
      $tcmd ${TAR[$ttar]}
      ttar=$(( $ttar+1 ))
    done

#echo -n "Check how many files were backuped... press Enter:"
#read

if [ "`ls $archdir | grep $tdate.$tsuffix | wc -l`" != "${#TAR[*]}" ]; then
    echo "The number of bak files in $archdir and number of files to backup aren't eq!" | mail -s "$problem" $email
    exit 88
fi

#echo -n "Check mount... press Enter:"
#read

if [ ! -d $dstdir ] ; then
    echo "Failed to check whether directory $dstdir is mounted!" | mail -s "$problem" $email
    exit 88
fi

#echo -n "Copy to destination... press Enter:"
#read

cp $archdir/*_$tdate.$tsuffix $dstdir

#echo -n "Check number of files... press Enter:"
#read

if [ "`ls $archdir | grep $tdate.$tsuffix | wc -l`" != "`ls $dstdir | grep $tdate.$tsuffix | wc -l`" ]; then
    echo "The number of bak files in $archdir and files in $dstdir aren't eq!" | mail -s "$problem" $email
    exit 88
fi

#echo -n "Check md5sum... press Enter:"
#read

checksum() {
    NEW="$1"
    OLD="$archdir/`basename $NEW`"
    #if [ "`cat $OLD | md5sum`" != "`cat $NEW | md5sum`" ]; then
    if [ "`md5sum $OLD | cut -f1 -d ' '`" != "`md5sum $NEW | cut -f1 -d ' '`" ]; then
      echo "Failed to check md5sum of $NEW!" | mail -s "$problem" $email
      exit 88
    fi
}

for file in `ls $dstdir | grep $tdate.$tsuffix`
    do
      checksum $dstdir/$file
    done

RETVAL=$?
[ $RETVAL == 0 ] && touch $dstdir/flag

#echo -n "Start report... press Enter:"
#read

report() {
    count=`ls $archdir | grep $tdate.$tsuffix | wc -l`
    touch $archdir/report
    echo "$success." > $archdir/report
    echo "Archive files were copied to $dstdir." >> $archdir/report
    echo "" >> $archdir/report
    echo "There were made $count archives." >> $archdir/report
    echo "" >> $archdir/report
    echo "size:" >> $archdir/report
    for file in `ls $archdir | grep $tdate.$tsuffix`
    do
      du -sh $archdir/$file >> $archdir/report
    done
    echo "" >> $archdir/report
    echo "md5sum:" >> $archdir/report
    for file in `ls $archdir | grep $tdate.$tsuffix`
    do
      md5sum $archdir/$file >> $archdir/report
    done
    echo "" >> $archdir/report
    echo "`date`"  >> $archdir/report
    cat $archdir/report | mail -s "$success" $email
}

if [ ! -f $dstdir/flag ] ; then
    echo "Failed to check flag file! Problem has been occured during backup!" | mail -s "$problem" $email
    exit 88
fi

report

#echo -n "Remove local bak files... press Enter:"
#read

for file in `ls $archdir --hide=report`
    do
      rm -f $archdir/$file
    done

#echo -n "End... press Enter:"
#read
WWW_directory
Код: BASH
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.
97.
98.
99.
100.
101.
102.
103.
104.
105.
106.
107.
108.
109.
110.
111.
112.
113.
114.
115.
116.
117.
118.
119.
120.
121.
122.
123.
124.
125.
126.
127.
128.
129.
130.
131.
132.
133.
134.
135.
136.
137.
138.
139.
140.
141.
142.
143.
144.
145.
146.
147.
148.
149.
150.
151.
152.
153.
154.
155.
156.
157.
158.
159.
160.
161.
162.
163.
164.
165.
166.
167.
168.
#!/bin/bash
#

sname="dragon.itwrks.org"
archdir="/tmp/backup/$sname/www"
dstdir="/mnt/yadisk/itwrks/IT-WORKS/BACKUP/$sname/www"
tdate=`date '+%Y%m%d%H%M%S'`
problem="Failed to make WWW backup on $sname"
success="WWW backup on $sname has been completed successfully"
email=root
RETVAL=0

#
# Define TAR
tcmd="tar -cvjf"
tsuffix="tar.bz2"
TAR[0]="itwrks.org_$tdate.$tsuffix \
     --exclude=/var/www/html/public/itwrks.org/downloads \
     --exclude=/var/www/html/public/itwrks.org/test_gallery \
/var/www/html/public/itwrks.org"
TAR[1]="nosql.itwrks.org_$tdate.$tsuffix \
    --exclude=/var/www/html/public/nosql.itwrks.org/forum \
    --exclude=/var/www/html/public/nosql.itwrks.org/forum-old \
    --exclude=/var/www/html/public/nosql.itwrks.org/album/pictures \
    --exclude=/var/www/html/public/nosql.itwrks.org/album/thumbs \
    --exclude=/var/www/html/public/nosql.itwrks.org/vstrechy/pictures \
    --exclude=/var/www/html/public/nosql.itwrks.org/vstrechy/thumbs \
/var/www/html/public/nosql.itwrks.org"
TAR[2]="nosql.itwrks.org_forum_$tdate.$tsuffix \
    --exclude=/var/www/html/public/nosql.itwrks.org/forum/log \
    --exclude=/var/www/html/public/nosql.itwrks.org/forum/tmp \
    --exclude=/var/www/html/public/nosql.itwrks.org/forum/user_data/attachments \
    --exclude=/var/www/html/public/nosql.itwrks.org/forum/user_data/avatars \
    --exclude=/var/www/html/public/nosql.itwrks.org/forum/user_data/custom_smileys \
    --exclude=/var/www/html/public/nosql.itwrks.org/forum/user_data/photos \
    --exclude=/var/www/html/public/nosql.itwrks.org/forum/user_data/thumbs \
/var/www/html/public/nosql.itwrks.org/forum"
TAR[3]="anton.koudinov.ru_$tdate.$tsuffix \
/var/www/html/public/anton.koudinov.ru"
#

if [ -f $dstdir/flag ] ; then
   rm -f $dstdir/flag
fi

#echo -n "Check $archdir... press Enter:"
#read

if [ ! -d $archdir ] ; then
    mkdir -p $archdir
    chown root:root $archdir
    chmod 750 $archdir
fi

#echo -n "Purge $archdir... press Enter:"
#read

for file in `ls $archdir`
    do
      rm -f $archdir/$file
    done

#echo -n "Start TAR... press Enter:"
#read

cd $archdir

ttar=0
while [ $ttar -lt ${#TAR[*]} ]
    do
      $tcmd ${TAR[$ttar]}
      ttar=$(( $ttar+1 ))
    done

#echo -n "Check how many files were backuped... press Enter:"
#read

if [ "`ls $archdir | grep $tdate.$tsuffix | wc -l`" != "${#TAR[*]}" ]; then
    echo "The number of bak files in $archdir and number of files to backup aren't eq!" | mail -s "$problem" $email
    exit 88
fi

#echo -n "Check mount... press Enter:"
#read

if [ ! -d $dstdir ] ; then
    echo "Failed to check whether directory $dstdir is mounted!" | mail -s "$problem" $email
    exit 88
fi

#echo -n "Copy to destination... press Enter:"
#read

cp $archdir/*_$tdate.$tsuffix $dstdir

#echo -n "Check number of files... press Enter:"
#read

if [ "`ls $archdir | grep $tdate.$tsuffix | wc -l`" != "`ls $dstdir | grep $tdate.$tsuffix | wc -l`" ]; then
    echo "The number of bak files in $archdir and files in $dstdir aren't eq!" | mail -s "$problem" $email
    exit 88
fi

#echo -n "Check md5sum... press Enter:"
#read

checksum() {
    NEW="$1"
    OLD="$archdir/`basename $NEW`"
    #if [ "`cat $OLD | md5sum`" != "`cat $NEW | md5sum`" ]; then
    if [ "`md5sum $OLD | cut -f1 -d ' '`" != "`md5sum $NEW | cut -f1 -d ' '`" ]; then
      echo "Failed to check md5sum of $NEW!" | mail -s "$problem" $email
      exit 88
    fi
}

for file in `ls $dstdir | grep $tdate.$tsuffix`
    do
      checksum $dstdir/$file
    done

RETVAL=$?
[ $RETVAL == 0 ] && touch $dstdir/flag

#echo -n "Start report... press Enter:"
#read

report() {
    count=`ls $archdir | grep $tdate.$tsuffix | wc -l`
    touch $archdir/report
    echo "$success." > $archdir/report
    echo "Archive files were copied to $dstdir." >> $archdir/report
    echo "" >> $archdir/report
    echo "There were made $count archives." >> $archdir/report
    echo "" >> $archdir/report
    echo "size:" >> $archdir/report
    for file in `ls $archdir | grep $tdate.$tsuffix`
    do
      du -sh $archdir/$file >> $archdir/report
    done
    echo "" >> $archdir/report
    echo "md5sum:" >> $archdir/report
    for file in `ls $archdir | grep $tdate.$tsuffix`
    do
      md5sum $archdir/$file >> $archdir/report
    done
    echo "" >> $archdir/report
    echo "`date`"  >> $archdir/report
    cat $archdir/report | mail -s "$success" $email
}

if [ ! -f $dstdir/flag ] ; then
    echo "Failed to check flag file! Problem has been occured during backup!" | mail -s "$problem" $email
    exit 88
fi

report

#echo -n "Remove local bak files... press Enter:"
#read

for file in `ls $archdir --hide=report`
    do
      rm -f $archdir/$file
    done

#echo -n "End... press Enter:"
#read
SQL dump
Код: BASH
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.
97.
98.
99.
100.
101.
102.
103.
104.
105.
106.
107.
108.
109.
110.
111.
112.
113.
114.
115.
116.
117.
118.
119.
120.
121.
122.
123.
124.
125.
126.
127.
128.
129.
130.
131.
132.
133.
134.
135.
136.
137.
138.
139.
140.
141.
142.
143.
144.
145.
146.
147.
148.
149.
150.
151.
#!/bin/bash
#

sname="dragon.itwrks.org"
archdir="/tmp/backup/$sname/db"
dstdir="/mnt/yadisk/itwrks/IT-WORKS/BACKUP/$sname/db"
tdate=`date '+%Y%m%d%H%M%S'`
problem="Failed to make SQL backup on $sname"
success="SQL backup on $sname has been completed successfully"
email=root
RETVAL=0

# SQL dump
mysqldump -hlocalhost -uroot --routines --triggers --single-transaction --quick --max_allowed_packet=32M --lock-tables=false NOSQL > /opt/itwrks/backup/db/NOSQL_$tdate.sql
#

#
# Define TAR
tcmd="tar -cvjf"
tsuffix="tar.bz2"
TAR[0]="DB_NOSQL_$tdate.$tsuffix /opt/itwrks/backup/db/NOSQL_$tdate.sql"
#

if [ -f $dstdir/flag ] ; then
   rm -f $dstdir/flag
fi

#echo -n "Check $archdir... press Enter:"
#read

if [ ! -d $archdir ] ; then
    mkdir -p $archdir
    chown root:root $archdir
    chmod 750 $archdir
fi

#echo -n "Purge $archdir... press Enter:"
#read

for file in `ls $archdir`
    do
      rm -f $archdir/$file
    done

#echo -n "Start TAR... press Enter:"
#read

cd $archdir

ttar=0
while [ $ttar -lt ${#TAR[*]} ]
    do
      $tcmd ${TAR[$ttar]}
      ttar=$(( $ttar+1 ))
    done

#echo -n "Check how many files were backuped... press Enter:"
#read

if [ "`ls $archdir | grep $tdate.$tsuffix | wc -l`" != "${#TAR[*]}" ]; then
    echo "The number of bak files in $archdir and number of files to backup aren't eq!" | mail -s "$problem" $email
    exit 88
fi

#echo -n "Check mount... press Enter:"
#read

if [ ! -d $dstdir ] ; then
    echo "Failed to check whether directory $dstdir is mounted!" | mail -s "$problem" $email
    exit 88
fi

#echo -n "Copy to destination... press Enter:"
#read

cp $archdir/*_$tdate.$tsuffix $dstdir

#echo -n "Check number of files... press Enter:"
#read

if [ "`ls $archdir | grep $tdate.$tsuffix | wc -l`" != "`ls $dstdir | grep $tdate.$tsuffix | wc -l`" ]; then
    echo "The number of bak files in $archdir and files in $dstdir aren't eq!" | mail -s "$problem" $email
    exit 88
fi

#echo -n "Check md5sum... press Enter:"
#read

checksum() {
    NEW="$1"
    OLD="$archdir/`basename $NEW`"
    #if [ "`cat $OLD | md5sum`" != "`cat $NEW | md5sum`" ]; then
    if [ "`md5sum $OLD | cut -f1 -d ' '`" != "`md5sum $NEW | cut -f1 -d ' '`" ]; then
      echo "Failed to check md5sum of $NEW!" | mail -s "$problem" $email
      exit 88
    fi
}

for file in `ls $dstdir | grep $tdate.$tsuffix`
    do
      checksum $dstdir/$file
    done

RETVAL=$?
[ $RETVAL == 0 ] && touch $dstdir/flag

#echo -n "Start report... press Enter:"
#read

report() {
    count=`ls $archdir | grep $tdate.$tsuffix | wc -l`
    touch $archdir/report
    echo "$success." > $archdir/report
    echo "Archive files were copied to $dstdir." >> $archdir/report
    echo "" >> $archdir/report
    echo "There were made $count archives." >> $archdir/report
    echo "" >> $archdir/report
    echo "size:" >> $archdir/report
    for file in `ls $archdir | grep $tdate.$tsuffix`
    do
      du -sh $archdir/$file >> $archdir/report
    done
    echo "" >> $archdir/report
    echo "md5sum:" >> $archdir/report
    for file in `ls $archdir | grep $tdate.$tsuffix`
    do
      md5sum $archdir/$file >> $archdir/report
    done
    echo "" >> $archdir/report
    echo "`date`"  >> $archdir/report
    cat $archdir/report | mail -s "$success" $email
}

if [ ! -f $dstdir/flag ] ; then
    echo "Failed to check flag file! Problem has been occured during backup!" | mail -s "$problem" $email
    exit 88
fi

report

#echo -n "Remove local bak files... press Enter:"
#read

for file in `ls $archdir --hide=report`
    do
      rm -f $archdir/$file
    done
rm -f /opt/itwrks/backup/db/NOSQL_$tdate.sql

#echo -n "End... press Enter:"
#read
Комплексный скрипт для вызова по расписанию
Код: BASH
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.
#!/bin/bash
#

sname="dragon.itwrks.org"
scdir="/opt/itwrks/scripts"
sc_backup_sys="$scdir/backup_sys.sh"
sc_backup_www="$scdir/backup_www.sh"
sc_backup_sql="$scdir/backup_sql.sh"
email=root
RETVAL=0

do_backup_sys() {
    $sc_backup_sys
    [ $? == 0 ] || exit 88
}

do_backup_www() {
    $sc_backup_www
    [ $? == 0 ] || exit 88
}

do_backup_sql() {
    $sc_backup_sql
    [ $? == 0 ] || exit 88
}

do_backup_all() {
    do_backup_sys
    sleep 5
    do_backup_www
    sleep 5
    do_backup_sql
}

statusYaDisk() {
    yandex-disk status > /dev/null 2>&1
    RETVAL=$?
}

checkYaDisk() {
    statusYaDisk
    if [ $RETVAL != 0 ]; then
        echo "Yandex disk is not started on $sname. Trying to start..." | mail -s "Yandex disk is not started on $sname" $email
        yandex-disk start > /dev/null 2>&1
    fi

    statusYaDisk
    if [ $RETVAL != 0 ]; then
        echo "Failed to start Yandex disk on $sname." | mail -s "Failed to start Yandex disk on $sname" $email
    fi
}

#####

case "$1" in
    --backup-sys)
        do_backup_sys
        ;;
    --backup-www)
        do_backup_www
        ;;
    --backup-sql)
        do_backup_sql
        ;;
    --backup-all)
        do_backup_all
        ;;
    --check-yadisk)
        checkYaDisk
        ;;
    *)
        echo "Usage: $0 --backup-sys | --backup-www | --backup-sql | --backup-all | --check-yadisk"
esac
...
Рейтинг: 0 / 0
Linux: простые скрипты резервного копирования
    #471990
IT-Клоп
Скрыть профиль Поместить в игнор-лист Сообщения автора в теме
Участник
в начале кошерного bash скрипта должно стоять

set -u
set -e
...
Рейтинг: 0 / 0
Linux: простые скрипты резервного копирования
    #472390
basename
Скрыть профиль Поместить в игнор-лист Сообщения автора в теме
Участник
IT-Клоп  06.07.2023, 11:44
[игнорируется]
в начале кошерного bash скрипта должно стоять

set -u
set -e
ну ещё вот это встречал, bash -feu

и такое

set -e
set -u
set -o pipefail
...
Рейтинг: 0 / 0
Linux: простые скрипты резервного копирования
    #542379
bga83
Скрыть профиль Поместить в игнор-лист Сообщения автора в теме
Участник
Есть же варианты типа duplicity, которые скрипты в несколько раз сократили
...
Рейтинг: 0 / 0
4 сообщений из 4, страница 1 из 1
Форумы / UNIX-подобные OC [закрыт для гостей] / Linux: простые скрипты резервного копирования
Целевая тема:
Создать новую тему:
Автор:
Закрыть
Цитировать
Найденые пользователи ...
Разблокировать пользователей ...
Читали тему (1): Анонимы (1)
Игнорируют тему (1): erbol 
Читали форум (3): Анонимы (3)
Пользователи онлайн (125): Анонимы (109), Разум вселенной, a.v.z, Брюквенные годы 1 мин., Yandex Bot 1 мин., Ветер 2 мин., erbol 2 мин., basename 3 мин., Google Bot 4 мин., Горбатый ёж 4 мин., zzdima 5 мин., Bing Bot 6 мин., Antonariy 6 мин., IT-Клоп 7 мин., Шоколадный01 8 мин., Кусь 8 мин., Гарыныч 8 мин.
x
x
Закрыть


Просмотр
0 / 0
Close
Debug Console [Select Text]