├── README └── backup.sh /README: -------------------------------------------------------------------------------- 1 | ####################################################### 2 | # git-sitebackup v0.2 3 | ####################################################### 4 | 5 | # Usage: backup.sh webrootdir dbname dbuser dbpass 6 | # 7 | # Full backup of website files, tool scripts and database content. 8 | # 9 | # A number of variables defining file location and database connection 10 | # information are sent as arguments 11 | # 12 | # Files are copied to a local git repository and pushed up to master repo. 13 | # All files are copied. 14 | # 15 | # Useful to setup cron to run daily and do automatic backups to master repo. 16 | 17 | # Usage: 18 | # Backup files only locally: 19 | 20 | > backup /path/to/dir 21 | 22 | # Backup files locally and to remote origin 23 | 24 | > backup /path/to/dir branch-name 25 | 26 | # backup files and database locally 27 | 28 | > backup /path/to/dir db-name db-user db-password 29 | 30 | # backup files and database locally and to remote 31 | 32 | > backup /path/to/dir db-name db-user db-password branch-name 33 | 34 | 35 | -------------------------------------------------------------------------------- /backup.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | ####################################################### 4 | # git-sitebackup 5 | ####################################################### 6 | version="0.1" 7 | 8 | ####################################################### 9 | # Banner 10 | ####################################################### 11 | echo -e "\n git-sitebackup $version \n" 12 | 13 | ####################################################### 14 | # sqldump database information 15 | ####################################################### 16 | function dump_db { 17 | dir=$1 18 | dbname=$2 19 | dbuser=$3 20 | dbpass=$4 21 | 22 | echo -e " .. sqldump'ing database:" 23 | echo -e " user: $dbuser database: $dbname" 24 | cd $dir 25 | mysqldump --password=$dbpass --user=$dbuser --skip-extended-insert $dbname > dbcontent.sql 26 | echo -e " done\n" 27 | } 28 | 29 | 30 | 31 | ####################################################### 32 | # Commit to Git Repo 33 | ####################################################### 34 | function commit_changes { 35 | dir=$1 36 | 37 | echo -e " .. Committing to local git repository at $dir" 38 | datestamp=$(date +"%Y-%m-%d") 39 | timestamp=$(date +"%H:%M") 40 | cd $dir 41 | git add . 42 | git commit -am "Backup created on $datestamp at $timestamp." 43 | echo -e " done\n" 44 | } 45 | 46 | 47 | 48 | 49 | ####################################################### 50 | # Push out to Master 51 | ####################################################### 52 | function push_to_remote { 53 | dir=$1 54 | branch=$2 55 | 56 | echo -e " .. Pushing backup to $2 repo" 57 | git push origin $2 58 | echo -e " done\n" 59 | 60 | } 61 | 62 | 63 | ####################################################### 64 | # Cleanup 65 | ####################################################### 66 | function cleanup { 67 | echo -e " .. Cleaning up" 68 | git gc 69 | echo -e " done\n" 70 | 71 | 72 | # Exit banner 73 | echo -e " .. Full site backup complete\n" 74 | } 75 | 76 | 77 | 78 | ####################################################### 79 | # Grand Central... direct to the correct actions 80 | # based on number of inputs 81 | ####################################################### 82 | 83 | 84 | # only filepath present: do not backup dB and do not push to remote 85 | if [ $# -eq 1 ] ; then 86 | 87 | 88 | 89 | dir=$1 90 | commit_changes $dir 91 | 92 | 93 | 94 | # only filepath & remote branch present: do not backup dB, push to remote 95 | elif [ $# -eq 2 ]; then 96 | 97 | dir=$1 98 | branch=$2 99 | 100 | commit_changes $dir 101 | push_to_remote $dir $branch 102 | 103 | 104 | # filepath and dB info present: backup both, do not push to remote 105 | elif [ $# -eq 4 ]; then 106 | 107 | dir=$1 108 | dbname=$2 109 | dbuser=$3 110 | dbpass=$4 111 | 112 | dump_db $dir $dbname $dbuser $dbpass 113 | commit_changes $dir 114 | 115 | # all arguments present: backup files and db, push to remote 116 | elif [ $# -eq 5 ]; then 117 | dir=$1 118 | dbname=$2 119 | dbuser=$3 120 | dbpass=$4 121 | branch=$5 122 | 123 | dump_db $dir $dbname $dbuser $dbpass 124 | commit_changes $dir 125 | push_to_remote $dir $branch 126 | 127 | fi 128 | 129 | #cleanup and exit 130 | cleanup 131 | 132 | 133 | --------------------------------------------------------------------------------