Скрипт для бекапа сайта на удаленный сервер по FTP.
Скрипт бекапит всю рабочую директорию пофайлово (для того, чтобы сайт сразу после бекапа был доступен ан резервной площадке - бесплатного хостера), а также делает общий шифрованный архив, разбивает его на небольшие куски и кладет в директорию с текущей датой.
Скрипт проверяет, что глубина архива (число директорий с полным шифрованным архивом) не превышает заданного лимита и удаляет старое.
#!/bin/bash dir_to_backup=/pve-data/kubernetes_volumes/wiki-data backup_name_prefix=wiki ftpserver=ftp.server.com username=ftp_login password=ftp_pass currentdate=$(date +%d-%m-%Y_%H_%M) remote_www_dir=/htdocs backup_depth_days=10 encryption_password=enc_pass tmp_dir=/pve-data/tmp tmp_arc_dir=$tmp_dir/$backup_name_prefix_$currentdate #Get list of directories with full encrypted backups dirs=`echo "lftp -e 'set ssl:verify-certificate no ssl:check-hostname false net:connection-limit 4; cls --sort=date $remote_www_dir/$backup_name_prefix; bye;' ftp://$username:$password@$ftpserver" | /bin/bash` dirs=(${dirs// / }) #check number of directories and delete oldest if [[ ${#dirs[@]} -gt $backup_depth_days ]]; then for ((i = $backup_depth_days; i < ${#dirs[@]}; i++)) { echo "lftp -e 'set ssl:verify-certificate no ssl:check-hostname false net:connection-limit 4; rm -r -f ${dirs[$i]}; bye;' ftp://$username:$password@$ftpserver" | /bin/bash } fi #Create tmp folder mkdir --parents $tmp_arc_dir #Disable URL rewrites sed -i '/userewrite/s/1/0/' $dir_to_backup/conf/local.php #Make ENCRYPTED archive tar -cpf - --use-compress-program=pigz $dir_to_backup | openssl enc -aes-192-cbc -pass pass:$encryption_password -out $tmp_arc_dir/$backup_name_prefix-$currentdate.tar.gz #Enable URL rewrites sed -i '/userewrite/s/0/1/' $dir_to_backup/conf/local.php #Split archive to small parts cd $tmp_arc_dir split -d -a 3 -b 8000000 ./$backup_name_prefix-$currentdate.tar.gz #Calculate md5 checksum md5sum $tmp_arc_dir/$backup_name_prefix-$currentdate.tar.gz > $tmp_arc_dir/$backup_name_prefix-$currentdate.tar.gz.md5 rm -f $tmp_arc_dir/$backup_name_prefix-$currentdate.tar.gz #Put archive parts to remote ftp and remove tmp dir echo "lftp -e 'set ssl:verify-certificate no ssl:check-hostname false net:connection-limit 4; mkdir $remote_www_dir/$backup_name_prefix; mkdir $remote_www_dir/$backup_name_prefix/$currentdate; mirror -c -R $tmp_arc_dir $remote_www_dir/$backup_name_prefix/$currentdate; bye;' ftp://$username:$password@$ftpserver" | /bin/bash rm -Rf $tmp_arc_dir #Mirror current site to remote server echo "lftp -e 'set ssl:verify-certificate no ssl:check-hostname false net:connection-limit 4; mirror --use-pget-n=10 -c -R -e --exclude=data/tmp/captcha/ --exclude=data/cache/ $dir_to_backup $remote_www_dir; bye;' ftp://$username:$password@$ftpserver" | /bin/bash #Disable URL rewrites sed -i '/userewrite/s/1/0/' $dir_to_backup/conf/local.php #replace remote conf.php with disabled rewrites echo "lftp -e 'set ssl:verify-certificate no ssl:check-hostname false net:connection-limit 4; put -O $remote_www_dir/conf $dir_to_backup/conf/local.php; bye;' ftp://$username:$password@$ftpserver" | /bin/bash #Enable URL rewrites sed -i '/userewrite/s/0/1/' $dir_to_backup/conf/local.php
#!/bin/bash backup_name_prefix=wiki ftpserver=ftp.unaux.com username=unaux_user password=superpassword remote_www_dir=/htdocs encryption_password=superencryptionpassword echo "lftp -e 'set ssl:verify-certificate no ssl:check-hostname false net:connection-limit 4; mirror -c $remote_www_dir $dir_to_backup; bye;' ftp://$username:$password@$ftpserver" | /bin/bash
#!/bin/bash backup_name_prefix=wiki ftpserver=ftp.unaux.com username=unaux_user password=superpassword remote_www_dir=/htdocs encryption_password=superencryptionpassword restore_dir=~/restore dirs=`echo "lftp -e 'set ssl:verify-certificate no ssl:check-hostname false net:connection-limit 4; cls --sort=date $remote_www_dir/$backup_name_prefix; bye;' ftp://$username:$password@$ftpserver" | /bin/bash` dirs=(${dirs// / }) latest_backup_dir=${dirs[0]} mkdir -p $restore_dir echo "lftp -e 'set ssl:verify-certificate no ssl:check-hostname false net:connection-limit 4; mirror -c $latest_backup_dir $restore_dir; bye;' ftp://$username:$password@$ftpserver" | /bin/bash archive_filename=`ls $restore_dir/*md5 | sed 's/.md5//'` cat $restore_dir/x* > $archive_filename openssl enc -in $archive_filename -d -aes-192-cbc -pass pass:$encryption_password | sudo tar -xzvf - -C $restore_dir