Skip to content

Instantly share code, notes, and snippets.

@kjprince
Created February 17, 2015 18:13
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kjprince/4b1523fdce4448b9f926 to your computer and use it in GitHub Desktop.
Save kjprince/4b1523fdce4448b9f926 to your computer and use it in GitHub Desktop.
Server Cron Job and Backup
# ------------------------------------------------------------------------------
# SETUP CRON JOBS 4 Backup + Server Love ---------------------------------------
# ------------------------------------------------------------------------------
# Start by opening the default logger
nano /etc/rsyslog.d/50-default.conf
# Start tracking cron jobs
Uncomment: cron.* [remove the "#" around line 10]
Uncomment: daemon.* [remove the "#" around line 11]
# save file, exit
ctrl+x, y, enter = save file and exit
# restart the logger service
sudo service rsyslog restart
# Setup the nightly server reboot process
nano /etc/cron.daily/server-reboot
# add all of the text between the starting [[ and ending ]]
# [[
#!/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
# ]]
# save file, exit
ctrl+x, y, enter = save file and exit
# Make the job executable
chmod +x /etc/cron.daily/server-reboot
# Open crontab configuration
nano /etc/crontab
# Add the following under "# m h dom mon dow user command" starting [[ and ending ]] at the end of the file:
# [[
# Reboot Server Daily @ 1:30 AM
30 01 * * * root /etc/cron.daily/server-reboot
# ]]
# save file, exit
ctrl+x, y, enter = save file and exit
# Optional: you can test this job if you like by typing the following command
sh /etc/cron.daily/server-reboot
# Setup the nightly server backup database processes
mkdir /srv/backup/daily
mkdir /srv/backup/daily/databases
nano /etc/cron.daily/backup-databases
# add all of the text between the starting [[ and ending ]]
# [[
#!/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
21mysqldump --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
# ]]
# save file, exit
ctrl+x, y, enter = save file and exit
# Make the job executable
chmod +x /etc/cron.daily/backup-databases
# Open crontab configuration
nano /etc/crontab
# Add the following under "# m h dom mon dow user command" starting [[ and ending ]] at the end of the file:
# [[
# Backup Databases Daily @ 12:30 AM
30 00 * * * root /etc/cron.daily/backup-databases
# ]]
# save file, exit
ctrl+x, y, enter = save file and exit
# Optional: you can test this job if you like by typing the following command
sh /etc/cron.daily/backup-databases
# Setup the nightly server backup file processes
mkdir /srv/backup/daily
mkdir /srv/backup/daily/websites
nano /etc/cron.daily/backup-wordpress-site-files
# add all of the text between the starting [[ and ending ]]
# [[
#!/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
21#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
# ]]
# save file, exit
ctrl+x, y, enter = save file and exit
# Make the job executable
chmod +x /etc/cron.daily/backup-wordpress-site-files
# Open crontab configuration
nano /etc/crontab
# Add the following under "# m h dom mon dow user command" starting [[ and ending ]] at the end of the file:
# [[
# Backup Wordpress Site Files Daily @ 12:45 AM
45 00 * * * root /etc/cron.daily/backup-wordpress-site-files
# ]]
# save file, exit
ctrl+x, y, enter = save file and exit
# Optional: you can test this job if you like by typing the following command
sh /etc/cron.daily/backup-wordpress-site-files
# Setup the nightly server backup cleanup
nano /etc/cron.daily/backup-cleanup
# add all of the text between the starting [[ and ending ]]
# [[
#!/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
# ]]
# save file, exit
ctrl+x, y, enter = save file and exit
# Make the job executable
chmod +x /etc/cron.daily/backup-cleanup
# Open crontab configuration
nano /etc/crontab
# Add the following under "# m h dom mon dow user command" starting [[ and ending ]] at the end of the file:
# [[
# Remove Backups Greater than 7 Days Old Daily @ 01:00 AM
00 01 * * * root /etc/cron.daily/backup-cleanup
# ]]
# save file, exit
ctrl+x, y, enter = save file and exit
# Optional: you can test this job if you like by typing the following command
sh /etc/cron.daily/backup-cleanup
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment