sh Ubuntu服务器CRON脚本:重新启动服务器,备份数据库,备份Wordpress站点文件,备份清理,Ubuntu服务器日志轮换计划

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了sh Ubuntu服务器CRON脚本:重新启动服务器,备份数据库,备份Wordpress站点文件,备份清理,Ubuntu服务器日志轮换计划相关的知识,希望对你有一定的参考价值。

#!/bin/bash
#start
#-----------------------------------------------------------------------
find /srv/backup/daily/databases/ -name '*.gz' -mtime +7 | xargs rm -f;
find /srv/backup/daily/websites/ -name '*.gz' -mtime +7 | xargs rm -f;
# Are Weekly Backups Implemented? 
# find /srv/backup/weekly/ -name '*.gz' -mtime +30 | xargs rm -f;
#-----------------------------------------------------------------------
#end

# Example Schedule
# Remove Backups Greater than 7 Days Old Daily @ 01:00 AM
# 00 01 * * * root /etc/cron.daily/backup-cleanup
#!/bin/bash
#start
#-----------------------------------------------------------------------
#verify directory structure exists prior to running this job
BackUpDIR="/srv/backup/daily/databases/";
DateStamp=$(date +"%Y%m%d");

#Format of DBList="db1 db2 db3 db4"
DBList="[dbname]";
#I have a server system administrator account with access to all dbs, typically named sysadmin
DBUser="[user with db permissions like sysadmin]";
DBPwd="[user password]";

for DB in $DBList; 
do
mysqldump --opt -u$DBUser -p$DBPwd --add-drop-table --lock-tables --databases $DB > $BackUpDIR$DateStamp.$DB.sql;
	tar zcf "$BackUpDIR$DateStamp.DB.$DB.tar.gz" -P $BackUpDIR$DateStamp.$DB.sql;
	rm -rf $BackUpDIR$DateStamp.$DB.sql;
	mysqldump --opt -u$DBUser -p$DBPwd --add-drop-table --lock-tables $DB > $BackUpDIR$DateStamp.$DB.tbls.sql;
	tar zcf "$BackUpDIR$DateStamp.DB.$DB.tbls.tar.gz" -P $BackUpDIR$DateStamp.$DB.tbls.sql;
	rm -rf $BackUpDIR$DateStamp.$DB.tbls.sql;
done
#-----------------------------------------------------------------------
#end

# Example Schedule
# Backup Databases Daily @ 12:30 AM
# 30 00 * * *	root /etc/cron.daily/backup-databases
#!/bin/bash
#start
#-----------------------------------------------------------------------
#verify directory structure exists prior to running this job
BackUpDIR="/srv/backup/daily/websites/";
SrvDir="/srv/www/";
#Format of SiteList="sitefolder1 sitefolder2 sitefolder3"
SiteList="[sitefolders]";
DateStamp=$(date +"%Y%m%d");

for Site in $SiteList; 
do
#backup all files, however, exclude the rackspace cloud cdn if you are using it
 	tar zcf "$BackUpDIR$DateStamp.website.code.$Site.tar.gz" -P $SrvDir$Site --exclude "$SrvDir$Site/wp-content-cloudfiles";
done
#-----------------------------------------------------------------------
#end

# Example Schedule
# Backup Wordpress Site Files Daily @ 12:45 AM
# 45 00 * * *	root /etc/cron.daily/backup-wordpress-site-files

# see "man logrotate" for details
# rotate log files weekly
daily
 
# keep 2 weeks worth of backlogs
rotate 1
 
# create new (empty) log files after rotating old ones
create
 
# use the date in backlog filenames
dateext
 
# compress backlogs with a delay
compress
 
# packages drop log rotation information into this directory
include /etc/logrotate.d
 
# no packages own wtmp, or btmp -- we'll rotate them here
/var/log/wtmp {
    missingok
    weekly
    create 0664 root utmp
    rotate 7
}
 
/var/log/btmp {
    missingok
    weekly
    create 0664 root utmp
    rotate 7
}
 
# system-specific logs may be configured here
#!/bin/bash
#start
#-----------------------------------------------------------------------

#delete nginx cache if exists
rm -rf /var/cache/nginx
#restart server
DateStamp=$(date +"%Y%m%d %k:%M:%S");
echo $DateStamp >> /var/log/cron.reboot.log;
/sbin/shutdown -r now

#-----------------------------------------------------------------------
#end

# Example Schedule
# Reboot Server Daily @ 1:30 AM
# 30 01 * * *  root /etc/cron.daily/server-reboot

以上是关于sh Ubuntu服务器CRON脚本:重新启动服务器,备份数据库,备份Wordpress站点文件,备份清理,Ubuntu服务器日志轮换计划的主要内容,如果未能解决你的问题,请参考以下文章

linux ubuntu /bin/sh 上的脚本错误:curl:找不到? [关闭]

从 cron 重新启动弹性 beanstalk 应用程序服务器

sh 在Ubuntu 16.04 linux服务器上启动,停止和重新启动MySQL

脚本手动执行正常,放cron中执行有问题的原因

如何让 NGINX 从 cron 作业重新启动/重新加载?

@reboot 在 CRON 中不起作用