- 备份Drupal sites目录并压缩
- 备份Drupal数据库并压缩
在运行脚本之前,要创建一个用于存放备份文件的目录,如SiteBackup,然后分别创建DB和Site目录分别用于存放数据库的Drupal程序文件。
脚本source code如下:
# Usage:# backup_drupal_db.ps1Write-Host "This script will backup&zip the drupal database and zip it."$site_name = "rzchina"$db_name = "rzchina"$drupal_site_dir = "C:\XAMPP\xampplite\htdocs\rzchina\sites"$target_db_dir = "C:\Users\example.net\Site-Backup\DB"$target_drupal_dir = "C:\Users\example.net\Site-Backup\Site"$mysql_bin_dir = "C:\XAMPP\xampplite\mysql\bin"$7zip_bin_dir = "C:\Program Files\7-Zip"$curr_dir = Get-Location$curr_date = date -format "yyyyMMdd-Hmss"# Backup database$target_db_filename = $target_db_dir + "\" + $db_name.ToString() + "_" +$curr_date.ToString()$sql_filename = $target_db_filename + ".sql"Write-Host "Start backup db..."cd $mysql_bin_dir./mysqldump -uroot -c -e --default-character-set=gbk --skip-set-charset --hex-blob $db_name > tmp_sqlget-content tmp_sql | set-content -encoding utf8 $sql_filenameWrite-Host "DB backup ended."# Zip db fileWrite-Host "Start zipping...";cd $7zip_bin_dir$zip_filename = $target_db_filename + ".7z"./7z.exe a $zip_filename $sql_filename# Clean upWrite-Host "Clean up..."del $sql_filename;# Zip Drupal Site File$zip_site_filename = $target_drupal_dir + "\" + $site_name + $curr_date.ToString() + ".7z";./7z.exe a $zip_site_filename $drupal_site_dirWrite-Host "The End."cd $curr_dir
由于是第一个脚本,因此略显幼稚,不过任务还完成的不错。接下来研究如何传递参数,实现更通用的脚本。