├── .gitignore ├── README.md ├── backup.sh ├── backup_all.sh ├── backup_zip.sh ├── dump.sh ├── dump_all.sql ├── execute.sh ├── execute_all.sh ├── index_backup.sh ├── index_recover.sh ├── name_reset.sh ├── privilege.sh ├── privilege_all.sh ├── recover.sh ├── recover_all.sh ├── recover_zip.sh └── remove.sh /.gitignore: -------------------------------------------------------------------------------- 1 | config 2 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Backup Script for Oneinstack 2 | The script to backup and recover PHP website for oneinstack(https://oneinstack.com) running on Linux server. 3 | 4 | Oneinstack(https://github.com/lj2007331/oneinstack) is a script written using the shell, in order to quickly deploy LEMP/LAMP/LNMP/LNMPA/LTMP(Linux, Nginx/Tengine/OpenResty, MySQL in a production environment/MariaDB/Percona, PHP, JAVA), applicable to CentOS 57(including redhat), Debian 68, Ubuntu 12~16 of 32 and 64. 5 | 6 | ## Setup 7 | 8 | The format of config file: 9 | 10 | ```shell 11 | wwwroot=/data/wwwroot/ 12 | indexes=/data/indexes/ 13 | indexFiles=index.html/index.php 14 | nginx=/usr/local/nginx/conf/vhost/ 15 | username= 16 | password= 17 | ``` 18 | Create a config file named `config` in the backup script folder and add the config above. 19 | 20 | ## Documentation 21 | 22 | **All folder should endup with "/".** 23 | 24 | - backup.sh: 25 | - Backup a site to a folder. 26 | - All files of the site, sql backup of database and nginx configuration file will be copied to the folder. 27 | 28 | ```shell 29 | ./backup.sh [site] [path] 30 | ``` 31 | 32 | - backup_zip.sh: 33 | - Backup a site to a zip file. 34 | 35 | ```shell 36 | ./backup_zip.sh [site] [path] 37 | ``` 38 | 39 | - recover.sh: 40 | - Recover a site from the folder created by the shell script backup.sh. 41 | - Nginx server should be restarted manually after recovering the website. 42 | 43 | ```shell 44 | ./recover.sh [site] [path] 45 | service nginx restart 46 | ``` 47 | 48 | - recover_all.sh: 49 | - Recover all sites from the folder that contains zip files created by backup_zip.sh. 50 | - Here, the name of zip file will be used as the name of the site. 51 | - Nginx server should be restarted manually after recovering the website. 52 | 53 | ```shell 54 | ./recover_all.sh [path] 55 | service nginx restart 56 | ``` 57 | 58 | - dump.sh: 59 | - Dump a database to a sql file. 60 | 61 | ```shell 62 | ./dump.sh [site] [path] 63 | ``` 64 | 65 | - execute.sh: 66 | - Execute a sql file and restore it into a new database. 67 | - The name of the new database is the parameter site. 68 | 69 | ```shell 70 | ./execute.sh [site] [path] 71 | ``` 72 | 73 | - execute_all.sh: 74 | - Execute all sql files in a folder and store them to databases. 75 | - The name of a database is same with the name of a sql file. 76 | 77 | ```shell 78 | ./execute.sh [folder] 79 | ``` 80 | 81 | - privilege.sh: 82 | - Change privilege of a DedeCMS website. 83 | 84 | ```shell 85 | ./privilege.sh [path] 86 | ``` 87 | 88 | - remove.sh: 89 | - Remove a website. 90 | 91 | ```shell 92 | ./remove.sh [site] 93 | ``` 94 | 95 | - index_backup.sh: 96 | - Backup all index files defined in indexFiles of the config file. 97 | 98 | ```shell 99 | ./index_backup.sh 100 | ``` 101 | 102 | - index_recover.sh: 103 | - Recover all index files defined in indexFiles of the config file. 104 | - This script can be added to crontab in order to recover index files automatically. 105 | 106 | ```shell 107 | ./index_recover.sh 108 | ``` -------------------------------------------------------------------------------- /backup.sh: -------------------------------------------------------------------------------- 1 | while read line;do 2 | eval "$line" 3 | done < config 4 | 5 | site=$1; 6 | path=$2; 7 | 8 | backupPath="$path$site/" 9 | 10 | mkdir $backupPath 11 | echo "Copying source file from $wwwroot$site to $backupPath." 12 | cp -rf "$wwwroot/$site" $backupPath 13 | echo "Copying nginx config file from $nginx$site.conf to $backupPath" 14 | cp "$nginx/$site.conf" $backupPath 15 | echo "Dumping database $site to file ${backupPath}$site.sql." 16 | mysqldump -u $username -p$password $site > "${backupPath}$site.sql" 17 | -------------------------------------------------------------------------------- /backup_all.sh: -------------------------------------------------------------------------------- 1 | while read line;do 2 | eval "$line" 3 | done < config 4 | 5 | path=$1; 6 | 7 | for file in `ls $wwwroot` 8 | do 9 | site="${file%.*}"; 10 | if [ "$site" == "default" ]; then 11 | continue 12 | fi 13 | echo "Backup the website $site" 14 | sh backup_zip.sh $site $path 15 | done 16 | -------------------------------------------------------------------------------- /backup_zip.sh: -------------------------------------------------------------------------------- 1 | site=$1; 2 | path=$2; 3 | 4 | sh backup.sh $site $path 5 | 6 | cd $path 7 | zip -r $site.zip $site 8 | rm -rf $site 9 | -------------------------------------------------------------------------------- /dump.sh: -------------------------------------------------------------------------------- 1 | while read line;do 2 | eval "$line" 3 | done < config 4 | 5 | site=$1; 6 | path=$2; 7 | 8 | echo "Dumping database $site to file $site.sql." 9 | mysqldump -u $username -p$password $site > "$path/$site.sql" 10 | -------------------------------------------------------------------------------- /dump_all.sql: -------------------------------------------------------------------------------- 1 | while read line;do 2 | eval "$line" 3 | done < config 4 | 5 | path=$1; 6 | 7 | for file in `ls $wwwroot` 8 | do 9 | site="${file%.*}"; 10 | if [ "$site" == "default" ]; then 11 | continue 12 | fi 13 | echo "Backup the website $site" 14 | sh dump.sh $site $path 15 | done 16 | -------------------------------------------------------------------------------- /execute.sh: -------------------------------------------------------------------------------- 1 | while read line;do 2 | eval "$line" 3 | done < config 4 | 5 | site=$1; 6 | sql=$2; 7 | 8 | echo "drop database if exists ${site}; create database ${site} character set utf8 collate utf8_general_ci; use ${site}; source ${sql};" | mysql -u $username -p$password 9 | -------------------------------------------------------------------------------- /execute_all.sh: -------------------------------------------------------------------------------- 1 | path=$1; 2 | 3 | for file in `ls $path` 4 | do 5 | site="${file%.*}"; 6 | sql="$path$file"; 7 | echo "Recovering sql file $sql to database $site..." 8 | sh "`pwd`/execute.sh" "$site" "$sql" 9 | done 10 | -------------------------------------------------------------------------------- /index_backup.sh: -------------------------------------------------------------------------------- 1 | while read line;do 2 | eval "$line" 3 | done < config 4 | 5 | files=$(echo $indexFiles|tr "/" "\n") 6 | 7 | if [ ! -d $indexes ];then 8 | mkdir $indexes 9 | fi 10 | 11 | for file in `ls $wwwroot` 12 | do 13 | site="${file%.*}"; 14 | if [ "$site" == "default" ]; then 15 | continue 16 | fi 17 | echo "Backup the index files of the website $site" 18 | indexBackPath="$indexes$site" 19 | rm -rf $indexBackPath 20 | mkdir $indexBackPath 21 | for file in ${files[@]}; do 22 | filePath="$wwwroot$site/$file" 23 | if [ -f $filePath ];then 24 | echo " $filePath has been saved." 25 | cp $filePath "$indexes$site/$file" 26 | fi 27 | done 28 | done -------------------------------------------------------------------------------- /index_recover.sh: -------------------------------------------------------------------------------- 1 | while read line;do 2 | eval "$line" 3 | done < config 4 | 5 | files=$(echo $indexFiles|tr "/" "\n") 6 | 7 | for file in `ls $indexes` 8 | do 9 | site="${file%.*}"; 10 | if [ "$site" == "default" ]; then 11 | continue 12 | fi 13 | path="$wwwroot$site" 14 | if [ -d $path ];then 15 | echo "Recover the index files of the website $site" 16 | for file in ${files[@]}; do 17 | filePath="$path/$file" 18 | if [ -f $filePath ];then 19 | rm -rf $filePath 20 | cp "$indexes$site/$file" $filePath 21 | chmod 755 $filePath 22 | chown www:www $filePath 23 | echo " $filePath has been recoverd." 24 | fi 25 | done 26 | fi 27 | done -------------------------------------------------------------------------------- /name_reset.sh: -------------------------------------------------------------------------------- 1 | path=$1; 2 | 3 | for file in `ls $path` 4 | do 5 | site="${file%.*}"; 6 | echo $site 7 | mv "$path/$site/index.html1" "$path/$site/index.html" 8 | mv "$path/$site/index.php1" "$path/$site/index.php" 9 | done 10 | -------------------------------------------------------------------------------- /privilege.sh: -------------------------------------------------------------------------------- 1 | path=$1; 2 | 3 | chmod -R 755 $path 4 | chown -R www:www $path 5 | 6 | chmod -R 555 "${path}install" 7 | chmod -R 755 "${path}dede" 8 | chmod -R 755 "${path}include" 9 | chmod -R 555 "${path}member" 10 | chmod -R 755 "${path}plus" 11 | chmod -R 755 "${path}a" 12 | chmod -R 666 "${path}data" 13 | chmod -R 666 "${path}html" 14 | chmod -R 666 "${path}templets" 15 | chmod -R 755 "${path}uploads" 16 | chmod -R 555 "${path}company" 17 | chmod -R 755 "${path}special" 18 | chmod -R 755 "${path}book" 19 | chmod -R 555 "${path}ask" 20 | chmod -R 555 "${path}group" 21 | -------------------------------------------------------------------------------- /privilege_all.sh: -------------------------------------------------------------------------------- 1 | path=$1; 2 | 3 | for file in `ls $path` 4 | do 5 | folder="$path$file/"; 6 | echo "Set privilege for $folder" 7 | sh privilege.sh $folder 8 | done 9 | -------------------------------------------------------------------------------- /recover.sh: -------------------------------------------------------------------------------- 1 | while read line;do 2 | eval "$line" 3 | done < config 4 | 5 | site=$1; 6 | path=$2; 7 | 8 | recoverPath="$wwwroot$site" 9 | 10 | echo "Copying source file from $path$site to $recoverPath." 11 | cp -rf "$path$site" $recoverPath 12 | chmod -R 755 $recoverPath 13 | chown -R www:www $recoverPath 14 | 15 | echo "Reovering database $site" 16 | echo "drop database if exists ${site}; create database ${site} character set utf8 collate utf8_general_ci; use ${site}; source ${path}${site}.sql;" | mysql -u $username -p$password 17 | 18 | echo "Copying nginx config file from $path$site.conf to $nginx$site.conf" 19 | cp "$path$site.conf" "$nginx$site.conf" 20 | -------------------------------------------------------------------------------- /recover_all.sh: -------------------------------------------------------------------------------- 1 | path=$1; 2 | 3 | for file in `ls $path` 4 | do 5 | site="${file%.*}"; 6 | folder="$path$file"; 7 | echo "Recovering site $site from $folder." 8 | sh recover_zip.sh $site $folder 9 | done 10 | -------------------------------------------------------------------------------- /recover_zip.sh: -------------------------------------------------------------------------------- 1 | site=$1; 2 | path=$2; 3 | 4 | unzip $path 5 | sh recover.sh $site "`pwd`/$site/" 6 | rm -rf "`pwd`/$site/" 7 | -------------------------------------------------------------------------------- /remove.sh: -------------------------------------------------------------------------------- 1 | while read line;do 2 | eval "$line" 3 | done < config 4 | 5 | site=$1; 6 | 7 | path="$wwwroot$site/" 8 | 9 | rm -rf $path 10 | echo "Deleting source file from $path." 11 | 12 | echo "Deleting nginx config file from $nginx$site.conf" 13 | rm -rf "$nginx/$site.conf" 14 | 15 | echo "Deleting database $site." 16 | echo "drop database if exists ${site};" | mysql -u $username -p$password 17 | --------------------------------------------------------------------------------